iOS-UITextView富文本点击事件
2018-11-20 本文已影响3人
fly大梦想家
uitextview文本点击.gif
NSString *policyPromptStr = @"I agree to the Terms of Use and Privacy Policy.";
NSDictionary *attributes = @{NSForegroundColorAttributeName:[UIColor blackColor]};
NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:policyPromptStr attributes:attributes];
[attStr addAttributes:@{NSForegroundColorAttributeName :[UIColor redColor],NSUnderlineStyleAttributeName:@(NSUnderlineStyleSingle),NSLinkAttributeName:@"Terms of Use"} range:NSMakeRange(15, 12)];
[attStr addAttributes:@{NSForegroundColorAttributeName :[UIColor redColor],NSUnderlineStyleAttributeName:@(NSUnderlineStyleSingle),NSLinkAttributeName:@"Privacy Policy"} range:NSMakeRange(32, 14)];
UITextView *policyTextView = [[UITextView alloc] initWithFrame:CGRectMake(20,100,260,30)];
policyTextView.editable = NO;
policyTextView.scrollEnabled = NO;
policyTextView.attributedText = attStr;
policyTextView.delegate = self;
policyTextView.textContainerInset = UIEdgeInsetsMake(5, 0,0,0);
[self.view addSubview:policyTextView];
代理里执行事件
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange{
if (characterRange.location == 15 && characterRange.length == 12) {
NSLog(@"Terms of use was clicked");
}else if (characterRange.location == 32 && characterRange.length == 14){
NSLog(@"Privacy Policy was clicked");
}
return NO;
}