为了账号安全,请及时绑定邮箱和手机立即绑定

iPhone - UILabel包含同时具有多种字体的文本

iPhone - UILabel包含同时具有多种字体的文本

jeck猫 2019-08-31 11:20:50
我正在寻找一种方法来使用UILabel(或类似的东西)来显示这样的东西:汤姆:有些消息。这就像在Facebook应用程序中显示“你在想什么?”的方式。消息。有没有人有任何建议如何处理这个?
查看完整描述

3 回答

?
qq_笑_17

TA贡献1818条经验 获得超7个赞

根据需要使用两个UILabel IBOutlets,每个都有不同的格式(字体/颜色/等)。根据第一个文本结束的位置,将第二个移到第一个上面。你可以通过sizeWithFont得到它:forWidth:lineBreakMode:

或者,您可以继承UILabel,并在drawRect中自己绘制文本。如果你这样做,只需添加一个实例变量来告诉你要用一种格式绘制多少字符串,然后在另一种格式中绘制其余的字符串。

从iOS6开始,UILabel可以包含NSMutableAttributedString。当我写这篇文章时,这是不可用的。


查看完整回答
反对 回复 2019-08-31
?
UYOU

TA贡献1878条经验 获得超4个赞

对不起,迟到了。下面的代码非常适合我。我发布这个,以便它可以对某人有所帮助。


    UIFont *font1 = [UIFont fontWithName:kMyriadProSemiBold size:15];

NSDictionary *arialDict = [NSDictionary dictionaryWithObject: font1 forKey:NSFontAttributeName];

NSMutableAttributedString *aAttrString1 = [[NSMutableAttributedString alloc] initWithString:@"My" attributes: arialDict];


UIFont *font2 = [UIFont fontWithName:kMyriadProRegular size:15];

NSDictionary *arialDict2 = [NSDictionary dictionaryWithObject: font2 forKey:NSFontAttributeName];

NSMutableAttributedString *aAttrString2 = [[NSMutableAttributedString alloc] initWithString:@"Profile" attributes: arialDict2];



[aAttrString1 appendAttributedString:aAttrString2];

myProfileLabel.attributedText = aAttrString1;

请注意我的是半成品和个人资料是正常的。我使用过MyRiad字体。谢谢


查看完整回答
反对 回复 2019-08-31
  • 3 回答
  • 0 关注
  • 481 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信