iOS 7 Simulator Bug - NSAttributedString does not

2019-02-22 04:06发布

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

iOS 7 Device
iOS7 Simulator
iOS 7 Simulator

3条回答
贪生不怕死
2楼-- · 2019-02-22 04:31

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.

查看更多
狗以群分
3楼-- · 2019-02-22 04:38

This Worked for me

//Swift
let underlineAttribute = [NSUnderlineStyleAttributeName:NSNumber(int: 1)];
lblMessage.attributedText = NSAttributedString(string: "Your text", attributes: underlineAttribute)
//Am33t
查看更多
Summer. ? 凉城
4楼-- · 2019-02-22 04:46

Following line causing the issue.

NSParagraphStyleAttributeName:paragraph

查看更多
登录 后发表回答