开发者

Can iphone's labels, textfields or other UI elements accept HTML formatted text and display it correctly?

开发者 https://www.devze.com 2023-03-31 17:39 出处:网络
Is there any trick so i can give html formatted text to e.g. UIL开发者_如何学Cabel or UITextField and iphone will format the text accordingly?

Is there any trick so i can give html formatted text to e.g. UIL开发者_如何学Cabel or UITextField and iphone will format the text accordingly?

For example if there is a bold tag, the text will be bolded.


No, the only class I know in iOS that displays HTML is UIWebView. Of course, you can use a small web view to display some HTML to get text styles. But if you're only want to draw static styled text in a view you can use CoreText. There are many examples out there (including one from Apple called CoreTextPageViewer) of how to draw an NSAttributedString with CoreText.

Hope that helps!


try with NSAttributedString.

https://nodeload.github.com/AliSoftware/OHAttributedLabel/zipball/master

inthis example ,they show on UILable


You can try with NSAttributedString

label.attributedText =
[[NSAttributedString alloc]
          initWithData: [html dataUsingEncoding:NSUTF8StringEncoding]
               options: @{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType }
    documentAttributes: nil
                 error: &err];


Short answer, no.

To display HTML you need to use a UIWebView. If you want a rich text editor then you need to look at 3rd party Such as three20. Or on iOS > 4 you can use contentEditable in a UIWebView


Only UIWebView supports html but you should also checkout this repo on github -

https://github.com/honcheng/RTLabel


i think it is better way display HTML content in UIwebview compare to handle in other component


Take a look at HTML String content for UILabel and TextView Where he mentions about "Three20 project which includes a class TTStyledTextLabel which allows you to render HtmL-Code and even activates links"

0

精彩评论

暂无评论...
验证码 换一张
取 消