注意事項:需要點擊事件則必須使用textView來呈現(xiàn)文本稽犁。不需要的話可隨便使用?
#define XGWidth [UIScreen mainScreen].bounds.size.width
控制器內(nèi)代碼部分
UITextView *textView = [[UITextView alloc]initWithFrame:CGRectMake(0, 0, XGWidth,60)];
textView.text = @"用戶注冊代表同意《服務(wù)條款》和《隱私政策》"; 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
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? };
//設(shè)置富文本點擊
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:textView.text attributes:attributes];
[attributedString addAttribute:NSLinkAttributeName
?? ? ? ? ? ? ? ? ? ? ? ? ? ? value:@"fuwutiaokuan://"
?? ? ? ? ? ? ? ? ? ? ? ? ? ? range:[[attributedStringstring]rangeOfString:@"《服務(wù)條款》"]];
?[attributedString addAttribute:NSLinkAttributeName
?? ? ? ? ? ? ? ? ? ? ? ? ? ? value:@"yisizhengce://"
?? ? ? ? ? ? ? ? ? ? ? ? ? ? range:[[attributedStringstring]rangeOfString:@"《隱私政策》"]];
//設(shè)置字體
[attributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14] range:NSMakeRange(8,6)];
[attributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:16] range:NSMakeRange(15,6)];
//設(shè)置整體顏色
[attributedString addAttribute:NSForegroundColorAttributeName value:XGCOLORFromRGB(0x999999) range:NSMakeRange(0,textView.text.length)];
textView.attributedText= attributedString;
?//設(shè)置被點擊字體顏色
?textView.linkTextAttributes = @{NSForegroundColorAttributeName:XGCOLORFromRGB(0x666666)};
textView.delegate=self;
//必須禁止輸入嫂粟,否則點擊將彈出輸入鍵盤
textView.editable=NO; ? ? ? ?
?textView.scrollEnabled=NO;
代理方法的實現(xiàn)
#pragma mark 富文本點擊事件
- (BOOL)textView:(UITextView*)textView shouldInteractWithURL:(NSURL*)URL inRange:(NSRange)characterRange {
? ? if ([[URL scheme] isEqualToString:@"fuwutiaokuan"]) {
? ? ? ? NSLog(@"富文本點擊 服務(wù)條款");
? ? }else if ([[URL scheme] isEqualToString:@"yisizhengce"]) {
? ? ? ? NSLog(@"富文本點擊 隱私政策");
? ? }
? ? return YES;
}
效果圖
二蝶棋、使用lable實現(xiàn) (沒有點擊事件)
UILabel*agreeLab = [[UILabel alloc]initWithFrame:CGRectMake(0,0,XGWidth,60)];
agreeLab.text = @"用戶注冊代表同意《服務(wù)條款》和《隱私政策》";
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)];
//設(shè)置字體
[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
至此溶诞,花里胡哨的富文本點擊事件設(shè)置完成