iOS 6和iOS 5.1之间的Core-Text差异是什么?

3

看起来在iOS 5.1和iOS 6之间的CoreText实现中存在一些差异,您可以从以下两个屏幕截图中看到:

iOS 6: enter image description here

iOS 5: enter image description here

首先,文本颜色没有正确应用。似乎在iOS 5.1上,kCTForegroundColorAttributeName需要使用CGColor,而在iOS 6上,传递一个UIColor就足够了。所以我通过更改我的代码来解决了这个问题:

[attributes setObject:(id)[color CGColor] 
               forKey:(NSString*)kCTForegroundColorAttributeName];

其次,段落间距有点问题。在“sight”和“According”之间的距离为11px,而不是25px(在截图中测量)。在这两种情况下,段落间距都设置为5:

NSMutableData *styleSettingsArray = [NSMutableData data];
CGFloat spaceBefore,spaceAfter;
...
CTParagraphStyleSetting styleSettingB = {kCTParagraphStyleSpecifierParagraphSpacingBefore   ,sizeof(CGFloat),&spaceBefore};
CTParagraphStyleSetting styleSettingA = {kCTParagraphStyleSpecifierParagraphSpacing         ,sizeof(CGFloat),&spaceAfter};
[styleSettingsArray appendBytes:&styleSettingB length:sizeof(styleSettingB)];
[styleSettingsArray appendBytes:&styleSettingA length:sizeof(styleSettingA)];
...
if(styleSettingsArray.length > 0)
{
    CTParagraphStyleRef paragraphStyleRef = CTParagraphStyleCreate([styleSettingsArray bytes], [styleSettingsArray length] / sizeof(CTParagraphStyleSetting));
    [dictionary setObject:(__bridge id)(paragraphStyleRef) forKey:(NSString*)kCTParagraphStyleAttributeName];
    CFRelease(paragraphStyleRef);
}

控制台中paragraphStyleRef的描述:

iOS 6:
CTParagraphStyle:
base writing direction = -1, alignment = 3, line break mode = 0, default tab interval = 0
first line head indent = 0, head indent = 0, tail indent = 0
line height multiple = 0, maximum line height = 0, minimum line height = 0
line spacing adjustment = 0, paragraph spacing = 5, paragraph spacing before = 5


iOS 5:

CTParagraphStyle:
writing direction = -1, alignment = 3, line break mode = 0, default tab interval = 0
first line head indent = 0, head indent = 0, tail indent = 0
line height multiple = 0, maximum line height = 0, minimum line height = 0
line spacing adjustment = 0, paragraph spacing = 5, paragraph spacing before = 5

这两段文字在我看来是一样的,所以我不知道问题出在哪里。除了段落间距之外,它们是完全相同的。

那么我该如何修复呢?还有其他什么可能导致文本显示不同的问题需要我注意吗?

编辑:经过一些调查,发现段落样式的差异实际上是由于我的换行符引起的,即 "\r\n"。将其更改为"\n"解决了间距问题。

1个回答

7

iOS 6对Core Text进行了重大改进。如果您有Apple Developer帐户,可以通过观看免费的WWDC 2012视频查看所有更改内容。

因此,在iOS 6中,您不能使用任何低级别的Core Text属性,例如kCTForegroundColorAttributeName或kCTParagraphStyleAttributeName。

相反,您需要使用新的一组高级属性,如NSForegroundColorAttributeName和NSParagraphStyle。

因此,您的代码将更改为:

/*Note that you have use the Foundation class
  for the attribute value instead of it's Core-Foundation counterpart.*/

[attributes setObject:color 
           forKey:NSForegroundColorAttributeName];

CGFloat spaceBefore, spaceAfter;

NSMutableParagraphStyle *mutableParagraphStyle = [NSMutableParagraphStyle defaultParagraphStyle];
mutableParagraphStyle.paragraphSpacing = spaceAfter;
mutableParagraphStyle.paragraphSpacingBefore = spaceBefore;

[attributes setObject:mutableParagraphStyle 
           forKey:NSParagraphStyleAttributeName];

您可以在此处查找有关所有新属性的文档: http://developer.apple.com/library/ios/#documentation/uikit/reference/NSAttributedString_UIKit_Additions/Reference/Reference.html

非常有用的信息,即使它没有回答问题也值得点赞。 - Lescai Ionel
你的问题是如何修复你的代码,以及是否有其他原因导致文本显示不同。那些其他原因就是新属性,仅此而已。在iOS 5.1到iOS 6之间,Core Text还有其他差异,但它们只是新增的API。你觉得我漏掉了什么? - Saurav Sachidanand
无论是NSParagraphStyle还是kCTParagraphStyleAttributeName,在iOS 6上都产生了相同的结果,但是在iOS 5上不能使用NSParagraphStyle。我在编辑我的问题时指出了实际引起问题的原因(有点傻,因为我混淆了Unix / Windows之间的换行符),并且我也授予了您赏金,因为您的答案很有用,但是我没有接受答案,因为问题是由其他原因引起的... - Lescai Ionel
好的,无论如何感谢您的慷慨奖励。 - Saurav Sachidanand

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