在字符串中,指定某字符子串的交互效果,使用YYLabel和YYTextHighlight进行设置NSAttributedString来实现。
效果图
代码示例
实例化YYLabel
YYLabel *label = [[YYLabel alloc] initWithFrame:CGRectMake(10, 10, (self.view.frame.size.width - 20), 100)];
[self.view addSubview:label];
label.backgroundColor = [UIColor colorWithWhite:0 alpha:0.3];
label.numberOfLines = 0;
实例化字符NSAttributedString
NSString *string = @"我已阅读并同意服务平台相关协议和条款《用户服务协议》《法律声明》";
NSMutableAttributedString *attributed = [[NSMutableAttributedString alloc] initWithString:string];
[attributed setAttribute:NSForegroundColorAttributeName value:UIColor.blueColor range:[string rangeOfString:@"《用户服务协议》"]];