2014-07-17 102 views
1
屬性

我在我們的應用程序中使用自定義字體系列「阿列爾錯字」用功能支持多國語言文字編輯(UITextView的)和使用 -文本字體回退與自定義字體應用於NSAttributedString

「AllerTypo淡味」純文本(無型面)

「AllerTypo正規」的大膽。

「AllerTypo-LightItalic」 爲斜體。

「AllerTypo斜體」 爲粗斜體。

問題是當我申請在此字體系列不支持文本(字形)的字體,它大概是回退到字體(如「黑體」家族俄文本)重新應用客戶端字型特徵,但不知道我使用「AllerTypo-Regular」版本的事實,因爲它的大膽 -ness。請幫我解決這個問題。

請參見下面的代碼片段是我上面講的。

// Apply current theme on default attributes (like font, text color) 
NSMutableAttributedString *updatedAttribText = [attribText mutableCopy]; 

[updatedAttribText beginEditing]; 
[attribText enumerateAttributesInRange:NSMakeRange(0, attribText.length) 
           options:0 
          usingBlock:^(NSDictionary *attrs, NSRange range, BOOL *stop) { 

           // Check for font attributes 
           UIFont *textRunfont = [attrs objectForKey:NSFontAttributeName]; 
           NSMutableDictionary *updatedAttribs = [attrs mutableCopy]; 

           [updatedAttribs updateAttributesWithTextStyle:textRunfont.fontDescriptor.symbolicTraits action:YES fontFamily:@"Aller Typo"]; 

           // Check for default text color now 
           UIColor *textRunColor = [attrs objectForKey:NSForegroundColorAttributeName]; 

           if ([textRunColor isEqual:[UIColor colorWithRed:0 green:0 blue:0 alpha:1.0]]) { 
            UIColor *defaultTextColor = [[QGThemeHelper currentTheme] uiTextViewTextColor]; 

            [updatedAttribs setValue:defaultTextColor forKey:NSForegroundColorAttributeName]; 
           } 

           [updatedAttribText setAttributes:updatedAttribs range:range]; 
          }]; 

[updatedAttribText endEditing]; 

上面使用的NSMutableDictionary類別方法來更新文本屬性。

- (void)updateAttributesWithTextStyle:(UIFontDescriptorSymbolicTraits)updateStyleTraits action:(BOOL)addAction fontFamily:(NSString *)familyName 
{ 
    UIFont *currentFont = [self objectForKey:NSFontAttributeName]; 
    UIFontDescriptorSymbolicTraits updatedTraits = 0; 

    // Fetch existing traits and then apply the text style 
    if (!addAction) { // Remove 
     updatedTraits = ([currentFont qg_textStyleTraits] & ~updateStyleTraits); 
    } 
    else { // Apply 
     updatedTraits = ([currentFont qg_textStyleTraits] | updateStyleTraits); 
    } 

    // Appropriate font from the family with mentioned traits (bold, italic, bold italic). 
    NSString *updatedFontName = [UIFont qg_fontNameFromFamily:familyName withTextStyleTraits:updatedTraits]; 
    UIFont *updatedFont = [UIFont fontWithName:updatedFontName size:currentFont.pointSize]; 

    [self setObject:updatedFont forKey:NSFontAttributeName]; 
} 

換句話說,有什麼辦法可以與fallback系統溝通我的意圖(字體定製)嗎?斜體功能正常工作,因爲「AllerTypo-Italic」字體帶有該特性,系統在故障預置期間重新應用該特性。

補充說明 - 文本內容源(屬性文本)進行編碼存儲在CoreData HTML標籤被同步回我們的web應用程序也是如此。

回答

2

你試過UIFontDescriptorCascadeListAttribute

爲了達到這個目的,您可能需要使用-[UIFont fontWithDescriptor:size:]創建UIFont,對於描述符,您需要爲要返回的字體設置屬性UIFontDescriptorCascadeListAttribute