【问题标题】:Highlighting particular text in UILabel突出显示 UILabel 中的特定文本
【发布时间】:2015-07-19 06:19:26
【问题描述】:

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

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

在这里我想突出显示用户名和 CuriousCount(红色),并且用户名和 CuriousCount 应该是可点击的。

【问题讨论】:

标签: ios objective-c uilabel


【解决方案1】:
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;

【讨论】:

  • 由于不兼容的指针类型将 nsstring 发送到 nsattributedstring 导致错误(应用程序崩溃)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-23
  • 2020-12-17
  • 2020-04-17
  • 2013-04-30
  • 1970-01-01
相关资源
最近更新 更多