iOS 7 Simulator Bug - NSAttributedString does not appear

ぐ巨炮叔叔 提交于 2019-12-19 19:04:13

问题


UPDATE: I have just encountered this issue on an actual iPhone 5 running iOS 7. Will provide more information soon.

I think I have found a bug in the iOS 7 Simulator where a NSAttributedString does not appear. It would be great if someone else could test this to confirm it is a bug, then I will file a bug with Apple.

The problem appears to be the combination of using NSUnderlineStyleAttributeName and NSParagraphStyleAttributeName for NSAttributedString.

Here are the steps to reproduce:

1) In Xcode 5 create a new 'Single View Application'. Call it whatever.

2) In ViewController.m, replace the viewDidLoad method with:

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSMutableParagraphStyle* paragraph = [[NSMutableParagraphStyle alloc] init];
    paragraph.alignment = NSTextAlignmentCenter;

    NSAttributedString* attrStr = [[NSAttributedString alloc] initWithString:@"Lorem ipsum dolor sit" attributes:
                                   @{NSUnderlineStyleAttributeName:@1,
                                     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) Run on an iOS 7 device and then run it again in the iOS 7 simulator.

4) Lastly, set the deployment target to iOS 6 and run it on the iOS 6 simulator.

The results should be the following

  • iOS 7 Device: Displays correctly
  • iOS 7 Simulator: Only displays label background
  • iOS 6 Simulator: Displays correctly

Screenshots:

iOS7 Device


iOS7 Simulator


回答1:


It appears this is not a Simulator bug, but an iOS 7 bug as I have been able to reproduce it on a device now. I have created a new question for it here: iOS 7 BUG - NSAttributedString does not appear

The bug appears to be the combination of using NSUnderlineStyleAttributeName & NSParagraphStyleAttributeName as attributes for a NSAttributedString.




回答2:


Following line causing the issue.

NSParagraphStyleAttributeName:paragraph




回答3:


This Worked for me

//Swift
let underlineAttribute = [NSUnderlineStyleAttributeName:NSNumber(int: 1)];
lblMessage.attributedText = NSAttributedString(string: "Your text", attributes: underlineAttribute)
//Am33t


来源:https://stackoverflow.com/questions/18994479/ios-7-simulator-bug-nsattributedstring-does-not-appear

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!