[英]Change font size of NSAttributedString
我的應用程序從網站上獲取了一些HTML格式的文本,我想更改NSAttributedString
的字體大小並將其顯示在textview中
var htmlString : String //the html formatted text
textView.attributedText = handleHtml(htmlString) // this is how I call the function, but the font size didn't change
func handleHtml(string : String) -> NSAttributedString{
let attrs = [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSFontAttributeName: UIFont.systemFontOfSize(20.0)]
var returnStr = NSAttributedString()
do {
returnStr = try NSAttributedString(data: string.dataUsingEncoding(NSUnicodeStringEncoding, allowLossyConversion: true)!, options: attrs, documentAttributes: nil)
} catch {
print(error)
}
return returnStr
}
如上面的代碼所示,我嘗試了[NSFontAttributeName: UIFont.systemFontOfSize(20.0)]
,但是字體大小沒有變化。
我只是想通了。 我應該使用NSMutableAttributedString
而不是NSAttributedString
func handleHtml(string : String) -> NSAttributedString{
var returnStr = NSMutableAttributedString()
do {
returnStr = try NSMutableAttributedString(data: string.dataUsingEncoding(NSUnicodeStringEncoding, allowLossyConversion: true)!, options: [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil)
let fullRange : NSRange = NSMakeRange(0, returnStr.length)
returnStr.addAttributes([NSFontAttributeName : yourFont], range: fullRange)
} catch {
print(error)
}
return returnStr
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.