在 UIView 中将字符串的一部分设置为粗体

发布于 2024-10-05 15:50:45 字数 343 浏览 0 评论 0原文

我正在解析一个 XML 文件并获取一个字符串作为 NSMutableString。 现在我想在我的 UIView 中将此字符串显示为文本。 因此,我创建了一个 TextView 并将其值设置为我的字符串。结果是我在 TextView 中显示了带有 HTML 标签的文本。

从字符串中删除标签没有问题,但某些文本需要显示为粗体。

有没有办法以编程方式将字符串的一部分显示为粗体?

我用 \n 替换了
元素,它对于换行效果很好。

但我仍然不知道如何在 UIView 中将强标记之间的单词显示为粗体。

I'm parsing an XML file an get a string as NSMutableString.
Now I want to show this string as text in my UIView.
So, I created a TextView and set its value to my string. The result was that I have the text with HTML Tags shown up in the TextView.

There is no problem removing the tags from the string, but some of the text needs to appear as bold.

Is there any way to display parts of a string as bold programmatically?

I replaced <br> Elements with \n and it works fine for line-break.

But I still don't know what I can do to display words between strong tags as bold in the UIView.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

赠意 2024-10-12 15:50:45

有两种方法。第一种是在 UIWebViews 中显示它们;在 iOS 4.1 之前,这实际上是唯一的方法。因为文本中已经有 HTML 代码,所以这肯定很容易。

如果您同意您的应用程序仅在 iOS 4.1 及更高版本上运行,您可以使用 Core Text,它提供了非常丰富的格式。

编辑

Apple提供了一些很好的资源:

关于 iOS 中的文本、Web 和编辑支持

核心文本编程指南

WWDC 2010 会议视频 - 点击 iTunes 链接并获取会议 110:iPhone 操作系统的高级文本处理
(需要登录苹果开发者)

There are two ways. The first is by displaying them in UIWebViews; prior to iOS 4.1 this was effectively the only way. Because you already have HTML codes in the text, it would certainly be easy.

If you're ok with your app running only on iOS 4.1 and up you can use Core Text, which provides very rich formatting.

EDIT

Apple provides some good resources:

About Text, Web, and Editing Support in iOS

Core Text Programming Guide

WWDC 2010 Session Videos -- follow the link to iTunes and grab session 110: Advanced Text Handling for iPhone OS
(Apple Developer login required)

梦中楼上月下 2024-10-12 15:50:45

您可以使用 UIWebView 并从 NSString 对象加载内容。

无论如何,如果你想坚持使用 Cocoa,请看看 NSAttributedString。

You may use UIWebView and load your content from an NSString object.

Anyway if you want to stick to Cocoa, have a look to NSAttributedString.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文