我正在寻找一种使用UILabel (或类似的东西)来显示类似以下内容的方法:
:一些消息。
这就像Facebook应用程序一样,显示“你在想什么?”消息。有没有人有什么建议来解决这个问题呢?
发布于 2009-09-13 10:11:54
根据需要使用两个UILabel IBOutlets,每个都有不同的格式(字体/颜色/等)。根据第一个文本的结束位置,将第二个文本移到第一个文本上。你可以通过sizeWithFont:forWidth:lineBreakMode获得:
或者,您可以子类化UILabel,然后自己在drawRect中绘制文本。如果您这样做,只需添加一个实例变量来告诉您以一种格式绘制多少字符串,而以另一种格式绘制其余部分。
更新:请参阅下面@Akshay的回复。从iOS6开始,UILabel可以包含NSMutableAttributedString。当我写这篇文章的时候,这是不可用的。
发布于 2013-02-06 00:31:40
有一种方法可以使用NSMutableAttributedString在标签上设置不同/多种字体和其他属性。Foll是我的代码:
UIFont *arialFont = [UIFont fontWithName:@"arial" size:18.0];
NSDictionary *arialDict = [NSDictionary dictionaryWithObject: arialFont forKey:NSFontAttributeName];
NSMutableAttributedString *aAttrString = [[NSMutableAttributedString alloc] initWithString:title attributes: arialDict];
UIFont *VerdanaFont = [UIFont fontWithName:@"verdana" size:12.0];
NSDictionary *verdanaDict = [NSDictionary dictionaryWithObject:VerdanaFont forKey:NSFontAttributeName];
NSMutableAttributedString *vAttrString = [[NSMutableAttributedString alloc]initWithString: newsDate attributes:verdanaDict];
[vAttrString addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:(NSMakeRange(0, 15))];
[aAttrString appendAttributedString:vAttrString];
lblText.attributedText = aAttrString;
请注意,lblText是UILabel,outlet是文件所有者。一个人可以继续添加他想要的NSMutableAttributedString。
还请注意,我已经在我的项目中添加了verdana & arial字体&为相同的字体添加了plist。
发布于 2014-10-30 19:33:30
很抱歉回答得太晚了。下面的代码很适合我。我张贴这篇文章是为了能对某些人有所帮助。
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字体。谢谢
https://stackoverflow.com/questions/1417346
复制相似问题