str 为某一字符串
NSString *str = RowArr[indexPath.row];
NSRange allRange = NSMakeRange(0, str.length);
NSRange range = NSMakeRange(0, 3);
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:str];
UIFont *boldSystemFont = [UIFont boldSystemFontOfSize:15];
CTFontRef font = CTFontCreateWithName((__bridge CFStringRef)boldSystemFont.fontName, boldSystemFont.pointSize, NULL);
[attributedString addAttribute:(NSString *)kCTFontAttributeName value:(__bridge id)font range:NSMakeRange(0, str.length)];
[attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHex:0xD94448] range:allRange];
[attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHex:0X5E5E5E] range:range];
cell.orderInfoLabel.delegate = self;
[cell.orderInfoLabel addLinkToURL:[NSURL URLWithString:@"买家"] withRange:NSMakeRange(3, str.length)];
cell.orderInfoLabel.attributedText = attributedString;
事件点击事件的方法
#pragma mark - TTTAttributedLabelDelegate
- (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithURL:(NSURL *)url
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Message" bundle:nil];
BubbleChatViewController *bubbleChatVC = [storyboard instantiateViewControllerWithIdentifier:@"BubbleChatVC"];
NSString *str = [[label.text componentsSeparatedByString:@":"] objectAtIndex:0];
if ([str isEqualToString:@"买家"]) {
bubbleChatVC.userID = self.order.buyer.userID;
} else {
bubbleChatVC.userID = self.order.opus.opusAuthor.userID;
}
[self.navigationController pushViewController:bubbleChatVC animated:YES];
}