【问题标题】:Font fallback on text applied with custom font attributes on NSAttributedString使用 NSAttributedString 上的自定义字体属性应用文本的字体回退
【发布时间】:2014-09-08 23:16:19
【问题描述】:

我在我们的应用程序中使用自定义字体系列“Aller Typo”,它具有支持多语言文本编辑 (UITextView) 的功能并使用 -

纯文本的“AllerTypo-Light”(无字体)

“AllerTypo-Regular”表示粗体。

“AllerTypo-LightItalic”为斜体。

“AllerTypo-Italic”表示粗斜体。

问题是当我将字体应用于此字体系列不支持的文本(字形)时,它可能会回退到重新应用客户端字体的字体(例如,俄罗斯文本的“Helvetica”系列)面对特征,但不知道我使用“AllerTypo-Regular”版本是因为它的bold-ness。请帮我解决这个问题。

请参阅下面的代码 sn-p 了解我上面谈到的内容。

// 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];
}

换句话说,有什么方法可以与备用系统沟通我的意图(字体定制)?斜体功能运行良好,因为“AllerTypo-Italic”字体带有该特性,系统在回退期间重新应用该特性。

附加说明 - 文本内容源(属性文本)是存储在 CoreData 中的编码 HTML 标签,它也会同步回我们的网络应用程序。

【问题讨论】:

    标签: ios multilingual nsattributedstring uifont typeface


    【解决方案1】:

    你试过UIFontDescriptorCascadeListAttribute吗。

    为了实现这一点,您可能需要使用-[UIFont fontWithDescriptor:size:] 创建UIFont,并且对于描述符,您需要使用要回退到的字体设置属性UIFontDescriptorCascadeListAttribute

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-05-05
      • 2013-11-15
      • 2016-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-07
      相关资源
      最近更新 更多