0

Got a iPhone 4 in the field and a strange problem, the UILabel does not show up any text. I tested it on iPhone 4S + iOS 7 simulator, it works fine.

Code:

NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:[colLabel.text copy]];
[attributeString addAttribute:NSUnderlineStyleAttributeName
                        value:[NSNumber numberWithInt:1]
                        range:(NSRange){0,[attributeString length]}];
colLabel.text = nil;
colLabel.attributedText = [attributeString copy];
colLabel.font = [UIFont boldSystemFontOfSize:12];
Wingzero
  • 9,644
  • 10
  • 39
  • 80
  • Please refer below links:- http://stackoverflow.com/questions/19127828/ios-7-bug-nsattributedstring-does-not-appear http://stackoverflow.com/questions/3482346/how-do-you-use-nsattributedstring – poojathorat Aug 25 '15 at 05:10
  • Above code works fine in most cases. Just weird on iPhone 4 with NSUnderlineStyleAttributeName. I tried deleting the under line attribute, it can draw. Once add back, not working again. So it's the NSUnderlineStyleAttributeName problem. And, your links does not solve mine. – Wingzero Aug 25 '15 at 05:15
  • Figured out! The god dam* iPhone 4 is on 7.0.2 and just buggy as it is. – Wingzero Aug 25 '15 at 05:39

2 Answers2

0

I have checked. Its showing on iPhone 4, there may be something else. Clean build and delete from device and run again

Bhanupriya
  • 1,202
  • 1
  • 9
  • 20
  • No. I tried deleting the under line attribute, it can draw. Once add back, not working again. So it's the NSUnderlineStyleAttributeName problem. – Wingzero Aug 25 '15 at 05:06
  • FYI, iOS version is 7.1.2 in my iPhone 4. You can try by passing static text: NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:@"test"]; – Bhanupriya Aug 25 '15 at 05:16
  • Figured out! The god dam* iPhone 4 is on 7.0.2 and just buggy as it is. – Wingzero Aug 25 '15 at 05:39
0

I have been played with the attributed text for a while, and find out something new:

It seems like on iOS 7.0.x, NSUnderlineStyleAttributeName does not play well with other attributes like color or font, once they are bundled together, it just will not show up the text. Only having underline style actually could draw the text like below:

NSAttributedString* attrStr = [[NSAttributedString alloc] initWithString:colLabel.text
                                                attributes:@{NSUnderlineStyleAttributeName:@(NSUnderlineStyleSingle)}];
colLabel.attributedText = attrStr;

But once you add something like

colLabel.backgroundColor = [UIColor greenColor];

or

colLabel.font = [UIFont boldSystemFontOfSize:12];

It just won't show up, unless you make two changes: appended a newline character to your original string, and set the label's numberOfLines to 2.

like:

NSAttributedString* attrStr = 
    [[NSAttributedString alloc] initWithString:@"TEST\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; // <---
Wingzero
  • 9,644
  • 10
  • 39
  • 80