Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios NSAttributedString:设置FontAttributes不';不要改变字体_Ios_Objective C_Nsstring_Nsattributedstring_Foundation - Fatal编程技术网

Ios NSAttributedString:设置FontAttributes不';不要改变字体

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

我正在尝试更改NSAttribute字符串上的字体

我有一个迭代每个字符的方法,并为该字符创建一个新的NSFontAttribute字典。然而,最后字符串仍然保持不变。为了演示,以下是如何设置字符串:

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