ios - 在UILabel中突出显示特定文本

标签 ios objective-c uilabel

在我的应用程序中,我像这样在标签中显示文本

cell.lblUserDetails.text=[NSString stringWithFormat:@"%@ and %@ other likes your post", [dictionary valueForKey:@"username"], [dictionary valueForKey:@"CuriousCount"]];

在这里,我要突出显示Username和CuriousCount(以红色显示),并且username和CuriousCount也应该是可单击的。

最佳答案

UIColor *color = [UIColor redColor];
NSDictionary *attrs = @{ NSForegroundColorAttributeName : color };
NSAttributedString *nameStr = [[NSAttributedString alloc] initWithString:[dictionary valueForKey:@"username"] attributes:attrs];
NSAttributedString *countStr = [[NSAttributedString alloc] initWithString:[dictionary valueForKey:@"CuriousCount"] attributes:attrs];

NSMutableAttributedString *string = [[NSMutableAttributedString alloc] init];
[string appendAttributedString:nameStr];
[string appendAttributedString:[[NSAttributedString alloc] initWithString:@" and "]];
[string appendAttributedString:countStr];
[string appendAttributedString:[[NSAttributedString alloc] initWithString:@" other likes your post"]];

cell.lblUserDetails.attributedText = string;

关于ios - 在UILabel中突出显示特定文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30122031/

相关文章:

ios - 一种在开发和生产之间更改应用程序名称的方法

ios - 更新到 Xcode 7.0.1 后上传 iOS 应用程序时出错

ios - 在 Swift 的后台线程中执行时 usleep 的精度不佳

ios - 只为 iPad 设置横向方向而不是 iPhone

ios - 如何为 iOS 应用程序使用现有的 GCM 发件人 ID

ios - UILabel 无法正确更新

ios - 刷新 UILabel

ios - 你能从应用程序端静音传入的推送通知吗[iOS]

ios - 在 Xcode 中更改 UIlabel 文本大小?

ios - 存折集成 iOS(动态)