突出显示 UILabel 中的特定文本
Highlighting particular text in UILabel
在我的应用程序中,我在标签中显示这样的文本
cell.lblUserDetails.text=[NSString stringWithFormat:@"%@ and %@ other likes your post", [dictionary valueForKey:@"username"], [dictionary valueForKey:@"CuriousCount"]];
这里我想突出显示用户名和 CuriousCount(红色),用户名和 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;
在我的应用程序中,我在标签中显示这样的文本
cell.lblUserDetails.text=[NSString stringWithFormat:@"%@ and %@ other likes your post", [dictionary valueForKey:@"username"], [dictionary valueForKey:@"CuriousCount"]];
这里我想突出显示用户名和 CuriousCount(红色),用户名和 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;