UITextView忽略Retina中的AttributedText

时间:2013-04-19 12:01:29

标签: ios objective-c cocoa uitextview nsattributedstring

我现在一直在研究一个错误(因为它让我疯狂)已经持续了几个星期了,而我似乎无法深入了解它,所以我伸手去拿希望有人能为我揭开这一点......

我在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

有什么想法吗?

3 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:1)

您可以尝试调整刻度的最小线高吗?

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

答案 2 :(得分:0)

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

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

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