Ios NSAttributedString:设置FontAttributes不';不要改变字体
我正在尝试更改NSAttribute字符串上的字体 我有一个迭代每个字符的方法,并为该字符创建一个新的NSFontAttribute字典。然而,最后字符串仍然保持不变。为了演示,以下是如何设置字符串:Ios NSAttributedString:设置FontAttributes不';不要改变字体,ios,objective-c,nsstring,nsattributedstring,foundation,Ios,Objective C,Nsstring,Nsattributedstring,Foundation,我正在尝试更改NSAttribute字符串上的字体 我有一个迭代每个字符的方法,并为该字符创建一个新的NSFontAttribute字典。然而,最后字符串仍然保持不变。为了演示,以下是如何设置字符串: UIFontDescriptor *fontDescriptor = [UIFontDescriptor fontDescriptorWithName:@"Avenir-Book" size:14.0f]; NSDictionary *fontAttributes = [fontDescrip
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字典:
Font Attributes: {
NSCTFontSymbolicTrait = 2147483648;
NSFontNameAttribute = "Avenir-Book";
NSFontSizeAttribute = 14;
}
我遍历并修改每个角色的FontAttribute以添加粗体或斜体,如下所示:
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}];
}
但是,NSAttributedString本身仍然完全未更改。它仍然是默认字体。如何使其反映我对其属性所做的更改 以下是解决方案:
1:UIFontDescriptor的文档将键:UIFontDescriptorNameAttribute
定义为一个NSString
实例
2:NSAttributeString的文档将键:NSFontAttributeName
定义为UIFont
实例
因此,从使用以下方法初始化的(UIFontDescriptor*)FontDescriptor WithName:(NSString*)fontName size:(CGFloat)size
从UIFontDescriptor
中获取fontAttributes
字典只会设置键UIFontDescriptorNameAttribute
。但是,如果您希望实际修改NSAttributedString的字体,则需要使用保存到键NSFontAttributeName
的UIFont实例应用属性
这就是困惑的根源。但是,应该注意的是,您实际上可以使用键
NSFontAttributeName
从UIFontDescriptor实例的fontAttributes字典中获取UIFontDescriptorNameAttribute
。这也可能令人困惑 仅供参考-阅读UIFontSymbolicTrait
的文档。请注意,它需要的是NSNumber
值,而不是NSString
。此代码有效。医生的报告不可靠。我尝试使用键“UIFontDescriptorRaitsAttribute”添加它,该键接受NSString,但它引发异常:出于某种原因,此“不正确”键是将TraitAttribute添加到FontAttributes字典的唯一方法。不要问我为什么。但是您应该将值包装在NSNumber
中,而不是NSString
中。例如[fontAttributes setValue:@([fontDescriptor symbolicTraits])forKey:UIFontSymbolicTrait]代码>@maddy你是对的。我把自己和UIFontDescriptorRaitsAttribute搞混了。我已经解决了这个问题,但它仍然对结果没有影响。
Index 1: {
NSCTFontSymbolicTrait = 2147483650;
NSFontNameAttribute = "Avenir-Black";
NSFontSizeAttribute = 14;
}
Index 2: {
NSCTFontSymbolicTrait = 2147483651;
NSFontNameAttribute = "Avenir-BlackOblique";
NSFontSizeAttribute = 14;
}