iOS 7 BUG - NSAttributedString 没有出现 [英] iOS 7 BUG - NSAttributedString does not appear

查看:12
本文介绍了iOS 7 BUG - NSAttributedString 没有出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上周我问了一个关于 NSAttributedString 未显示的模拟器错误的问题:iOS 7 Simulator Bug - NSAttributedString 没有出现

Last week I asked a question about a Simulator bug with NSAttributedString not displaying: iOS 7 Simulator Bug - NSAttributedString does not appear

不幸的是,现在看来这不是模拟器错误,而是 iOS 7 错误.我现在已经在 iPhone 5 设备上重现了这个问题.

Unfortunately it now appears this is not a simulator bug but an iOS 7 bug. I have now reproduced this issue on an iPhone 5 device.

该错误似乎是结合使用 NSUnderlineStyleAttributeName &NSParagraphStyleAttributeName 作为 NSAttributedString 的属性.

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

到目前为止,我只在两台 iOS 7 设备上进行了测试,问题只出现在其中一台上.即使它们都升级到完全相同的版本:

I have only tested on two iOS 7 devices so far, and the issue has only appeared on one of them. Even after they have both been upgraded to the exact same version:

  • 第一台装有 iOS 7.0 (11A465) 的 iPhone 5:不显示文本

  • 1st iPhone 5 with iOS 7.0 (11A465): Text does NOT appear

升级到 7.0.2 (11A501) 后的第一台 iPhone 5:文本不出现

1st iPhone 5 after upgrading to 7.0.2 (11A501): Text does NOT appear

运行 iOS 7.0 (11A4449d) 的第二台 iPhone 5:文本显示正确

2nd iPhone 5 running iOS 7.0 (11A4449d): Text displays correctly

升级到 7.0.2 (11A501) 后的第二部 iPhone 5:文本不出现

2nd iPhone 5 after upgrading to 7.0.2 (11A501): Text does NOT appear

因此,Apple 似乎在 iOS 7.0 (11A4449d) 之后引入了此错误.我已经向他们提交了一个错误,并将在收到任何回复时向您更新.

So it appears Apple introduced this bug after iOS 7.0 (11A4449d). I've filed a bug with them and will update you on any response I get.

重现错误的步骤

如果您运行的是 iOS 7.0.2,那么您应该能够重现此错误.

If you are running iOS 7.0.2 then you should be able to reproduce this bug.

或者在你的设备上下载并运行这个项目 https://github.com/rohinnz/iOS-7-BUG---NSAttributedString-does-not-appear

Either download and run this project on your device https://github.com/rohinnz/iOS-7-BUG---NSAttributedString-does-not-appear

1) 在 Xcode 5 中创建一个新的单视图应用程序".随便叫.

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

2) 在 ViewController.m 中,将 viewDidLoad 方法替换为:

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:@(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 的背景色在这两种情况下都会显示.

3) Compile and run on your device. Depending on your version of iOS 7, the text will either display, or will not. The UILabel's background color will display in both cases.

截图

装有 iOS 7.0 (11A465) 的 iPhone 5

装有 iOS 7.0 (11A4449d) 的 iPhone 5

我的问题

有人能在设备上重现这个问题吗?

Is anyone able to reproduce this issue on a device?

推荐答案

我找到了解决此错误的方法.在您的 github 代码中,要使用变通方法,您可以这样说:

I have found a workaround for this bug. In your github code, to use the workaround, you would say this:

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

myLabel.numberOfLines = 2; // <---

我做了两个更改:我在原始字符串中附加了一个换行符,并将标签的 numberOfLines 设置为 2.

I have made two changes: I've appended a newline character to your original string, and I've set the label's numberOfLines to 2.

解决方法的作用是强制标签的文本靠在标签的顶部.这似乎解决了问题.换句话说,该错误似乎源于标签试图将其文本垂直居中;如果通过调整标签的高度、numberOfLines 和文本末尾多余的换行符,故意使文本对于标签的大小来说太长,则将绘制属性字符串.

What the workaround does is to force the label's text up against the top of the label. This seems to solve the problem. In other words, the bug appears to stem from the label's attempt to vertically center its text; if you deliberately make the text too long for the size of the label by juggling the label's height, numberOfLines, and excess newline characters at the end of the text, the attributed string will be drawn.

编辑我刚刚找到了另一种解决方法:让标签自行调整大小以适应文本.在此处查看我的代码和解释:https://stackoverflow.com/a/19545193/341994 在该代码中,我做来自另一端的同样的事情,就像它一样:我给标签一个固定的宽度约束但一个灵活的高度约束,并通过设置自己的高度,标签将其文本的顶部靠在自身的顶部,因此能够正确显示文本.换句话说,这只是防止标签将其文本垂直居中的另一种方式,这似乎是触发错误的原因.

EDIT I've just found another workaround along the same lines: let the label resize itself to fit the text. See my code and explanation here: https://stackoverflow.com/a/19545193/341994 In that code, I do the same thing from the opposite end, as it were: I give the label a fixed width constraint but a flexible height constraint, and by setting its own height, the label brings the top of its text up against the top of itself, and thus is able to display the text correctly. In other words, this is just another way of preventing the label from centering its text vertically, which is what seems to trigger the bug.

进一步编辑我觉得这个错误可能会在 iOS 7.1 中得到修复.

FURTHER EDIT I have the sense that this bug may get fixed in iOS 7.1.

这篇关于iOS 7 BUG - NSAttributedString 没有出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆