Ios 在NSAttributedString上应用自定义字体属性的文本上的字体回退
我在我们的应用程序中使用自定义字体系列“Aller-Typo”,它具有支持多语言文本编辑(UITextView)的功能,并使用- “AllerTypo Light”用于纯文本(无字体面) “AllerTypo常规”表示粗体。 “AllerTypo LightItalic”表示斜体 “AllerTypo Italic”表示粗体斜体。 问题是当我在该字体系列不支持的文本(字形)上应用字体时,它可能会退回到一种字体(例如俄语文本的“Helvetica”系列)上,重新应用客户端字体特征,但没有意识到我使用“AllerTypo常规”版本的粗体-ness。请帮我解决这个问题 请参阅下面的代码片段,了解我在上面提到的内容Ios 在NSAttributedString上应用自定义字体属性的文本上的字体回退,ios,multilingual,nsattributedstring,uifont,typeface,Ios,Multilingual,Nsattributedstring,Uifont,Typeface,我在我们的应用程序中使用自定义字体系列“Aller-Typo”,它具有支持多语言文本编辑(UITextView)的功能,并使用- “AllerTypo Light”用于纯文本(无字体面) “AllerTypo常规”表示粗体。 “AllerTypo LightItalic”表示斜体 “AllerTypo Italic”表示粗体斜体。 问题是当我在该字体系列不支持的文本(字形)上应用字体时,它可能会退回到一种字体(例如俄语文本的“Helvetica”系列)上,重新应用客户端字体特征,但没有意识到我使
// 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标记,该标记也同步回我们的web应用程序。您是否尝试过
UIFontDescriptorCascadeListAttribute
为了实现这一点,您可能需要使用-[UIFont-fontWithDescriptor:size:]
创建UIFont
,并且对于描述符,您需要使用要返回的字体设置属性uifontsdescriptorcascadelistAttribute