UITextView: move view when keyboard appears
For showing the textview fields without being hidden by the keyboard, the standard way is to move up/down the view having text fields whenever the keyboard is shown.
The following simple steps is used to move up/down the UITextField in your iPhone.
Step 1: Set the UITextViewDelegate, and tag the textViewFields (in your .h file)
Step 2: Place "CGFloat animatedDistance;" in to inside the interface (in your .h file)
Step 3: Define the following values in top of your .m file (after @implimentation)
static const CGFloat KEYBOARD_ANIMATION_DURATION = 0.3;
static const CGFloat MINIMUM_SCROLL_FRACTION = 0.2;
static const CGFloat MAXIMUM_SCROLL_FRACTION = 0.8;
static const CGFloat PORTRAIT_KEYBOARD_HEIGHT = 216;
static const CGFloat LANDSCAPE_KEYBOARD_HEIGHT = 162;
Step 4: And finally add the following code into your .m file
-(void) textViewDidBeginEditing:(UITextView *)textView {
CGRect textFieldRect = [self.view.window convertRect:textView.bounds fromView:textView];
CGRect viewRect = [self.view.window convertRect:self.view.bounds fromView:self.view];
CGFloat midline = textFieldRect.origin.y + 0.5 * textFieldRect.size.height;
CGFloat numerator = midline - viewRect.origin.y - MINIMUM_SCROLL_FRACTION * viewRect.size.height;
CGFloat denominator = (MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION) * viewRect.size.height;
CGFloat heightFraction = numerator / denominator;
if(heightFraction < 0.0){
heightFraction = 0.0;
}else if(heightFraction > 1.0){
heightFraction = 1.0;
}
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if(orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown){
animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction);
}else{
animatedDistance = floor(LANDSCAPE_KEYBOARD_HEIGHT * heightFraction);
}
CGRect viewFrame = self.view.frame;
viewFrame.origin.y -= animatedDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
}
- (void)textViewDidEndEditing:(UITextView *)textView{
CGRect viewFrame = self.view.frame;
viewFrame.origin.y += animatedDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
}
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
if([text isEqualToString:@"\n"])
{
[textView resignFirstResponder];
return NO;
}
return YES;
}
iphone - How to dismiss keyboard for UITextView with return key:
Did you saw the last para of the above code snippets?, that is used to hide the iphone keypad when clicking return key.
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
if([text isEqualToString:@"\n"])
{
[textView resignFirstResponder];
return NO;
}
return YES;
}
分享到:
相关推荐
UITextView:定制选中文字菜单和选择文字对应的Demo。
UITextView操作,代码细化
UITextView 实现UITextField功能
我们都知道iOS开发中的UITextField有个placeholder属性,placeholder可以很方便引导用户输入。但是UITextView却没有placeholder属性。 一、猥琐的方法 ...1.创建UITextView: 代码如下: UITextView *textViewPla
MyInputMethod MyInputMethod,我的输入法... ##参考 技术参考: iOS基础集合类: iOS-Core-Animation-Advanced-Techniques: 自定义控件: How To Make a Custom ...Undo typing in UITextView: Undo/redo with a UIText
带有palceholder的UITextView,只需引入LYTextView,然后进行调用即可,可以纯代码引入,也可以在XIB中引入,Demo中都有实例。 可以设定palceholder的4个参数,文字,字体,颜色,高度 改代码也可以自己修改满足自己...
使用UITextView来实现富文本编辑,实现:添加标题、加粗、斜体、下划线、删除线、图片等功能。UITextView富文本编辑主要通过NSMutableAttributedString和NSAttributedString进行属性设置,主要是计算好NSRange参数...
FOUNDATION_EXPORT double UITextView_PlaceholderVersionNumber; FOUNDATION_EXPORT const unsigned char UITextView_PlaceholderVersionString[]; @interface UITextView (Placeholder) /* 占位文字 */ @...
UITextView 和UILable 它本身是没有控制行高的。很多人说用Uiwebview,但我觉得那不好用,要用html布局麻烦,占内存。也有人说drawatpoint,但 发现画中文的时候也很占内存,我就投机取巧一下吧
UITextView的简单封装
自适应UITextView和键盘位置对应的Demo,解决在UITextView输入时被键盘挡住的问题。
UITextView +占位符 UITextView缺少一个占位符。 安装 使用 。 pod 'UITextView+Placeholder' 用法 导入动态框架: 例如,如果您将CocoaPods与use_frameworks!一起使用use_frameworks! 旗。 @import UITextView_...
可以输入默认Place提示的UITextView,对UITextView的扩展封装,使用简单
UITextView+placeholder,一行代码搞定
UItextView 的placeHodler属性封装
IOS没有原生的限制字数的输入控件,利用TextView封装一个
源码UITextViewDIYEmojiExample,该源码案例是一个UITextView编辑时插入自定义表情,也是一个比较简单的图文混编,在iOS开发中,经常需要用UITextView作为编辑文本的输入控件。但是如何在编辑时插入自定义表情呢?...
ios开发,uitextview的一个实例
SAMTextView, 向UITextView添加占位符 SAMTextView向UITextView添加占位符。SAMTextView在 iOS 6上测试,需要 arc 。 在 MIT许可协议下发布。用法// Initialize a text viewSAMTextView