iOS Tutorial Load HTML to Label

Discussion in 'iOS Tutorials' started by Brandsum, May 31, 2019.

  1. Brandsum

    Brandsum Active Member Licensed User

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

    Code:
    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:
    Code:
    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: Jun 9, 2019
  2. Sasuke Sama

    Sasuke Sama Active Member Licensed User

    Works so well
     
  3. Alberto Iglesias

    Alberto Iglesias Well-Known Member Licensed User

    Hello Brandsum, do you make this for B4A too?

    Thank you
     
  4. Brandsum

    Brandsum Active Member Licensed User

    Yes. I'm out of town. I'll post that in two days.
     
  5. Alberto Iglesias

    Alberto Iglesias Well-Known Member Licensed User

    Perfect Brandsum, I apreciate that! and I will wait!

    Thank youuuuuuuuuu
     
    Brandsum likes this.
  6. Brandsum

    Brandsum Active Member Licensed User

Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice