问题描述
我正在寻找一种使用 UILabel(或类似的东西)来显示如下内容的方法:
I am looking for a way to use a UILabel (or something similar) to display something like this:
汤姆:一些信息.
这就像在 Facebook 应用程序中显示你在想什么?"消息.有没有人有任何建议如何解决这个问题?
It is like how it's done in for example the Facebook app to display the "what's on your mind?" messages. Does anyone have any suggestions how to approach this?
推荐答案
使用两个 UILabel IBOutlets,每个都有不同的格式(字体/颜色/等),如你所愿.第一个的文本结束.你可以通过 sizeWithFont:forWidth:lineBreakMode:
Use two UILabel IBOutlets, each with a different format (font/color/etc), as you desire.. Move the second one over the first based on where the first one's text ends. You can get that via sizeWithFont:forWidth:lineBreakMode:
或者,您可以继承 UILabel,并在 drawRect 中自己绘制文本.如果这样做,只需添加一个实例变量,告诉您以一种格式绘制多少字符串,然后以另一种格式绘制其余部分.
Alternatively, you can subclass UILabel, and draw the text yourself in drawRect. If you do it this way, just add an instance variable to tell you how much of the string to draw in one format, and draw the rest in another.
更新:请参阅下面@Akshay 的回复.从 iOS6 开始,UILabel 可以包含 NSMutableAttributedString.当我写这篇文章时,这是不可用的.
Update: Please see @Akshay's response below. As of iOS6 UILabel's can contain NSMutableAttributedString. When I wrote this, this was not available.
这篇关于iPhone - UILabel 同时包含多种字体的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!