ios – Unbolding UIFontDescriptor

前端之家收集整理的这篇文章主要介绍了ios – Unbolding UIFontDescriptor前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在应用程序中使用动态类型,并且有我想要更改字体外观的场景,例如使其变为斜体或取消键入.添加样式很容易:
UIFontDescriptor *descriptor = [[UIFontDescriptor preferredFontDescriptorWithTextStyle:UIFontTextStyleHeadline]
                                fontDescriptorWithSymbolicTraits:UIFontDescriptorTraitItalic];
UIFont *font = [UIFont fontWithDescriptor:descriptor size:descriptor.pointSize];

然而,没有明确的移除风格的机制.我可以尝试调整属性,但它们看起来更令人生畏,完全没有文档的API:

06001

我还缺少另一条大道吗?我可以使用[UIFont systemFontWithSize:descriptor.pointSize],但我不想丢失动态类型提供的任何绘图规则.

解决方法

fontDescriptorWithSymbolicTraits:方法实际上能够做你想要的,除了内置语义文本样式中字体特征支持的一些边缘情况.这里的关键概念是该方法用新特征替换先前描述符上的所有符号特征. The documentation对此只是一点意外,只是说新特性“优先于旧”.

按位运算用于添加删除特定的特征,但在使用preferredFontDescriptorWithTextStyle:生成的描述符时,需要特别小心.并非所有字体都支持所有特征.例如,标题字体根据用户的首选内容大小进行加权,即使您可以删除其粗体特征的描述符,匹配的UIFont也将为粗体.不幸的是,这在任何地方都没有记载,因此任何其他细微差别的发现都留给读者练习.

以下示例说明了这些问题:

// Start with a system font,in this case the headline font
// bold: YES italic: NO
UIFontDescriptor * originalDescriptor = [UIFontDescriptor preferredFontDescriptorWithTextStyle:UIFontTextStyleHeadline];
NSLog(@"originalDescriptor bold: %d italic: %d",isBold(originalDescriptor),isItalic(originalDescriptor));

// Try to set the italic trait. This may not be what you expected; the 
// italic trait is not added. On a normal UIFontDescriptor the italic
// trait would have been set and the bold trait unset.
// Ultimately it seems that there is no variant of the headline font that
// is italic but not bold.
// bold: YES italic: NO
UIFontDescriptor * italicDescriptor = [originalDescriptor fontDescriptorWithSymbolicTraits:UIFontDescriptorTraitItalic];
NSLog(@"italicDescriptor bold: %d italic: %d",isBold(italicDescriptor),isItalic(italicDescriptor));

// The correct way to make this font descriptor italic (and coincidentally
// the safe way to make any other descriptor italic without discarding its
// other traits) would be as follows:
// bold: YES italic: YES
UIFontDescriptor * boldItalicDescriptor = [originalDescriptor fontDescriptorWithSymbolicTraits:(originalDescriptor.symbolicTraits | UIFontDescriptorTraitItalic)];
NSLog(@"boldItalicDescriptor bold: %d italic: %d",isBold(boldItalicDescriptor),isItalic(boldItalicDescriptor));

// Your intention was to remove bold without affecting any other traits,which
// is also easy to do with bitwise logic.
// Using the originalDescriptor,remove bold by negating it then applying
// a logical AND to filter it out of the existing traits.
// bold: NO  italic: NO
UIFontDescriptor * nonBoldDescriptor = [originalDescriptor fontDescriptorWithSymbolicTraits:(originalDescriptor.symbolicTraits & ~UIFontDescriptorTraitBold)];
NSLog(@"nonBoldDescriptor bold: %d italic: %d",isBold(nonBoldDescriptor),isItalic(nonBoldDescriptor));

// Seems like it worked,EXCEPT there is no font that matches. Turns out
// there is no regular weight alternative for the headline style font.
// To confirm,test with UIFontDescriptorTraitsAttribute as the mandatory
// key and you'll get back a nil descriptor.
// bold: YES italic: NO
nonBoldDescriptor = [nonBoldDescriptor matchingFontDescriptorsWithMandatoryKeys:nil].firstObject;
NSLog(@"nonBoldDescriptor bold: %d italic: %d",isItalic(nonBoldDescriptor));

仅供参考,为简洁起见,上面使用的isBold和isItalic函数可以实现如下:

BOOL isBold(UIFontDescriptor * fontDescriptor)
 {
    return (fontDescriptor.symbolicTraits & UIFontDescriptorTraitBold) != 0;
 }

 BOOL isItalic(UIFontDescriptor * fontDescriptor)
 {
    return (fontDescriptor.symbolicTraits & UIFontDescriptorTraitItalic) != 0;
 }
原文链接:https://www.f2er.com/iOS/335086.html

猜你在找的iOS相关文章