开发者

Objective-C: Formatting text in a string object within a plist

I am a new objective-c/iPhone developer and am wondering if there is any way to apply formatting to text within a string in a plist?

I have a plist that is an array of dictionaries. Each dictionary contains a few string objects, some of which I would like to contain lists and paragraphs of information. I am calling these strings and displaying them via labels in the UI.

Similar to开发者_运维问答 using <li> and </li> or <b> and </b> in html, I would like to be able to specify parts of a string to be formatted when displayed as a bulleted list, bold, italic, etc.. How could this be accomplished?

With the help of some kind users on this site, I was able to embed the <li> and <b> tags within the xml plist file, however this just resulted in literally printing the tags on the screen.

Thanks in advance for your help!!


It doesn't sound like the fact that your strings are in a plist has much to do with the issue. You'll be storing them that way regardless. But if you want to render HTML-like formatting inside your UI, you have two choices:

  1. Parse the data into an NSAttributedString, which is available starting in iOS 3.2. I believe you'll have to figure out how to get from the HTML representation to the right attributes in the string.
  2. Use a UIWebView and render your HTML directly. This sounds heavyweight, esp for labels, and it probably would be, but you could try.

(Gist here is that there isn't a trivial answer to your question-- the Cocoa frameworks don't natively speak much HTML, and intra-string formatting isn't something that's necessarily easily done.)


For HTML markup to work, you'll need to display the text in a UIWebView, not a UILabel. Check out the UIWebView documentation here.

Update: In response to the other answer, I don't believe UILabel will correctly display an NSAttributedString even on iOS 4. Apple's recommended approach to doing styled text is to use a UIWebView (or draw the text yourself using Core Text, which is significantly more complicated).

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜