iOS 7 BUG - NSAttributedString does not appear

15,291

Solution 1

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.

Solution 2

I also had the same problem when setting the background color on text of a UILabel in a UITableViewCell. My workaround was to use a UITextView with UserInteraction disabled instead of a UILabel in the cell and it worked.

Update: Found the issue only appearing with UILabel included in Basic UITableViewCell.

Update 2: Also found that the problem does not occur when a UILabel wraps to multiple lines of text. One workaround is to force text to wrap by adding a newline and space. Very hacky, but it works. Make sure numberOfLines is set to zero and lineBreakMode is set to NSLineBreakByWordWrapping.

Solution 3

I had the same issue in my application. It was occurring in the simulators, as well as on my device (iPhone 5 running 7.0.2 (11A501)). I then realized that my UILabels living in other ViewControllers were displaying NSAttributedStrings using the NSUnderlineStyleSingle attribute properly.

After some troubleshooting. It seems that if you're using the default font (I'm using System 17.0) and your UILabel has a height of less than 62 pixels, it will display correctly regardless of what background color, text color, or alignment you are using. A change of the UILabel's height to a value greater than 61 pixels, allowing auto-sizing to change the height for you, or a change of the font to a Custom one will result in the disappearing of the underlined NSAttributedText.

At first I thought this issue might be due to my positioning the UILabel behind the new Status Bar (or lack thereof), but even in positions which would interact with this new feature, the height-rule still held. I find it hard to believe that the height of the UILabel would cause such an issue, but that's the conclusion I came to.

Solution 4

Wow, took me a while to find this. Looks like I'm have a similar problem as Indi. Setting the background color of an attributed string caused the text to just disappear. Only place I can reproduce this is on a device running iOS 7.0.3.

Solution 5

workaround: use an image view

CGRect rect = self.frame;

CGRect rr = [attribText boundingRectWithSize:rect.size options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesDeviceMetrics context:nil];
UIGraphicsBeginImageContextWithOptions(rr.size, NO, 0.);
[attribText drawWithRect:rr options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesDeviceMetrics context:nil];
UIImage* image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageView *imView = [[UIImageView alloc]initWithImage:image];
[self addSubview:imView];
Share:
15,291

Related videos on Youtube

RohinNZ
Author by

RohinNZ

I do software development. Mostly iOS stuff.

Updated on July 14, 2022

Comments

  • RohinNZ
    RohinNZ almost 2 years

    Last week I asked a question about a Simulator bug with NSAttributedString not displaying: iOS 7 Simulator Bug - NSAttributedString does not appear

    Unfortunately it now appears this is not a simulator bug but an iOS 7 bug. I have now reproduced this issue on an iPhone 5 device.

    The bug appears to be the combination of using NSUnderlineStyleAttributeName & NSParagraphStyleAttributeName as attributes for a NSAttributedString.

    I have only tested on two iOS 7 devices so far, and the issue has only appeared on one of them. Even after they have both been upgraded to the exact same version:

    • 1st iPhone 5 with iOS 7.0 (11A465): Text does NOT appear

    • 1st iPhone 5 after upgrading to 7.0.2 (11A501): Text does NOT appear

    • 2nd iPhone 5 running iOS 7.0 (11A4449d): Text displays correctly

    • 2nd iPhone 5 after upgrading to 7.0.2 (11A501): Text does NOT appear

    So it appears Apple introduced this bug after iOS 7.0 (11A4449d). I've filed a bug with them and will update you on any response I get.

    Steps to reproduce bug

    If you are running iOS 7.0.2 then you should be able to reproduce this bug.

    Either download and run this project on your device https://github.com/rohinnz/iOS-7-BUG---NSAttributedString-does-not-appear

    or

    1) In Xcode 5 create a new 'Single View Application'. Call it whatever.

    2) In ViewController.m, replace the viewDidLoad method with:

    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        NSMutableParagraphStyle* paragraph = [[NSMutableParagraphStyle alloc] init];
        paragraph.alignment = NSTextAlignmentCenter;
    
        NSAttributedString* attrStr = [[NSAttributedString alloc] initWithString:@"Lorem ipsum dolor sit" attributes:
                                       @{NSUnderlineStyleAttributeName:@(NSUnderlineStyleSingle),
                                         NSParagraphStyleAttributeName:paragraph}];
    
        UILabel* myLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 30, 0, 0)];
        myLabel.backgroundColor = [UIColor greenColor];
        myLabel.attributedText = attrStr;
        [myLabel sizeToFit];
    
        [self.view addSubview:myLabel];
    }
    

    3) Compile and run on your device. Depending on your version of iOS 7, the text will either display, or will not. The UILabel's background color will display in both cases.

    Screenshots

    iPhone 5 with iOS 7.0 (11A465)

    iPhone 5 with iOS 7.0 (11A465)

    iPhone 5 with iOS 7.0 (11A4449d)

    iPhone 5 with iOS 7.0 (11A4449d)

    My Question

    Is anyone able to reproduce this issue on a device?

    • Greg
      Greg over 10 years
      You might want to create a minimal project that illustrates the bug, host it on github or something, and open a bug via developer.apple.com/bug-reporting , pointing at the github project in the bug report.
    • RohinNZ
      RohinNZ over 10 years
      Good idea, I'll upload something to github soon. I've already filed a bug with Apple, but I'm not expecting them to reply anytime soon as they still haven't gotten back to me about another iOS 7 bug I raised with them a while ago.
    • Jeanette Müller
      Jeanette Müller over 10 years
      i found similar results by using backgroundcolor on attributed text and resizing the uilabel while rotating the device. if i dont use backgroundcolor everythign works fine. with backgroundcolor after change the width the text disappears. on rotation back to original the text appears again.
    • EthanB
      EthanB over 10 years
      UILabel is just buggy. NSTextAttachments are also causing lines of text to disappear, but inversely related to the label's height. See this question: stackoverflow.com/q/19253224/1580288
    • matt
      matt over 10 years
      I think my second workaround, where you let the label resize its own height, is a completely reliable solution for this problem.
    • matt
      matt over 10 years
      Try again using iOS 7.1. This bug may now be fixed.
    • Paul Brewczynski
      Paul Brewczynski over 10 years
      Have you reported this bug ? I experience the same problem.
    • Borzh
      Borzh almost 6 years
      If text is very large, then you need to use UITextView with scrolling enabled and editable/selected disabled (instead of UIScrollView & Content view & UILabel).
  • EthanB
    EthanB over 10 years
    Definitely related to height, but in a weird way: stackoverflow.com/q/19253224/1580288
  • kas-kad
    kas-kad almost 10 years
    Got this problem in iOS 8. Simply set numberOfLines to 2 solved the problem. Spent almost 2 hours trying to find if there was my mistake before googled the problem. Thanks.
  • johnyorke
    johnyorke over 9 years
    Same in iOS 8. Spent ages trying to fix that! Thanks a lot.