Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question

2 Answers

up vote 3 down vote accepted

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.

share|improve this answer
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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.