textView的布局和点击事件
2015-11-18 14:17
253 查看
// - 设置 textView 不可以编辑
textView.editable =NO;
// - 设置文字的内边距
textView.textContainerInset =UIEdgeInsetsMake(0,
-5,0, -5);
// - 设置 textView 不可以滚动
textView.scrollEnabled =NO;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//
触摸对象
UITouch *touch = [touches
anyObject];
//
触摸点
CGPoint point = [touch
locationInView:self];
NSArray *specials = [self.attributedTextattribute:@"specials"atIndex:0effectiveRange:NULL];
BOOL contains =
NO;
for (HWSpecial *specialin specials) {
self.selectedRange = special.range;
// self.selectedRange --影响--> self.selectedTextRange
//
获得选中范围的矩形框
NSArray *rects = [selfselectionRectsForRange:self.selectedTextRange];
//
清空选中范围
10d8a
self.selectedRange =NSMakeRange(0,0);
for (UITextSelectionRect *selectionRectin rects) {
CGRect rect = selectionRect.rect;
if (rect.size.width ==0 || rect.size.height
==0) continue;
if (CGRectContainsPoint(rect, point)) {//
点中了某个特殊字符串
contains = YES;
break;
}
}
if (contains) {
for (UITextSelectionRect *selectionRectin rects) {
CGRect rect = selectionRect.rect;
if (rect.size.width ==0 || rect.size.height
==0) continue;
UIView *cover = [[UIViewalloc]
init];
cover.backgroundColor = [UIColorgreenColor];
cover.frame = rect;
cover.tag =
HWStatusTextViewCoverTag;
cover.layer.cornerRadius =5;
[self
insertSubview:cover atIndex:0];
}
break;
}
}
// 在被触摸的特殊字符串后面显示一段高亮的背景
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25
* NSEC_PER_SEC)),dispatch_get_main_queue(), ^{
[self touchesCancelled:toucheswithEvent:event];
});
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
// 去掉特殊字符串后面的高亮背景
for (UIView *childin
self.subviews) {
if (child.tag ==HWStatusTextViewCoverTag) [child
removeFromSuperview];
}
}
textView.editable =NO;
// - 设置文字的内边距
textView.textContainerInset =UIEdgeInsetsMake(0,
-5,0, -5);
// - 设置 textView 不可以滚动
textView.scrollEnabled =NO;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//
触摸对象
UITouch *touch = [touches
anyObject];
//
触摸点
CGPoint point = [touch
locationInView:self];
NSArray *specials = [self.attributedTextattribute:@"specials"atIndex:0effectiveRange:NULL];
BOOL contains =
NO;
for (HWSpecial *specialin specials) {
self.selectedRange = special.range;
// self.selectedRange --影响--> self.selectedTextRange
//
获得选中范围的矩形框
NSArray *rects = [selfselectionRectsForRange:self.selectedTextRange];
//
清空选中范围
10d8a
self.selectedRange =NSMakeRange(0,0);
for (UITextSelectionRect *selectionRectin rects) {
CGRect rect = selectionRect.rect;
if (rect.size.width ==0 || rect.size.height
==0) continue;
if (CGRectContainsPoint(rect, point)) {//
点中了某个特殊字符串
contains = YES;
break;
}
}
if (contains) {
for (UITextSelectionRect *selectionRectin rects) {
CGRect rect = selectionRect.rect;
if (rect.size.width ==0 || rect.size.height
==0) continue;
UIView *cover = [[UIViewalloc]
init];
cover.backgroundColor = [UIColorgreenColor];
cover.frame = rect;
cover.tag =
HWStatusTextViewCoverTag;
cover.layer.cornerRadius =5;
[self
insertSubview:cover atIndex:0];
}
break;
}
}
// 在被触摸的特殊字符串后面显示一段高亮的背景
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25
* NSEC_PER_SEC)),dispatch_get_main_queue(), ^{
[self touchesCancelled:toucheswithEvent:event];
});
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
// 去掉特殊字符串后面的高亮背景
for (UIView *childin
self.subviews) {
if (child.tag ==HWStatusTextViewCoverTag) [child
removeFromSuperview];
}
}
相关文章推荐
- Leetcode161: Repeated DNA Sequences
- shell 中数学计算总结
- 史上最全设计模式导学目录(完整版)
- JVM 堆栈区域数据存放流程
- 源码编译git@osc
- 第 12 章 剪贴板
- 导航菜单指示当前页脚本
- C#数据库编程---事务回滚
- [Elasticsearch] 分布式文档存储
- 使用imageview显示Gif图片
- 软键盘的显示问题 imeOptions
- 多线程调试必杀技 - GDB的non-stop模式
- 网站前端素材网站
- oracle 11g ctss 集群时间同步服务几个要点
- Cocos2dx-OpenGL ES2.0教程:初识MVP(3)
- windows 远程安全连接
- mongodb基本使用
- 关于sql语句,select查询时很快,加了top + 数字之后就会很慢
- s5pc100开发板linux内核移植
- (转载)Cocos2dx-OpenGL ES2.0教程:初识MVP(3)