iPhone - UILabel包含同时具有多种字体的文本 [英] iPhone - UILabel containing text with multiple fonts at the same time

查看:127
本文介绍了iPhone - UILabel包含同时具有多种字体的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法来使用UILabel(或类似的东西)来显示如下内容:

I am looking for a way to use a UILabel (or something similar) to display something like this:

Tom:一些消息。

就像在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包含同时具有多种字体的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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