登录界面经常使用 “用户协议”,“安全使用说明”等链接文本说明,下面是实现方式。
使用UITextView,在文本中加入链接,并设置连接的颜色等
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"点击登录,即表示您同意《用户使用协议》"];
//设置行间距以及字体大小、颜色
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = 4;// 字体的行间距
NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:12.0],
NSForegroundColorAttributeName:[UIColor colorWithHex:@"CFCFCF"],
NSParagraphStyleAttributeName:paragraphStyle};
[attributedString setAttributes:attributes range:NSMakeRange(0, attributedString.length)];
[attributedString addAttribute:NSLinkAttributeName
value:@"action://"
range:[[attributedString string] rangeOfString:@"《用户使用协议》"]];
NSDictionary *linkAttributes = @{NSForegroundColorAttributeName:[UIColor colorWithHex:@"333333"]};
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(22,100, CGRectGetWidth(self.view.frame)-44, 65)];
textView.backgroundColor = [UIColor clearColor];
textView.linkTextAttributes = linkAttributes;
textView.attributedText = attributedString;
textView.scrollEnabled = NO;
textView.font = [UIFont systemFontOfSize:12.0];
textView.textAlignment = NSTextAlignmentCenter;
textView.editable = NO;
textView.delegate = self;
[self.view addSubview:textView];
实现代理方法,监听点击“《用户使用协议》”点击事件
#pragma mark -
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange
{
//《用户使用协议》
if ([URL.absoluteString isEqualToString:@"action://"])
{
return NO;
}
return YES;
}