带有部分粗体文本的自定义 UITableViewCell
尝试实现类似于内置联系人 iPhone 应用程序的名称 UITableView,其中名字为普通字体,姓氏为粗体。快速谷歌一下后发现,这并不像听起来那么容易,因为 iPhone SDK 缺乏 UILabels 中的富文本支持。
找到的解决方案之一建议使用 UIWebView 来格式化文本,从而允许 HTML 粗体标签提供所需的标记。如果我错了,请纠正我,但这似乎有点矫枉过正,并且会影响应用程序的性能。
我试图进一步研究的另一个解决方案是创建一个自定义 UITableViewCell,其中包含两个并排的 UILabel,第二个标签格式为粗体文本。这看起来很简单,但我不确定如何让标签自动水平调整大小以适应文本(达到某个最大宽度)。这可以通过 Interface Builder 实现还是需要以编程方式完成?
我是否走在正确的轨道上,或者是否有更简单的方法来实现这种看似微不足道的效果?
Trying to implement a UITableView of names similar to the built-in Contacts iPhone app where the first name is in normal font and the last name is bold. A quick google suggests this isn't as easy as it sounds due to the iPhone SDKs lack of rich text support in UILabels.
One of the solutions found recommended using a UIWebView to format the text allowing HTML bold tags to provide the required markup. Correct me if I'm wrong but this seems to be overkill and would have an affect on the performance of the app.
Another solution which I'm trying to investigate more is to create a custom UITableViewCell containing two UILabels side by side with the second label formatted for bold text. This seems straightforward, but I'm not sure how to have the labels auto-size horizontally to fit the text (up to a certain max width). Is this possible via the Interface Builder or does this need to be done programmatically?
Am I on the right track here or is there a much easier way to achieve this seemingly trivial effect?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我想知道你是否应该子类化 UILabel 并自己绘制。然后你可以使用
CGContextSelectFont
,CGContextGetTextPosition
,CGContextSetTextPosition
,CGContextShowText
等来绘制你的文本。选择普通字体,设置初始文本位置,绘制文本,将文本位置前进几个像素,选择粗体字体,绘制粗体文本,等等。
我自己以前没有这样做过,但我也考虑在我的一个应用程序中使用这个解决方案。
I wonder if you should subclass UILabel and do the drawing yourself. Then you could use
CGContextSelectFont
,CGContextGetTextPosition
,CGContextSetTextPosition
,CGContextShowText
etc to draw your text.Select the normal font, set the initial text position, draw your text, advance the text position by a few pixels, select the bold font, draw your bold text, and so on.
I haven't done this myself before, but am thinking of using this solution for one of my applications as well.
看起来这个问题已经被问了很少< /a> Stack Overflow 上的次数各种伪装。一个常见的解决方案似乎是 Three20 库的 TTStyledTextLabel。在 iPhone SDK 包含内置的富文本标签并且没有推出我自己的标签之前,这个类看起来是下一个最好的东西。
编辑:刚刚遇到这个解决方案,它看起来更好: https://github.com/chrisdevereux/Slash
It looks like this has been asked a few times on Stack Overflow under various guises. A common solution seems to be the Three20 library's TTStyledTextLabel. Until the iPhone SDK includes an inbuilt rich text label and short of rolling my own, this class looks to be the next best thing.
Edit: Just came across this solution and it looks even better: https://github.com/chrisdevereux/Slash