ios 6和ios 5.1之间的核心文本差异?

时间:2013-03-28 09:46:24

标签: ios ios5 ios6 core-text

看来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];

其次,段落间距有点偏。 "视线"之间的距离和"根据"是11px vs 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 个答案:

答案 0 :(得分:7)

Core Text在iOS 6中进行了大修。如果您拥有Apple开发者帐户,可以通过观看免费提供的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