UITextView笔记
来源:互联网 发布:全国数学建模大赛知乎 编辑:程序博客网 时间:2024/06/15 05:01
自定义UITextView,点击部分文字做事情.
//1.测试
#import "ViewController.h"
#import "MyTextView.h"
@interfaceViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
MyTextView *textView = [[MyTextViewalloc] init];
NSString *string =@"北方有佳人。绝世而独立。一顾倾人城。再顾倾人国。宁不知倾城与倾国。佳人难再得。";
NSString *subString =@"佳人难再得";
NSMutableAttributedString *attriString = [[NSMutableAttributedStringalloc] initWithString:string];
[attriString setAttributes:@{NSForegroundColorAttributeName:[UIColorblueColor]}
range:NSMakeRange(0, string.length - subString.length)];
NSRange range = [stringrangeOfString:subString];
textView.specialRange = range;
[attriString setAttributes:@{NSForegroundColorAttributeName:[UIColorredColor],
NSUnderlineStyleAttributeName:@(NSUnderlineStyleSingle)}
range:range];;
//统一设置字体大小,否则字体不正确
[attriString addAttribute:NSFontAttributeNamevalue:[UIFontsystemFontOfSize:16]range:NSMakeRange(0, string.length)];
CGFloat w =200;
CGSize size = [stringboundingRectWithSize:CGSizeMake(w,MAXFLOAT) options:NSStringDrawingUsesLineFragmentOriginattributes:@{NSFontAttributeName:[UIFontsystemFontOfSize:16]}context:nil].size;
textView.frame =CGRectMake(60,100, size.width, size.height);
//去除textView四周的间距,保证计算的frame正确
textView.backgroundColor = [UIColorlightGrayColor];
textView.attributedText = attriString;
textView.block = ^(){
NSLog(@"点了特殊文字");
};
[self.viewaddSubview:textView];
}
//2.自定义
#import <UIKit/UIKit.h>
typedefvoid(^specialBlock)();
@interface MyTextView :UITextView
/** 特殊文字的范围 */
@property (nonatomic,assign) NSRange specialRange;
@property (nonatomic,copy) specialBlock block;
@end
#import "MyTextView.h"
@implementation MyTextView
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [superinitWithFrame:frame]) {
self.backgroundColor = [UIColorclearColor];
self.editable =NO;
self.scrollEnabled=NO;
self.textContainerInset =UIEdgeInsetsMake(0, -5,0, -5);
}
returnself;
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touchesanyObject];
CGPoint point = [touchlocationInView:self];
self.selectedRange =NSMakeRange(0,0);
if ([selftouchingPointInPoint:point]) {
if (self.block) {
self.block();
}
}
}
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
return [selftouchingPointInPoint:point];
}
/**
* 触摸点是否处理(除特殊文字以外的都不处理)
*/
- (BOOL)touchingPointInPoint:(CGPoint)point
{
self.selectedRange =self.specialRange;
//选中范围的矩形框数组(换行就会有多个rect)
NSArray *rects = [selfselectionRectsForRange:self.selectedTextRange];
//清空选中范围(复制粘贴的范围)
self.selectedRange =NSMakeRange(0,0);
for (UITextSelectionRect *selectedRectin rects) {
CGRect rect = selectedRect.rect;
if (CGRectContainsPoint(rect, point)) {
returnYES;
}
}
returnNO;
}
- UITextView笔记
- UITextView使用笔记
- ios学习笔记-Lecture5:UITextView
- UITextView
- UITextView
- UITextView
- UITextView
- UITextView
- UITextView
- UITextView
- UITextView
- UITextView
- UITextView
- UITextView
- UITextView
- UITextView
- UITextView
- UITextView
- osx10.11 CocoaPods 正确安装
- stm32f103-gy86测试代码资源
- uboot编译学习----执行完make TQ2440_config后,在mkconfig 中
- cron配置详解
- 产品需求文档PRD的写作(五) – 用例文档(UML用例图、流程图)
- UITextView笔记
- 正确率、召回率和F值
- 数据结构_7:图算法 :图的最小生成树
- 【LeetCode-326】Power of three 下(C++)
- 详解C指针
- uboot编译学习----直接执行make all,提示编译失败
- 独家分享HP VC 模拟器
- 关于基类中函数修饰符abstract和virtual的使用
- Linux内核4.4版本带来的网络新特性