上周我提出了一个有关模拟器中NSAttributedString无法显示的问题:iOS 7 Simulator Bug - NSAttributedString does not appear
不幸的是,现在看来这不是模拟器的问题,而是iOS 7的问题。我已经在iPhone 5设备上重现了此问题。
该bug似乎是使用NSUnderlineStyleAttributeName和NSParagraphStyleAttributeName作为NSAttributedString的属性的组合造成的。
我目前只在两个iOS 7设备上测试过,并且该问题只在其中一个设备上出现。即使它们都升级到完全相同的版本:
第一个安装iOS 7.0 (11A465)的iPhone 5:文字不显示
第一个在升级到7.0.2(11A501)之后的iPhone 5:文字不显示
第二个运行iOS 7.0 (11A4449d)的iPhone 5:文本正确显示
第二个在升级到7.0.2(11A501)之后的iPhone 5:文字不显示
因此,看来苹果在iOS 7.0(11A4449d)之后引入了这个bug。我已向他们报告了这个问题,并将在得到任何反应时向您更新。
重现bug的步骤
如果您正在运行iOS 7.0.2,则应该能够重现此错误。
要么下载并在您的设备上运行此项目https://github.com/rohinnz/iOS-7-BUG---NSAttributedString-does-not-appear
要么
1)在Xcode 5中创建一个新的'Single View Application'。随便起名字。
2)在ViewController.m中,用以下代码替换viewDidLoad方法:
- (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)编译并在您的设备上运行。根据您的iOS 7版本,文本将显示或不显示。无论哪种情况,UILabel的背景颜色都将显示。
屏幕截图
iOS 7.0(11A465)的iPhone 5
iOS 7.0(11A4449d)的iPhone 5
我的问题
有人能在设备上再现此问题吗?
UILabel
存在一些bug。NSTextAttachments
也会导致文本行消失,但与标签的高度成反比关系。请参考此问题:https://dev59.com/cmIk5IYBdhLWcg3wdd_Y - EthanB