如何为 NSAttributedString 设置默认字体
How to set default font for NSAttributedString
根据 apple 的文档,如果您没有为给定范围的属性字符串提供任何字体,默认情况下它将采用 12 号的 Helvetica 字体。
现在我想更改默认字体。谁能建议我该怎么做。
实际上我想实现以下目标:
我有 html 的字符串(来自服务器的动态 html),假设它是 htmlStr
。在此字符串中,可能已为 html 的不同部分设置了字体,也可能没有为 htmlStr.
设置任何字体
现在,因为我想显示+编辑这个 htmlStr,所以首先我将这个 htmlStr 转换成 attributedString,然后通过设置 attributedText
在 textview 中显示它文本视图。
现在我的问题是,如果没有为这个 htmlStr 提供任何字体,它会使用默认字体,该字体太小而且看起来不好看。那么,如何更改属性字符串的默认字体。
尝试
Objective - C
__block BOOL found = NO;
NSMutableAttributedString *attrString = ...
[attrString beginEditing];
[attrString enumerateAttribute:NSFontAttributeName inRange:NSMakeRange(0, attrString.length) options:0 usingBlock:^(id value, NSRange range, BOOL *stop) {
if (value) {
UIFont *oldFont = (UIFont *)value;
UIFont *newFont = [oldFont fontWithSize:oldFont.pointSize * 2];
[attrString addAttribute:NSFontAttributeName value:newFont range:range];
found = YES;
}
}];
if (!found) {
// No font was found - do something else?
}
[attrString endEditing];
Swift 4
var found = false
var attrString = NSMutableAttributedString()
attributedText.enumerateAttribute(NSAttributedStringKey.font, in: NSMakeRange(0, attributedText.length), options: NSAttributedString.EnumerationOptions(rawValue: 0)) { (value, range, stop) in
if let oldFont = value as? UIFont {
let newFont = oldFont.withSize(oldFont.pointSize * 2)
attributedText.addAttribute(NSAttributedStringKey.font, value: newFont, range: range)
found = true
}
}
if !found {
// No font was found - do something else?
}
attributedText.endEditing()
Try this one also:-
- (NSAttributedString *) convertToAttributedString: (NSString *) string withStringToConvert: (NSString *) str2 {
NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:string];
[attributedStr addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0, string.length)];
[attributedStr addAttribute:NSFontAttributeName value:[UIFont fontWithName:FONT_APPLE_SD_GOTHIC_NEO_BOLD size:17.0] range:NSMakeRange(0, string.length)];
[attributedStr addAttribute:NSForegroundColorAttributeName value:[UIColor darkGrayColor] range:NSMakeRange(0,str2.length)];
[attributedStr addAttribute:NSFontAttributeName value: [UIFont fontWithName:FONT_APPLE_SD_GOTHIC_NEO size:17.0] range:NSMakeRange(0, str2.length)];
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
[style setLineSpacing:3];
[attributedStr addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, string.length)];
return attributedStr;
}
根据 apple 的文档,如果您没有为给定范围的属性字符串提供任何字体,默认情况下它将采用 12 号的 Helvetica 字体。
现在我想更改默认字体。谁能建议我该怎么做。
实际上我想实现以下目标:
我有 html 的字符串(来自服务器的动态 html),假设它是 htmlStr
。在此字符串中,可能已为 html 的不同部分设置了字体,也可能没有为 htmlStr.
现在,因为我想显示+编辑这个 htmlStr,所以首先我将这个 htmlStr 转换成 attributedString,然后通过设置 attributedText
在 textview 中显示它文本视图。
现在我的问题是,如果没有为这个 htmlStr 提供任何字体,它会使用默认字体,该字体太小而且看起来不好看。那么,如何更改属性字符串的默认字体。
尝试
Objective - C
__block BOOL found = NO;
NSMutableAttributedString *attrString = ...
[attrString beginEditing];
[attrString enumerateAttribute:NSFontAttributeName inRange:NSMakeRange(0, attrString.length) options:0 usingBlock:^(id value, NSRange range, BOOL *stop) {
if (value) {
UIFont *oldFont = (UIFont *)value;
UIFont *newFont = [oldFont fontWithSize:oldFont.pointSize * 2];
[attrString addAttribute:NSFontAttributeName value:newFont range:range];
found = YES;
}
}];
if (!found) {
// No font was found - do something else?
}
[attrString endEditing];
Swift 4
var found = false
var attrString = NSMutableAttributedString()
attributedText.enumerateAttribute(NSAttributedStringKey.font, in: NSMakeRange(0, attributedText.length), options: NSAttributedString.EnumerationOptions(rawValue: 0)) { (value, range, stop) in
if let oldFont = value as? UIFont {
let newFont = oldFont.withSize(oldFont.pointSize * 2)
attributedText.addAttribute(NSAttributedStringKey.font, value: newFont, range: range)
found = true
}
}
if !found {
// No font was found - do something else?
}
attributedText.endEditing()
Try this one also:-
- (NSAttributedString *) convertToAttributedString: (NSString *) string withStringToConvert: (NSString *) str2 {
NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:string];
[attributedStr addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0, string.length)];
[attributedStr addAttribute:NSFontAttributeName value:[UIFont fontWithName:FONT_APPLE_SD_GOTHIC_NEO_BOLD size:17.0] range:NSMakeRange(0, string.length)];
[attributedStr addAttribute:NSForegroundColorAttributeName value:[UIColor darkGrayColor] range:NSMakeRange(0,str2.length)];
[attributedStr addAttribute:NSFontAttributeName value: [UIFont fontWithName:FONT_APPLE_SD_GOTHIC_NEO size:17.0] range:NSMakeRange(0, str2.length)];
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
[style setLineSpacing:3];
[attributedStr addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, string.length)];
return attributedStr;
}