2014-01-23 43 views
2

我有一個UITextView,我試圖顯示一些HTML格式的屬性文本。它使用內聯粗體和斜體等內容。客戶現在想要改變這個字體。但是,每當我更改字體時,它都忽略了任何HTML格式。有什麼方法可以在保留其他格式屬性的情況下更改字體,如粗體斜體字?iOS 7使用HTML字符串作爲NSAttributedString並設置字體?

回答

5

我能得到它的工作方式是在前面加上我的字符串<span style=\"font-family: Avenir; font-size: 12\">%@</span>

1

對於一個Objective-C的方法,我用NSMutableString-setAttributes:

NSString *htmlString = @"..." 
NSData *textData = [htmlString dataUsingEncoding:NSUnicodeStringEncoding]; 
NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] 
     initWithData:textData 
      options:@{ NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType } 
documentAttributes:nil error:nil]; 
[attrStr setAttributes:@{NSFontAttributeName : [UIFont fontWithName:@"avenir" size:12.0f]} range:NSMakeRange(0,attrStr.length)]; 
相關問題