UITextView

195 阅读1分钟
#import "ViewController.h"

@interface ViewController ()<UITextViewDelegate>

@property (nonatomic,strong) UITextView *textView;

@end


@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    UITextView *textView  = [[UITextView alloc] initWithFrame:CGRectMake(20, 100, self.view.bounds.size.width - 20, 44)];
    textView.delegate = self;
    textView.editable = NO;
    textView.scrollEnabled = NO;
    self.textView = textView;
    NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:@"客服电话:400-775-5333 转4 (每天9:00-20:00)"];
    [attr setAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor],NSFontAttributeName:[UIFont systemFontOfSize:14]} range:NSMakeRange(0, attr.length)];
    //点击了链接拨打电话
    [attr setAttributes:@{NSForegroundColorAttributeName:[UIColor colorWithRed:0 green:147/255.0 blue:238/255.0 alpha:1],
                          NSFontAttributeName:[UIFont systemFontOfSize:14],
                          NSLinkAttributeName:@"dianji"}
     //如果点击了之后跳转网页可以将NSLinkAttributeName后面的修改为想要跳转的网页
                  range:NSMakeRange(5, 15)];
    textView.attributedText = attr;
    [self.view addSubview:textView];
}

#pragma mark **- 实现链接代理**
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange interaction:(UITextItemInteraction)interaction {
    if ([URL.absoluteString isEqualToString:@"dianji"]) {
        NSLog(@"~~~~~~~~~~~~~~~");
    }
    return YES;
}

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
    // 禁用文本复制、粘贴
    return NO;
}

@end

simulator_screenshot_7F0445F5-9B61-4E76-AA31-D2B8CCFF9B79.png