NSAttributedString:设置 FontAttributes 不会改变字体 [英] NSAttributedString: Setting FontAttributes doesn't change font

查看:96
本文介绍了NSAttributedString:设置 FontAttributes 不会改变字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更改 NSAttributedString 上的字体.

I am trying to change the font on an NSAttributedString.

我有一个方法可以遍历每个字符,并为该字符创建一个新的 NSFontAttribute 字典.然而,最后,字符串保持不变.为了演示,这里是我如何设置字符串:

I have a method which iterates over each character, and creates a new NSFontAttribute dictionary for that character. In the end, however, the string remains un-changed. To demonstrate, here is how I setup the string:

UIFontDescriptor *fontDescriptor = [UIFontDescriptor fontDescriptorWithName:@"Avenir-Book" size:14.0f];

 NSDictionary *fontAttributes = [fontDescriptor fontAttributes];

 [fontAttributes setValue:[NSString stringWithFormat:@"%u",[fontDescriptor symbolicTraits]] forKey:UIFontSymbolicTrait];

 [mutableAttributedString setAttributes:fontAttributes range:(NSRange){0,length}];

这会为整个字符串生成以下 NSFontAttribute 字典:

This produces the following NSFontAttribute dictionary for the entire string:

Font Attributes: {
    NSCTFontSymbolicTrait = 2147483648;
    NSFontNameAttribute = "Avenir-Book";
    NSFontSizeAttribute = 14;
}

我遍历并修改每个字符的 FontAttribute 以添加粗体或斜体,如下所示:

I go through and modify each character's FontAttribute to add bolding or italics, as follows:

for (int i = (int)range.location; i < (range.location + range.length); i++){
    /* Extract Font Attributes */
    NSDictionary *extractedAttributes = [[mutableAttributedString attributesAtIndex:i effectiveRange:NULL]mutableCopy];

    /* Determine New Trait */
    uint newTrait = ((uint)[[extractedAttributes valueForKey:UIFontSymbolicTrait]longLongValue] | [self symbolicTraitForMarkdownType:markdown]); // (markDown  is a mask)

    /* Set New Trait */
    [extractedAttributes setValue:[NSString stringWithFormat:@"%u",newTrait] forKey:UIFontSymbolicTrait];

    /* Create New Font Descriptor */
    UIFontDescriptor *newDescriptor = [UIFontDescriptor fontDescriptorWithFontAttributes:extractedAttributes];
    newDescriptor = [newDescriptor fontDescriptorWithSymbolicTraits:newTrait];

    /* Apply Font Descriptor */
    [mutableAttributedString setAttributes:[newDescriptor fontAttributes] range:(NSRange){i,1}];
}

这会产生许多不同的 FontAttributes:

This produces many different FontAttributes:

Index 1: {
    NSCTFontSymbolicTrait = 2147483650;
    NSFontNameAttribute = "Avenir-Black";
    NSFontSizeAttribute = 14;
}
Index 2: {
    NSCTFontSymbolicTrait = 2147483651;
    NSFontNameAttribute = "Avenir-BlackOblique";
    NSFontSizeAttribute = 14;
}

然而,NSAttributedString 本身保持完全不变.它仍然是默认字体.我怎样才能让它反映我对其属性所做的更改?

However, the NSAttributedString itself remains completely un-changed. It is still the default Font. How can I get it to reflect the changes I am making to its attributes?

推荐答案

解决方案如下:

1:UIFontDescriptor 的文档定义了键:UIFontDescriptorNameAttribute 作为一个 NSString 实例,它是.

1: The documentation for UIFontDescriptor defines the key: UIFontDescriptorNameAttribute as an NSString instance, which it is.

2: NSAttributedString 的文档定义了键:NSFontAttributeName 作为 UIFont 实例.

2: The documentation for NSAttributedString defines the key: NSFontAttributeName as a UIFont instance.

因此从使用以下方法初始化的 UIFontDescriptor 获取 fontAttributes 字典:(UIFontDescriptor *)fontDescriptorWithName:(NSString *)fontName size:(CGFloat)size 只会设置键UIFontDescriptorNameAttributeUIFontDescriptorSizeAttribute.但是,如果您希望实际修改 NSAttributedString 的字体,则需要使用保存到键 NSFontAttributeName 的 UIFont 实例来应用属性.

So obtaining the fontAttributes dictionary from a UIFontDescriptor initialized with a the method: (UIFontDescriptor *)fontDescriptorWithName:(NSString *)fontName size:(CGFloat)size will only set the keys UIFontDescriptorNameAttribute and UIFontDescriptorSizeAttribute. If you wish to actually modify the font of an NSAttributedString however, you need to apply attributes with a UIFont instance saved to the key NSFontAttributeName.

这就是混乱的来源.但是,应该注意的是,您实际上可以使用键 NSFontAttributeName 从 UIFontDescriptor 实例的 fontAttributes 字典中获取 UIFontDescriptorNameAttribute.这也可能令人困惑.

This is where the confusion comes from. However, it should be noted that you can actually obtain UIFontDescriptorNameAttribute from the fontAttributes dictionary of a UIFontDescriptor instance with the key NSFontAttributeName. This may also be confusing.

这篇关于NSAttributedString:设置 FontAttributes 不会改变字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆