#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...