gpt4 book ai didi

ios - UITextView 忽略 Retina 中的 AttributedText

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:04:25 27 4
gpt4 key购买 nike

几个星期以来,我一直在断断续续地研究一个错误(因为它让我发疯),但我似乎无法深入了解它,所以我正在伸出援手希望有人能为我阐明这一点......

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

输出在 iPad mini(或模拟器上的普通 iPad)上看起来很完美,但在 iPad retina(或 retina 模拟器)上,输出被打回系统默认字体 - 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;

据我所知,在 Retina 设备上,出于某种原因,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

有什么想法吗?

最佳答案

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

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

关于ios - UITextView 忽略 Retina 中的 AttributedText,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16104164/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com