【问题标题】:UILabel.attributedText not show up on iPhone 4 + iOS 7.0.3UILabel.attributedText 不显示在 iPhone 4 + iOS 7.0.3
【发布时间】:2015-08-25 04:29:08
【问题描述】:

有一个 iPhone 4 在现场和一个奇怪的问题,UILabel 没有显示任何文本。我在 iPhone 4S + iOS 7 模拟器上测试过,效果很好。

代码:

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];

【问题讨论】:

  • 以上代码在大多数情况下都能正常工作。在带有 NSUnderlineStyleAttributeName 的 iPhone 4 上很奇怪。我尝试删除下线属性,它可以绘制。一旦添加回来,不再工作。所以这是 NSUnderlineStyleAttributeName 问题。而且,您的链接不能解决我的问题。
  • 想通了! God dam* iPhone 4 在 7.0.2 上,只是有问题。

标签: ios


【解决方案1】:

我已经检查过了。它在 iPhone 4 上显示,可能还有其他内容。从设备中清理构建和删除并再次运行

【讨论】:

  • 没有。我尝试删除下线属性,它可以绘制。一旦添加回来,不再工作。所以这是 NSUnderlineStyleAttributeName 的问题。
  • 仅供参考,我的 iPhone 4 中的 iOS 版本是 7.1.2。您可以通过传递静态文本来尝试: NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:@"test"];
  • 想通了! God dam* iPhone 4 在 7.0.2 上,只是有问题。
【解决方案2】:

我已经玩了一段时间的属性文本,并发现了一些新的东西:

似乎在 iOS 7.0.x 上,NSUnderlineStyleAttributeName 不能很好地与颜色或字体等其他属性配合使用,一旦它们捆绑在一起,它就不会显示文本。只有下划线样式实际上可以绘制如下文本:

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

但是一旦你添加了类似的东西

colLabel.backgroundColor = [UIColor greenColor];

colLabel.font = [UIFont boldSystemFontOfSize:12];

它不会显示,除非您进行两项更改:将换行符附加到原始字符串,并将标签的 numberOfLines 设置为 2。

喜欢:

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; // <---

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-11
    • 1970-01-01
    • 2014-12-04
    • 1970-01-01
    • 1970-01-01
    • 2014-04-15
    • 2013-11-22
    • 2013-03-13
    相关资源
    最近更新 更多