7

In the Html class, Android has:

public static Spanned fromHtml (String source)

which according to the documentation:

Returns displayable styled text from the provided HTML string.

.. and combining that with:

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

which according to the TextView documentation:

Sets the text that this TextView is to display (see setText(CharSequence)) and also sets whether it is stored in a styleable/spannable buffer and whether it is editable.

.. makes it possible to display a string with HTML markup in a TextView ( example from this StackOverflow answer ):

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

My question is - what is the equivalent to this in iOS?

I have done some research, and it seems that people recommends using the UIWebView for this purpose - but is that really the only solution? And is it the recommended solution?

Thanks.

Community
  • 1
  • 1
Lasse Christiansen
  • 10,205
  • 7
  • 50
  • 79

2 Answers2

3

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

To display more complex styling in your application, you need to use an UIWebView object and render your content using HTML.

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

2
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 is work fine for me