带有NSAttributedString的UILabel可以在iOS 7中运行,但在iOS 6中不可用

时间:2013-12-12 14:06:18

标签: iphone ios6 ios7 uilabel uistoryboard

我有一个UILabel,我的故事板中设置了以下设置:

UILabel settings in storyboard

在我的代码中,我使用UILabel的setText:消息为标签设置了相应的文本。这在iOS 7中非常正常。iOS 7 view

但是,在iOS 6设备或模拟器上启动应用程序时,UILabel的设置将被完全忽略:iOS 6 view

如果我在Code中设置了UILabel的attributedText属性,这有点起作用,但我真的不想以编程方式设置我已在故事板中设置并在iOS 7中工作的所有标签。

这里有什么建议吗?

由于

更新

我同时发现,我在Storyboard中设置的attributedText-settings甚至没有应用到iOS 6中的UILabel对象:

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)}";
}

如您所见,我在故事板中选择的字体与UILabel的attributesText在iOS 6中分配的字体不同,但在iOS 7中没有。

更新2:

我创建了一个示例应用程序,请在此处下载:Example application

使用此应用程序,您可以完美地重现该问题。在第一个iOS 7,然后是iOS 6的Simulator中运行。您将看到此处所述的结果。

1 个答案:

答案 0 :(得分:5)

您需要使用attributedText属性。

试试这个:

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