我无法让 attributedText 一起管理字体和粗体。 我的场景是一个 View Controller ,它带有一个接受属性文本的可滚动 TextView 。
属性是通过使用下面的函数转换一个简单的 html 字符串(ul 和 b)来创建的。
没问题。当我稍后将文本设置为 attributedData 然后设置字体时,问题出现在我的 View Controller 中。
先处理文本,再处理字体:从显示中移除粗体。 先处理字体,再处理文本:停止字体工作。
我怀疑粗体在字符串中没有表示为“粗体”,而是使用字体粗体。
当我设置文本,然后设置字体时,它会覆盖属性中的所有字体(包括粗体)。
当我设置字体,然后是文本时,属性中的字体是最后一个,并且设置为默认字体但正确切换为粗体版本的字体。
有没有办法更改 attributedString 的字体并保持粗体/斜体? 不是我喜欢的地方,但是字体可以在 NSAttributedString 函数的选项中设置吗?
func html2AttributedString(fromString: String) -> NSAttributedString {
var returnString: NSAttributedString
do {
let attributedData = fromString.dataUsingEncoding(NSUnicodeStringEncoding)
let tempAttributedString = try NSAttributedString(data: attributedData!, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil)
returnString = tempAttributedString
} catch {
let tempString = "Unknown String"
let attributedData = tempString.dataUsingEncoding(NSUnicodeStringEncoding)
let tempAttributedString = try! NSAttributedString(data: attributedData!, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil)
returnString = tempAttributedString
}
return returnString
}
在 View Controller 中设置文本
contentTextView.attributedText = descriptionTextAttributed
contentTextView.textColor = UIColor.blackColor()
if let font = UIFont(name: "StoneSans", size: 20.0) {
contentTextView.font = font
}
更新:感谢 Wain 的评论。
我可以看到更改字体的三个级别:html、转换时和完成转换时。
Wain 提出了在转换后在 attributedText 中更改它的方法,但我的 CPU 也有限,而且该方法对用户来说会明显变慢。
我已经尝试以编程方式为 html 执行此操作,但它不起作用。如果有人可以提供帮助,也许我的插入内容需要修复:
//let tempString = "<style> {font-family: sans-serif;}</style>" + fromString
//let tempString = "<body style='font-family=sans-serif'>" + fromString
我目前正在尝试使用 documentAttributes 获得正确的语法,以尝试更改转换期间使用的属性(请参阅 How do you use NSAttributedString?)。这段代码没有编译,因为我没有正确准备 documentAttributes:
if let font = UIFont(name: "StoneSans", size: CGFloat(AppData.sharedInstance.instructionFontSize)) {
do {
let attributedData = fromString.dataUsingEncoding(NSUnicodeStringEncoding)
let myAttributes:Dictionary = [
NSParagraphStyleAttributeName : NSMutableParagraphStyle(),
NSKernAttributeName : 3, // (-1,5)
NSFontAttributeName : font,
NSForegroundColorAttributeName : UIColor.whiteColor(),
NSShadowAttributeName : nil
]
let tempAttributedString = try NSAttributedString(data: attributedData!, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: myAttributes)
returnString = tempAttributedString
} catch {
let tempString = "Unknown String"
let attributedData = tempString.dataUsingEncoding(NSUnicodeStringEncoding)
let tempAttributedString = try! NSAttributedString(data: attributedData!, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil)
returnString = tempAttributedString
}
}
如果有人可以修复我的代码以解决这两种更改字体的早期方法,我们将不胜感激。
蒂姆。
最佳答案
没错,粗体和斜体信息是字体信息的一部分。使用 NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType
使用默认字体(或 HTML 中指定的任何字体)。
因此,您需要编辑字体而不是仅仅替换它们,例如:
NSMutableAttributedString *attributedString = [self mutableCopy];
[attributedString beginEditing];
[attributedString enumerateAttribute:NSFontAttributeName inRange:NSMakeRange(0, attributedString.length) options:0 usingBlock:^(UIFont *value, NSRange range, BOOL *stop) {
NSString *fontName = [value.fontName lowercaseString];
UIFont *destinationFont = [UIFont XXXX];
if ([fontName rangeOfString:@"bold"].location != NSNotFound) {
destinationFont = [UIFont BOLD_XXXX];
} else if ([fontName rangeOfString:@"italic"].location != NSNotFound) {
destinationFont = [UIFont ITALIC_XXXX];
}
[attributedString removeAttribute:NSFontAttributeName range:range];
[attributedString addAttribute:NSFontAttributeName value:destinationFont range:range];
}];
[attributedString endEditing];
关于ios - 在 swift attributedString 中设置字体正在删除粗体强调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35200260/