Objective-C:格式化 plist 中字符串对象中的文本
我是一名新的 Objective-C/iPhone 开发人员,想知道是否有任何方法可以将格式应用于 plist 中字符串内的文本?
我有一个 plist,它是一组字典。每个字典都包含一些字符串对象,其中一些我想包含信息列表和段落。我正在调用这些字符串并通过 UI 中的标签显示它们。
类似于使用
在该网站上一些善良用户的帮助下,我能够嵌入
预先感谢您的帮助!
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!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
听起来您的字符串位于 plist 中这一事实与该问题没有多大关系。无论如何,您都会以这种方式存储它们。但是,如果您想在 UI 中呈现类似 HTML 的格式,您有两种选择:
NSAttributedString
,即从 iOS 3.2 开始可用。我
相信你必须弄清楚
如何从 HTML 中获取
代表权在右边
字符串中的属性。
UIWebView
并直接渲染 HTML。这听起来很重量级,尤其是对于唱片公司来说,而且可能会是这样,但你可以尝试一下。(这里的要点是,你的问题没有一个简单的答案——Cocoa 框架本身并不支持太多 HTML,而且字符串内格式化也不是一件容易完成的事情。)
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:
NSAttributedString
, which isavailable 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.
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.)
要使 HTML 标记正常工作,您需要在 UIWebView(而不是 UILabel)中显示文本。查看 UIWebView 文档
更新:针对其他答案,我不相信 UILabel 即使在 iOS 4 上也能正确显示 NSAttributedString。Apple 推荐的样式化文本方法是使用 UIWebView (或自己绘制文本)使用 核心文本,即明显更复杂)。
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).