【问题标题】:Differences in Core-Text between ios 6 and ios 5.1?ios 6 和 ios 5.1 之间的核心文本差异?
【发布时间】:2013-03-18 16:46:26
【问题描述】:

从这两张截图中可以看出,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];

其次,段落间距有点偏离。 “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" 解决了间距问题。

【问题讨论】:

    标签: ios ios5 ios6 core-text


    【解决方案1】:

    Core Text 在 iOS 6 中进行了大修。如果您拥有 Apple 开发者帐户,您可以通过观看 WWDC 2012 视频来查看所有更改。

    所以现在在 iOS 6 中,您不能使用任何低级核心文本属性,例如 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

    【讨论】:

    • 非常有用的信息,+1,即使它没有回答问题。
    • 您的问题是如何修复您的代码,以及是否有其他问题会导致您的文本以不同方式显示。那些其他的东西是新的属性,就是这样。 iOS 5.1 和 iOS 6 之间的 Core Text 还存在其他差异,但它们只是新添加的 API。你觉得我遗漏了什么?
    • NSParagraphStyle 和 kCTParagraphStyleAttributeName 在 ios 6 上产生了相同的结果,但当然,你不能在 ios 5 上使用 NSParagraphStyle。我在编辑我的问题时指出了问题的真正原因(其中有点傻——我在 unix/windows 之间混淆了我的结束线),我还奖励了你,因为你的答案很有用,但我没有接受答案,因为问题是由不相关的东西引起的.. .
    • 哦,好的。无论如何感谢您的赏金。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-02
    • 1970-01-01
    • 2015-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多