<b> tag not working in UILabel attributed string
Asked Answered
M

3

1

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;
Mackler answered 4/4, 2016 at 7:27 Comment(5)
Try with dataUsingEncoding:NSUnicodeStringEncoding instead of NSUTF8StringEncodingCrain
not working too, its displaying the text with color having in html but bold tag is not working hereMackler
Is your font kFontName already a bold font?Those
No, but let me check by removing font form here as the size is already there in htmlMackler
THANKS Xvolks, removing font form attributed worked and I am getting bold text now in HTMLMackler
G
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.

enter image description here

Glasper answered 4/4, 2016 at 8:19 Comment(2)
This is the correct way of doing this. Not using own font is an 'avoidance' mechanism. Setting NSDocumentTypeDocumentAttribute to NSHTMLTextDocumentType is the right way.Sivas
Did you try this with iOS11?Flagon
M
0

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])];
Mackler answered 4/4, 2016 at 8:5 Comment(0)
H
0

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

enter image description here

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
            }
        }
}
Hawfinch answered 21/3, 2020 at 9:16 Comment(3)
I would also recommend to set default text color. I had problems using <style> like this, I usually prefer wrapping the text in a <font style="..."> element (tag name font does not matter, just an element to wrap the string into).Ludwog
@Ludwog My answer is more on which Font file to use. Your suggestion regarding wrapping text in <font style="..."> is also correct but I think that depends on how your HTML text is formattedHawfinch
Use CTFontManagerRegisterFontURLs instead of the plist if you want to have full control over the font registration process. I had a case where I provided fonts from a framework so I could not use the plist anyway, but reading the fonts from the framework bundle and registering them manually enabled them properly in the NSAttributedString that was created from html.Dunsinane

© 2022 - 2024 — McMap. All rights reserved.