具有部分粗体文本的自定义UITableViewCell [英] Custom UITableViewCell with Partially Bold Text

查看:62
本文介绍了具有部分粗体文本的自定义UITableViewCell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试实现类似于内置Contacts iPhone应用程序的名称的UITableView,其中名字为普通字体,姓氏为粗体.一个快速的Google建议说这并不像听起来那样简单,因为iPhone SDK在UILabels中缺少富文本格式支持.

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.

推荐使用一种解决方案,建议使用UIWebView设置文本格式以允许HTML粗体标签提供所需的标记.如果我错了,请纠正我,但是这似乎是过大了,会对应用程序的性能产生影响.

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.

我正在尝试研究的另一种解决方案是创建一个自定义UITableViewCell,其中并排包含两个UILabel,第二个标签的格式设置为粗体文本.这似乎很简单,但是我不确定如何使标签水平自动调整大小以适合文本(最大一定宽度).是否可以通过Interface Builder做到这一点,还是需要以编程方式完成?

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?

推荐答案

我想知道是否应该继承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.

这篇关于具有部分粗体文本的自定义UITableViewCell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆