I used UITextView
in which i display NSAttributedString
. i got HTML string from server . I converted HTML to NSAttributedString using below code.
NSMutableAttributedString *attrib = [[NSMutableAttributedString alloc]initWithData:[strHTML dataUsingEncoding:NSUTF8StringEncoding] options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)} documentAttributes:nil error:nil];
To Apply font i tried below 2 codes.
- apply Attribute
- Custom String
To Apply Attribute i use below code.
[attrib addAttribute:NSFontAttributeName
value:myFont
range:NSMakeRange(0, attrib.length)];
For Custom String first i create below NSString
and then covert in NSAttributedString
NSString *strHTML = [NSString stringWithFormat:@"<span style="font-family: myFont; font-size: 12">%@</span>",@"my string"];
Using both code Font changed successfully. but Bold and Italic not applied only Underline is applied in NSAttributedString.
i refer below links.
- iOS 7 using an HTML string as an NSAttributedString AND setting the font?
- ios7 font size change when create nsattributedstring from html
- How to add CSS of an html to NSAttributedString?
for link 3 should i apply fonts and tags from server side and then retrieve HTML String?
Any Help would be appriciated !!!
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…