iOS Tutorial Load HTML to Label

(Here is the Android version)
This is how you can set HTML data to Label,

B4X:
Public Sub SetHTML(l As Label, htmlString As String)
    Dim NaObj As NativeObject = Me
    NaObj.RunMethod("SetHTML::",Array(l,htmlString))
End Sub

#if OBJC
- (void)SetHTML: (UILabel*) Label :(NSString *) htmlString{
   htmlString = [htmlString stringByAppendingString:[NSString stringWithFormat:@"<style>*{font-family: '%@'; font-size:%fpx;}</style>",
                                              Label.font.fontName,
                                              Label.font.pointSize]];
                                             
   NSMutableAttributedString * attrStr = [[NSMutableAttributedString alloc]
                                           initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding]
                                           options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType}
                                        documentAttributes:nil
                                        error:nil];
   
    if ([attrStr.string hasSuffix:@"\n"]) {
        [attrStr removeAttribute:NSParagraphStyleAttributeName range:NSMakeRange(0, attrStr.length)];
    }
    Label.attributedText = attrStr;
}

#end if

Usage:
B4X:
        Dim HtmlLable As Label
    HtmlLable.Initialize("")
    HtmlLable.Font = Font.CreateNew(16)
    SetHTML(HtmlLable,"YOUR HTML STRING")
    HtmlLable.Multiline = True
    HtmlLable.SizeToFit

It supports style tag, css class and id.
Happy Coding
 
Last edited:

Alberto Iglesias

Well-Known Member
Licensed User
Longtime User
Hello Brandsum, do you make this for B4A too?

Thank you
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…