iOS 7 BUG – NSAttributedString does not appear

I have found a workaround for this bug. In your github code, to use the workaround, you would say this:

NSAttributedString* attrStr = 
    [[NSAttributedString alloc] initWithString:@"Lorem ipsum dolor sit\n" // <---
    attributes:
        @{NSUnderlineStyleAttributeName:@(NSUnderlineStyleSingle),
        NSParagraphStyleAttributeName:paragraph}];

UILabel* myLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 30, 0, 0)];
myLabel.backgroundColor = [UIColor greenColor];
myLabel.attributedText = attrStr;
[myLabel sizeToFit];

myLabel.numberOfLines = 2; // <---

I have made two changes: I’ve appended a newline character to your original string, and I’ve set the label’s numberOfLines to 2.

What the workaround does is to force the label’s text up against the top of the label. This seems to solve the problem. In other words, the bug appears to stem from the label’s attempt to vertically center its text; if you deliberately make the text too long for the size of the label by juggling the label’s height, numberOfLines, and excess newline characters at the end of the text, the attributed string will be drawn.

EDIT I’ve just found another workaround along the same lines: let the label resize itself to fit the text. See my code and explanation here: https://stackoverflow.com/a/19545193/341994 In that code, I do the same thing from the opposite end, as it were: I give the label a fixed width constraint but a flexible height constraint, and by setting its own height, the label brings the top of its text up against the top of itself, and thus is able to display the text correctly. In other words, this is just another way of preventing the label from centering its text vertically, which is what seems to trigger the bug.

FURTHER EDIT I have the sense that this bug may get fixed in iOS 7.1.

Leave a Comment