Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/110.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中更改属性字符串的字体?_Ios_Objective C_Nsattributedstring - Fatal编程技术网

如何在iOS中更改属性字符串的字体?

如何在iOS中更改属性字符串的字体?,ios,objective-c,nsattributedstring,Ios,Objective C,Nsattributedstring,我在标签上添加了属性字符串。我显示的html文本工作正常,但默认情况下它总是显示Times new roman family。请告诉我如何更改文本族 NSAttributedString *attributedString = [[NSAttributedString alloc] initWithData:[inst.desc dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribut

我在标签上添加了属性字符串。我显示的html文本工作正常,但默认情况下它总是显示Times new roman family。请告诉我如何更改文本族

NSAttributedString *attributedString = [[NSAttributedString alloc] initWithData:[inst.desc dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
你可以这样做:

NSDictionary *attrDict = @{
    NSFontAttributeName : [UIFont fontWithName:Arial size:16.0],
    NSForegroundColorAttributeName : [UIColor redColor]
};
 NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:@"string" attributes:attrDict];
试试这个:

NSAttributedString *attributedString = [[NSAttributedString alloc] initWithData:[inst.desc dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
NSMutableAttributedString *newString = [[NSMutableAttributedString alloc] initWithAttributedString:attributedString];
NSRange range = (NSRange){0,[newString length]};
[newString enumerateAttribute:NSFontAttributeName inRange:range options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired usingBlock:^(id value, NSRange range, BOOL *stop) {
    UIFont *replacementFont =  [UIFont fontWithName:@"Palatino-Roman" size:14.0];
    [newString addAttribute:NSFontAttributeName value:replacementFont range:range];
}];
self.label.attributedText = newString;

最快的方法是编辑
inst.desc
并以HTML格式将其添加到那里。否则,您必须枚举NSFontAttributeName的整个属性字符串。无法在ur代码中设置html内容,其中为html格式。是的,您应该在分配属性字符串后添加此内容。检查我编辑的回答UIFont*font=[UIFont fontWithName:@“Palatino Roman”大小:14.0];是我的字体这是字体问题我不明白你的问题是什么。我添加了UIfont,但[newString addAttribute:NSFontAttributeName value:replacementFont range:range];显示字体错误如果有粗体或斜体效果,这将“删除”它。