UITextView忽略在Retina屏幕下的AttributedText

3

我一直在处理一个bug,这已经困扰了我几周了,但我似乎总是无法找到它的根源,所以我希望有人能为我解惑...

我在XIB文件中设置了基本的UITextView并使用了包含的字体来设置字体,但不是默认字体(在这种情况下是"Avenir"),然后我使用了几行代码来通过使用属性文本属性更改行高。我还应该提到,我正在使用ARC。

在iPad mini上(或模拟器上的普通iPad)输出看起来非常完美,但在iPad retina(或视网膜模拟器)上,输出被推回到系统默认字体 - Helvetica,尽管保留了行距。

我尝试编程创建UITextView而不是在XIB中创建,仍然没有好运。这是我用于设置属性文本的代码:

NSString *string = theTextView.text;

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.minimumLineHeight = 48.f;

NSMutableAttributedString *aStr = [[NSMutableAttributedString alloc] initWithString:string];
[aStr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0,[string length])];

theTextView.attributedText = aStr;

据我所知,在视网膜设备上,由于某种原因,UITextView 似乎持有一组双重的属性字符串。当我打印时,结果如下:
NSLog(@"Font Attributes: %@", theTextView.attributedText);

这是我得到的输出:
非Retina
Font Attributes: Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda.
{
    NSColor = "UIDeviceRGBColorSpace 0 0 0 1";
    NSFont = "<UICFFont: 0x714c930> font-family: \"Avenir-Roman\"; font-weight: normal; font-style: normal; font-size: 24px";
    NSKern = 0;
    NSParagraphStyle = "Alignment 4, LineSpacing 0, ParagraphSpacing 0, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 48/0, LineHeightMultiple 0, LineBreakMode 0, Tabs (\n), DefaultTabInterval 36, Blocks (null), Lists (null), BaseWritingDirection 0, HyphenationFactor 0, TighteningFactor 0, HeaderLevel 0";
    NSStrokeColor = "UIDeviceRGBColorSpace 0 0 0 1";
    NSStrokeWidth = 0;
}

视网膜屏幕

Font Attributes: Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda.
{
    NSColor = "UIDeviceRGBColorSpace 0 0 0 1";
    NSFont = "<UICFFont: 0x71ed950> font-family: \"Helvetica\"; font-weight: normal; font-style: normal; font-size: 12px";
    NSKern = 0;
    NSParagraphStyle = "Alignment 4, LineSpacing 0, ParagraphSpacing 0, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 48/0, LineHeightMultiple 0, LineBreakMode 0, Tabs (\n), DefaultTabInterval 36, Blocks (null), Lists (null), BaseWritingDirection 0, HyphenationFactor 0, TighteningFactor 0, HeaderLevel 0";
    NSStrokeColor = "UIDeviceRGBColorSpace 0 0 0 1";
    NSStrokeWidth = 0;
}
{
    NSColor = "UIDeviceRGBColorSpace 0 0 0 1";
    NSFont = "<UICFFont: 0x71e67d0> font-family: \"Avenir-Roman\"; font-weight: normal; font-style: normal; font-size: 24px";
    NSKern = 0;
    NSParagraphStyle = "Alignment 4, LineSpacing 0, ParagraphSpacing 0, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 48/0, LineHeightMultiple 0, LineBreakMode 0, Tabs (\n), DefaultTabInterval 36, Blocks (null), Lists (null), BaseWritingDirection 0, HyphenationFactor 0, TighteningFactor 0, HeaderLevel 0";
    NSStrokeColor = "UIDeviceRGBColorSpace 0 0 0 1";
    NSStrokeWidth = 0;
}

我已在GitHub上创建了一个简单的示例项目来展示问题 - https://github.com/mpatteson/UITextViewBug 有什么想法?
3个回答

2

我遇到了同样的问题。对于非Retina设备和模拟器,它可以正常工作,但是当我切换到Retina设备或模拟器时,字体就变成了标准的Helvetica,并且出现了与您相同的输出。

对于我来说,当我直接在代码中指定字体时(在界面构建器中已正确设置),它可以正常工作。因此,请添加以下行:

UIFont *font = [UIFont fontWithName:@"Avenir-Roman" size:24.0];
[aStr addAttribute:NSFontAttributeName value:font range:NSMakeRange(0,[string length])];

我尝试添加了这个字体,它确实修复了问题,但是我的指定行高现在被忽略了,这让它变得有点毫无意义。它们是否会发生冲突? - MPatteson
我也尝试将两个属性放入字典中,并使用addAttributes:range:方法将它们相加,但结果相同。如果两者都指定,则忽略行高。 - MPatteson

1
你可以尝试调整比例的最小行高吗?
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.minimumLineHeight = 48.0 * [UIScreen mainScreen] scale];

确实,这会将Retina上的行高更改为96,但并没有做太多其他事情。当然只是将Helvetica文本间隔更多,但重复的属性仍然存在。 - MPatteson

0

对于任何其他可能偶然发现此问题的人,在这里没有得到我所需的东西后,我使用了我的一个专用Apple支持票来与他们的工程师之一就此问题进行讨论。

他确认这确实是iOS中无法纠正的错误 - 您可以设置字体,此时行高和间距将被忽略;或者反过来,此时字体将重置为系统默认值。他说,快速测试似乎显示它已在iOS 7 Beta 2中修复,但仍不理想。

出于我的目的,我已使用UIWebView替换了UITextView,以便我可以通过CSS控制内容。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接