What is equivalent to Android Html.fromHtml () in iOS?

In the Html class, Android has:

 public static Spanned fromHtml (String source) 

which according to the documentation :

Returns the displayed stylized text from the provided HTML string.

.. and combining this with:

 public void setText (CharSequence text, TextView.BufferType type) 

which according to the documentation of TextView :

Sets the text displayed by this TextView (see setText (CharSequence)), and also sets whether it is stored in a custom / spun buffer and whether it is available.

.. allows you to display a line with HTML markup in a TextView (example from https://stackoverflow.com/a/3/9856/ ... ):

 String styledText = "This is <font color='red'>simple</font>."; textView.setText(Html.fromHtml(styledText), TextView.BufferType.SPANNABLE); 

My question is what is equivalent to this in iOS?

I did some research and it seems that people are recommending using UIWebView for this purpose - but is this really the only solution? And is this a recommended solution?

Thanks.

+6
source share
2 answers

Apple itself recommends it , so it should be a good alternative solution ...

To display a more complex style in your application, you need to use the UIWebView object and display its contents using HTML.

If you really don't want to use UIWebView, you can use NSAttributedString objects and render single-line text using OHAttributedLabel .

+3
source
 UITextView *textview= [[UITextView alloc]initWithFrame:CGRectMake(10, 130, 250, 170)]; NSString *str = @"This is <font color='red'>simple</font>"; [textview setValue:str forKey:@"contentToHTMLString"]; textview.textAlignment = NSTextAlignmentLeft; textview.editable = NO; textview.font = [UIFont fontWithName:@"vardana" size:20.0]; [UIView addSubview:textview]; 

this works great for me

+2
source

Source: https://habr.com/ru/post/924673/


All Articles