#import "ViewController.h"
#define font 17
@interface ViewController ()<UITextViewDelegate>
@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
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- UITextField : 只能输入一行,不可以滚动,可以设置提醒文字。 UITextView: 能输入多行,可以...
- //初始化并定义大小 UITextView *textview = [[UITextView alloc] ini...