UITextView

#import "ViewController.h"
#define font 17
@interface ViewController ()
@property (weak, nonatomic) UITextView *textview;
@property (assign, nonatomic) BOOL isSelect;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self protocolIsSelect:self.isSelect];
}
- (void)protocolIsSelect:(BOOL)select {
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"这里写文字"];
    _textview = [[UITextView alloc] init];
    UIImage *image = [UIImage imageNamed:@"icon_is"];
    CGSize size = CGSizeMake(font + 2, font + 2);
    UIGraphicsBeginImageContextWithOptions(size, false, 0);
    [image drawInRect:CGRectMake(0, 2, size.width, size.height)];
    UIImage *resizeImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init];
    textAttachment.image = resizeImage;
    NSMutableAttributedString *imageString = [NSMutableAttributedString attributedStringWithAttachment:textAttachment];
    [imageString addAttribute:NSLinkAttributeName
                        value:@"checkbox://"
                        range:NSMakeRange(0, imageString.length)];
    [attributedString insertAttributedString:imageString atIndex:23];
    [attributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:font] range:NSMakeRange(0, attributedString.length)];
    _textview.attributedText = attributedString;
    _textview.linkTextAttributes = @{NSForegroundColorAttributeName: [UIColor blueColor],
                                     NSUnderlineColorAttributeName: [UIColor lightGrayColor],
                                     NSUnderlineStyleAttributeName: @(NSUnderlinePatternSolid)};
    _textview.delegate = self;
    _textview.editable = NO;      
    _textview.scrollEnabled = NO;
    
    _textview.frame = CGRectMake(100, 100, 280, 100);
    [self.view addSubview:_textview];
}

- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange {
    if ([[URL scheme] isEqualToString:@"checkbox"]) {
        NSLog(@"图片");
        //        self.isSelect = !self.isSelect;
        //        [self protocolIsSelect:self.isSelect];
        return NO;
    }
    return YES;
}
@end

你可能感兴趣的:(UITextView)