设为首页 加入收藏

TOP

Core Text实现编辑的时候会用到的(二)
2014-11-23 21:45:50 来源: 作者: 【 】 浏览:12
Tags:Core Text 实现 编辑 时候 用到
t.y, endPoint.x - startPoint.x, endPoint.y - startPoint.y);
self.completedPath = [UIBezierPath bezierPathWithRect:rect];
// draw a shaded area over the entire view, so that the user can
// easily see where to focus their attention.
UIView *backgroundShade = [[[UIView alloc] initWithFrame:touchedView.bounds] autorelease];
backgroundShade.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.5];
backgroundShade.tag = SHADE_TAG;
backgroundShade.userInteractionEnabled = NO;
[touchedView addSubview:backgroundShade];
// now comes the fun part. we make a temporary UITextView for the
// actual text input.
UITextView *textView = [[[UITextView alloc] initWithFrame:rect] autorelease];
textView.font = delegate.font;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
// in case the chosen view is going to be below the keyboard, we need to
// make an effort to determine how far the display area should slide when
// the keyboard is going to be shown.
//
// keyboard heights:
//
// 352 landscape
// 264 portrait
CGFloat keyboardHeight = 0;
UIInterfaceOrientation orientation = ((UIViewController*)delegate).interfaceOrientation;
if (UIInterfaceOrientationIsPortrait(orientation)) {
keyboardHeight = 264;
} else {
keyboardHeight = 352;
}
CGRect viewBounds = touchedView.bounds;
CGFloat rectMaxY = rect.origin.y + rect.size.height;
CGFloat availableHeight = viewBounds.size.height - keyboardHeight;
if (rectMaxY > availableHeight) {
// calculate a slide distance so that the dragged box is centered vertically
viewSlideDistance = rectMaxY - availableHeight;
} else {
viewSlideDistance = 0;
}
textView.delegate = self;
[touchedView addSubview:textView];
textView.editable = NO;
textView.editable = YES;
[textView becomeFirstResponder];
}
}
}
首先 得到起点,终点,然后清空数组
if (distanceBetween(startPoint, endPoint) < 50.0) return; 进行了一个判断, 防止用户画的矩形太小,影响输入文字,
如果大小没问题,接下来 便将最终的矩形路径 赋值到 self.completedPath 在退出此方法后 进入drawTemporary后就会进入if分支
接着画出 灰色的背景, 此处的设置透明度为0.5, 并且为背景view设置tag为SHADE_TAG 这个标记用来在以后 编辑完成的时候通过 viewWithTag方法 找到这个背景的实例 并且 移除它
接下来便开始创建textView 其frame就是 刚在最终得到的矩形路径的位置信息
接着注册了一些通知, 即在键盘弹出,或者落下的时候 调用的几个方法
下面的一部分代码实现的功能是 如果键盘弹出 可能会挡住 textView 判断 是否挡住了 如果挡住了 需要向上移动多少距离,并且将这个距离 赋值给全局的 变量viewSlideDistance 如果没有挡住 设置为0
最后将textView显示出来,并且通过[textView becomeFirstResponder]; 在其刚显示的时候 马上成为第一响应者,弹出键盘
下面两个方法在键盘弹出之前以及落下之前,调用 移动视图,防止textView 被挡
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
2
首页 上一页 1 2 3 4 下一页 尾页 2/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇C常见问题之exit 下一篇C语言变参函数的编写

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: