注意事项:需要点击事件则必须使用textView来呈现文本。不需要的话可随便使用
#define XGWidth [UIScreen mainScreen].bounds.size.width
控制器内代码部分
UITextView *textView = [[UITextView alloc]initWithFrame:CGRectMake(0, 0, XGWidth,60)];
textView.text = @"用户注册代表同意《服务条款》和《隐私政策》"; textView.backgroundColor=XGCOLORFromRGB(0xf1f1f1);
textView.font = [UIFont systemFontOfSize:12];
[footerView addSubview:textView];
// 字体的行间距
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing= 1;
NSDictionary*attributes = @{
NSFontAttributeName:[UIFont systemFontOfSize:12],
NSParagraphStyleAttributeName:paragraphStyle
};
//设置富文本点击
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:textView.text attributes:attributes];
[attributedString addAttribute:NSLinkAttributeName
value:@"fuwutiaokuan://"
range:[[attributedStringstring]rangeOfString:@"《服务条款》"]];
[attributedString addAttribute:NSLinkAttributeName
value:@"yisizhengce://"
range:[[attributedStringstring]rangeOfString:@"《隐私政策》"]];
//设置字体
[attributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14] range:NSMakeRange(8,6)];
[attributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:16] range:NSMakeRange(15,6)];
//设置整体颜色
[attributedString addAttribute:NSForegroundColorAttributeName value:XGCOLORFromRGB(0x999999) range:NSMakeRange(0,textView.text.length)];
textView.attributedText= attributedString;
//设置被点击字体颜色
textView.linkTextAttributes = @{NSForegroundColorAttributeName:XGCOLORFromRGB(0x666666)};
textView.delegate=self;
//必须禁止输入,否则点击将弹出输入键盘
textView.editable=NO;
textView.scrollEnabled=NO;
代理方法的实现
#pragma mark 富文本点击事件
- (BOOL)textView:(UITextView*)textView shouldInteractWithURL:(NSURL*)URL inRange:(NSRange)characterRange {
if ([[URL scheme] isEqualToString:@"fuwutiaokuan"]) {
NSLog(@"富文本点击 服务条款");
}else if ([[URL scheme] isEqualToString:@"yisizhengce"]) {
NSLog(@"富文本点击 隐私政策");
}
return YES;
}
效果图
二、使用lable实现 (没有点击事件)
UILabel*agreeLab = [[UILabel alloc]initWithFrame:CGRectMake(0,0,XGWidth,60)];
agreeLab.text = @"用户注册代表同意《服务条款》和《隐私政策》";
agreeLab.textColor=XGCOLORFromRGB(0x999999);
agreeLab.font = [UIFont systemFontOfSize:12];
[footerView addSubview:agreeLab];
NSMutableAttributedString *attrStr1 = [[NSMutableAttributedString alloc]initWithString:agreeLab.text];
//颜色
[attrStr1 addAttribute:NSForegroundColorAttributeName value:XGCOLORFromRGB(0x666666) range:NSMakeRange(8,6)];
[attrStr1 addAttribute:NSForegroundColorAttributeName value:XGCOLORFromRGB(0x666666) range:NSMakeRange(15,6)];
//设置字体
[attrStr1 addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14] range:NSMakeRange(8,6)];
[attrStr1 addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:16] range:NSMakeRange(15,6)];
agreeLab.attributedText= attrStr1;
效果图和上面一样.png
至此,花里胡哨的富文本点击事件设置完成