<b> tag not working in UILabel attributed string

只愿长相守 提交于 2021-01-29 02:27:39

问题


I am displaying html string into attributed text if UILabel but few of the tags are not working in UILabel like

lineLabel.numberOfLines=0;
 NSString* html = self.offerArray[0][@"description"];
NSString * htmlString = [NSString stringWithFormat: @"%@%@</body></html>",kHTMLHeadOffer,html];
htmlString = [htmlString stringByReplacingPercentEscapesUsingEncoding:NSUnicodeStringEncoding];
NSMutableAttributedString * attrStr = [[NSMutableAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUTF8StringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
UIFont *font = [UIFont fontWithName:kFontName size:kOrderFont]; [attrStr addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, [attrStr length])];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init] ;
[paragraphStyle setAlignment:NSTextAlignmentCenter];
[attrStr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [attrStr length])];
lineLabel.attributedText = attrStr;

回答1:


Please try this code once.

NSString *htmlstr = @"<p>This text is normal.</p> <p><b>This text is bold</b>.
</p> <p><big>Bigger text</big></p> <font size=3 color=red>This is some text!</font>
<font size=2 color=blue>This is some text!</font> <font face=verdana color=green>This is 
some text!</font>"; 

NSAttributedString *strAttributed = [[NSAttributedString alloc]
initWithData: [htmlstr dataUsingEncoding:NSUTF8StringEncoding] options: 
@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes: nil
error: nil];

[_lblHtmlText setAttributedText:strAttributed];

Here is the output screen.




回答2:


Removing Font attribute worked for me :)

I have removed below lines from my code

 UIFont *font = [UIFont fontWithName:kFontName size:kOrderFont];
[attrStr addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, [attrStr length])];



回答3:


MY IMPORTANT OBSERVATION WHILE CONVERTING HTML TO NSAttributedString WITH CUSTOM FONT

Which font format to use?

Use .otf font for your custom font file, .ttf is not supporting HTML to NSAttributedString properly.

Important thing to remember while adding .otf font in project You need to give full name including .otf extension while adding in Info.plist file, like in following image

BONUS

Using following function to convert my HTML text to NSAttributedString

extension String {
    func htmlAttributed(using font: UIFont, alignment textAlignment: NSTextAlignment = .natural) -> NSAttributedString? {
            do {
                let htmlCSSString = "<style>" +
                    "html *" +
                    "{" +
                    "font-size: \(font.pointSize)px !important;" +
                    "font-family: \(font.fontName), Helvetica !important;" +
                    "text-align: \(textAlignment.htmlAlignment());" +
                "}</style> \(self)"

                guard let data = htmlCSSString.data(using: String.Encoding.utf8) else {
                    return nil
                }

                let attribText = try NSAttributedString(data: data,
                                                        options: [.documentType: NSAttributedString.DocumentType.html,
                                                                  .characterEncoding: String.Encoding.utf8.rawValue],
                                                        documentAttributes: nil)
                return attribText
            }
            catch {
                print("error: ", error)
                return nil
            }
        }
}


来源:https://stackoverflow.com/questions/36396779/b-tag-not-working-in-uilabel-attributed-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!