你怎么能在UILabel中只用一个词呢? [英] How can you bold just one word in a UILabel?

查看:88
本文介绍了你怎么能在UILabel中只用一个词呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

显示完全相同文字的单个标签很容易,但是当你想以粗体显示一个单词时会发生什么?

It's easy to display a single label with entirely the same text but what happens when you want to display one word in bold?

示例:

您所有的基地 属于我们。

All your bases are belong to us.

推荐答案

UILabel 本身无法在文本中绘制具有不同属性/字体的标签。但核心文本可以。您可以创建一个 UILabel 的子类,它包装一个属性字符串并使用Core Text绘制它。您可以像在此示例代码

UILabel itself cannot draw a label with different attributes/fonts within the text. But Core Text can. You can create a subclass of UILabel that wraps an attributed string and uses Core Text to draw it. You can handle the drawing as in this sample code from the Core Text programming guide:

// Build up your attributed string, then...
CTLineRef line = CTLineCreateWithAttributedString(attrString);

// Set text position and draw the line into the graphics context
CGContextSetTextPosition(context, 10.0, 10.0);
CTLineDraw(line, context);
CFRelease(line);

这篇关于你怎么能在UILabel中只用一个词呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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