【问题标题】:UITextView ignoring AttributedText in RetinaUITextView 忽略 Retina 中的 AttributedText
【发布时间】:2013-04-19 12:01:29
【问题描述】:

我现在断断续续地研究一个错误(因为它让我发疯)已经有几个星期了,但我似乎无法深入了解它,所以我正在联系希望有人能帮我解释一下……

我在 XIB 文件中有一个基本的UITextView 设置,并使用包含的字体设置了字体,但不是默认字体(在本例中为“Avenir”),然后我使用了几行使用 Attributed Text 属性更改行高的代码。我还应该提到我正在使用 ARC。

输出在 iPad mini(或模拟器上的普通 iPad)上看起来很完美,但在 iPad 视网膜(或视网膜模拟器)上,输出会被退回到系统默认字体 - Helvetica,尽管它保持了行间距。

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

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

这是我得到的输出:

非视网膜

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

有什么想法吗?

【问题讨论】:

    标签: ios objective-c cocoa uitextview nsattributedstring


    【解决方案1】:

    我遇到了同样的问题。对于非视网膜设备和模拟器,它可以工作,但是当我切换到视网膜设备或模拟器时,字体是标准的 helvetica,与你的输出相同。 对我来说,当我直接在代码中指定字体时(在 Interface Builder 中正确设置),它就起作用了。所以添加这样一行:

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

    【讨论】:

    • 我试过添加这个,字体确实可以自行修复,但我指定的行高现在被忽略了,这一切都变得毫无意义。它们会以某种方式发生冲突吗?
    • 我也只是尝试将这两个属性放入字典并使用 addAttributes:range: 方法将它们添加在一起,但结果相同。如果两者都指定,则忽略行高。
    【解决方案2】:

    你能试着调整你的最小线高吗?

    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.minimumLineHeight = 48.0 * [UIScreen mainScreen] scale];
    

    【讨论】:

    • 这确实将 Retina 上的行高更改为 96,但并没有做太多其他事情。当然,只是增加 Helvetica 文本的间距,但重复的属性仍然存在。
    【解决方案3】:

    对于可能偶然发现此问题的任何其他人,在没有在这里得到我需要的东西之后,我使用我的一张专门的 Apple 支持票与他们的一位工程师就该问题展开讨论。

    他确认这确实是 iOS 中无法修复的错误 - 您可以设置字体,在这种情况下,行高和间距将被忽略,或者在其他情况下,字体将重置为系统默认值。他确实说过,快速测试似乎表明它已在 iOS 7 Beta 2 中得到修复,幸好,但仍然不是很好。

    出于我的目的,我已将 UITextView 替换为 UIWebView,以便我可以使用 CSS 控制内容

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-13
      • 2012-09-17
      • 1970-01-01
      • 2019-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多