2013-03-28 33 views
3

它似乎有在IOS 5.1和iOS 6的CoreText實現一定的差異,你可以從這兩個截圖看到:ios 6與ios 5.1之間的核心文本差異?

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:在控制檯的paragraphStyleRef

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); 
} 

說明:

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」解決了間距問題。

回答

7

核心文本在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

+0

非常有用的信息,+1即使它沒有回答這個問題。 – 2013-04-01 08:27:29

+0

你的問題是你如何修復你的代碼,並且如果有其他的事情會導致你的文本以不同的方式顯示。其他的東西就是新的屬性,就是這樣。核心文本在iOS 5.1到iOS 6之間還有其他差異,但它們只是新添加的API。你覺得我遺漏了什麼? – 2013-04-01 09:33:22

+0

NSParagraphStyle和kCTParagraphStyleAttributeName在ios 6上產生了相同的結果,但是當然,您不能在ios 5上使用NSParagraphStyle。我指出了在編輯我的問題時實際上導致了問題的原因(這真是愚蠢的tbh - 我在unix/windows之間混淆了我的endlines),而且我也授予您賞金,因爲您的答案很有用,但我沒有接受答案,因爲問題是由無關的事情造成的... – 2013-04-01 10:10:46