从这两个屏幕快照中可以看出,ios 5.1和ios 6之间的CoreText实现似乎有所不同:
iOS 6:
iOS 5:
首先,文字颜色未正确应用。似乎在ios 5.1上,kCTForegroundColorAttributeName
要求您为其提供一个CGColor
,而在ios 6上,向其传递UIColor就足够了。因此,我通过将代码更改为以下方式解决了该问题:
[attributes setObject:(id)[color CGColor]
forKey:(NSString*)kCTForegroundColorAttributeName];
其次,段落间距有些偏离。 “视线”和“相应”之间的距离是11像素对25像素(在屏幕截图中测量)。在这两种情况下,段落间距都设置为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”可以解决间距问题。
最佳答案
Core Text在iOS 6中进行了全面改革。如果您拥有Apple Developer Account,则可以观看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