Iphone – UILabel with NSAttributedString does work in iOS 7 but doesn’t in iOS 6

ios6ios7iphoneuilabeluistoryboard

I have an UILabel with the following settings set up in my storyboard:

UILabel settings in storyboard

In my code, I set the corresponding text for the label using the UILabel's setText: message. This works pretty fine in iOS 7. iOS 7 view

However, when starting the app on an iOS 6 device or simulator, the UILabel's settings are ignored, completely: iOS 6 view

If I set the UILabel's attributedText property in Code, this somewhat works, but I really don't want to programatically style all the labels I have already set up and working in iOS 7 in my Storyboard.

Any suggestions here?

Thanks

Update:

I found out meanwhile, that the attributedText-settings I set in the Storyboard are not even applied to the UILabel object in iOS 6:

iOS 6:

(lldb) po [cell.eventName.attributedText attributesAtIndex:0 effectiveRange:NULL]
{
    NSColor = "UIDeviceWhiteColorSpace 0 1";
    NSFont = "<UICFFont: 0xb189240> font-family: \".Helvetica NeueUI\"; font-weight: normal; font-style: normal; font-size: 17px";
    NSKern = 0;
    NSLigature = 0;
    NSParagraphStyle = "Alignment 0, LineSpacing 0, ParagraphSpacing 0, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 0/0, LineHeightMultiple 0, LineBreakMode 4, Tabs (\n    28L,\n    56L,\n    84L,\n    112L,\n    140L,\n    168L,\n    196L,\n    224L,\n    252L,\n    280L,\n    308L,\n    336L\n), DefaultTabInterval 0, Blocks (null), Lists (null), BaseWritingDirection -1, HyphenationFactor 0, TighteningFactor 0, HeaderLevel 0";
    NSShadow = "NSShadow {0, -1} color = {(null)}";
}

iOS 7:

(lldb) po [cell.eventName.attributedText attributesAtIndex:0 effectiveRange:NULL]
{
    NSColor = "UIDeviceRGBColorSpace 0 0 0 1";
    NSFont = "<UICTFont: 0xc1c3cc0> font-family: \"HelveticaNeue-Light\"; font-weight: normal; font-style: normal; font-size: 21.00pt";
    NSParagraphStyle = "Alignment 2, LineSpacing 0, ParagraphSpacing 0, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 0/0, LineHeightMultiple 0, LineBreakMode 4, Tabs (\n    28L,\n    56L,\n    84L,\n    112L,\n    140L,\n    168L,\n    196L,\n    224L,\n    252L,\n    280L,\n    308L,\n    336L\n), DefaultTabInterval 0, Blocks (null), Lists (null), BaseWritingDirection -1, HyphenationFactor 0, TighteningFactor 0, HeaderLevel 0";
    NSShadow = "NSShadow {0, -1} color = {(null)}";
}

As you can see, the font I selected in the Storyboard differs from the font the attributedText of the UILabel has assigned in iOS 6, but not in iOS 7.

Update 2:

I created an example application, download here: Example application

With this application, you can perfectly reproduce the issue. Run in Simulator with first iOS 7, then iOS 6. You will see the result as explained here.

Best Answer

You will need to use the attributedText property.

Try this:

NSAttributedString* attrStr = [[NSAttributedString alloc] 
    initWithString:@"Opening Event" 
        attributes:[label.attributedText attributesAtIndex:0 effectiveRange:NULL]];
label.attributedText = attrStr;