从这两个屏幕截图中可以看出,ios 5.1 和 ios 6 之间的 CoreText 实现似乎存在一些差异:
IOS 6:
IOS 5:
首先,文本颜色未正确应用。似乎在 ios 5.1 上kCTForegroundColorAttributeName
要求你给它 a CGColor
,而在 ios 6 上,传递它一个 UIColor 就足够了。所以我通过将代码更改为:
[attributes setObject:(id)[color CGColor]
forKey:(NSString*)kCTForegroundColorAttributeName];
其次,段落间距有点偏离。“sight”和“According”之间的距离是 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" 解决了间距问题。