//初始化
UITextView *textView = [[[UITextView alloc] init] autorelease];
//设置代理 需在interface中声明UITextViewDelegate
textView.delegate = self;
//字体大小
textView.font = [UIFont systemFontOfSize:16];
//添加滚动区域
textView.contentInset = UIEdgeInsetsMake(-11, -6, 0, 0);
//是否可以滚动
textView.scrollEnabled = NO;
//获得焦点
[textView becomeFirstResponder];
//添加到视图上
[self.view addSubview:textView];
二、键盘操作
//返回键的类型
textView.returnKeyType = UIReturnKeyDefault;
//键盘类型
textView.keyboardType = UIKeyboardTypeDefault;
个人还是认为最方便的是在键盘上加上一个ToolBar,在上面加上一个按钮来隐藏键盘
①在键盘上加上隐藏按钮
/定义一个toolBar
UIToolbar * topView = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 30)];
//设置style
[topView setBarStyle:UIBarStyleBlack];
//定义两个flexibleSpace的button,放在toolBar上,这样完成按钮就会在最右边
UIBarButtonItem * button1 =[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:self action:nil];
UIBarButtonItem * button2 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:self action:nil];
//定义完成按钮
UIBarButtonItem * doneButton = [[UIBarButtonItem alloc]initWithTitle:@"完成"style:UIBarButtonItemStyleDone
target:self
action:@selector(resignKeyboard)];
//在toolBar上加上这些按钮
NSArray * buttonsArray = [NSArray arrayWithObjects:button1,button2,doneButton,nil];
[topView setItems:buttonsArray];
[textView setInputAccessoryView:topView];
//隐藏键盘
- (void)resignKeyboard {
[textView resignFirstResponder];
}
还有几种也可隐藏键盘的方式
②用回车键,前提是你的textView中不需要用到回车键
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
if([text isEqualToString:@"\n"]){
[textView resignFirstResponder]; returnNO;
}
returnYES;
}
③触摸空白处隐藏键盘
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{ //隐藏键盘
[textView resignFirstResponder];
}
//设置textView圆角
[self.textView.layer setCornerRadius:10];
①、在viewWillAppear中添加键盘监听事件
//添加键盘的监听事件
//注册通知,监听键盘弹出事件
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardDidShow:)
name:UIKeyboardDidShowNotification object:nil];
//注册通知,监听键盘消失事件
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardDidHidden)
name:UIKeyboardDidHideNotification
object:nil];
②、完成①selector中键盘弹出keyboardDidShow:和消失keyboardDidHidden方法
在.m文件#import后面添加
//动画时间
#define kAnimationDuration 0.2
//view高度
#define kViewHeight 56
键盘出现
// 键盘弹出时
-(void)keyboardDidShow:(NSNotification *)notification
{
//获取键盘高度
NSValue *keyboardObject = [[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect;
[keyboardObject getValue:&keyboardRect];
//调整放置有textView的view的位置
//设置动画
[UIView beginAnimations:nil context:nil];
//定义动画时间
[UIView setAnimationDuration:kAnimationDuration];
//设置view的frame,往上平移
[(UIView *)[self.view viewWithTag:1000] setFrame:CGRectMake(0, self.view.frame.size.height-keyboardRect.size.height-kViewHeight, 320, kViewHeight)];
[UIView commitAnimations];
}
//键盘消失时
-(void)keyboardDidHidden{
//定义动画
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:kAnimationDuration];
//设置view的frame,往下平移
[(UIView *)[self.view viewWithTag:1000] setFrame:CGRectMake(0, self.view.frame.size.height-kViewHeight, 320, kViewHeight)];
[UIView commitAnimations];
}
PS:有网友遇到textView在ios7上出现编辑进入最后一行时光标消失,看不到最后一行,变成盲打,stackOverFlow网站上有大神指出,是ios7本身bug,加上下面一段代码即可(网友调试得出,在此mark一下,有问题,欢迎大神们指出)
-(void)textViewDidChange:(UITextView *)textView {
CGRect line = [textView caretRectForPosition:
textView.selectedTextRange.start];
CGFloat overflow = line.origin.y + line.size.height
- ( textView.contentOffset.y + textView.bounds.size.height
- textView.contentInset.bottom - textView.contentInset.top );
if( overflow > 0 ) {
// We are at the bottom of the visible text and introduced a line feed, scroll down (iOS 7 does not do it)
// Scroll caret to visible area
CGPoint offset = textView.contentOffset;
offset.y += overflow + 7; // leave 7 pixels margin
// Cannot animate with setContentOffset:animated: or caret will not appear
[UIView animateWithDuration:.2 animations:^{
[textView setContentOffset:offset];
}];
}
}
UITextView自定选择文字后的菜单
UIMenuItem *menuItem = [[UIMenuItem alloc]initWithTitle:@"分享到新浪微博" action:@selector(changeColor:)];
UIMenuController *menu = [UIMenuController sharedMenuController];
[menu setMenuItems:[NSArray arrayWithObject:menuItem]];
[menuItem release];
当然上面那个@selector里面的changeColor方法还是自己写吧,也就是说点击了我们自定义的菜单项后会触发的方法。
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender{
if(action == @selector(changeColor:)){
if(textView.selectedRange.length>0)
return YES;
}
return NO;
}
实现后如下图:
UITextView 自动调整高度
这里主要记录一下 textview 的自动调整高度的代码
方法二和三比较可以,方法一在中文后面不断输入数字时 会换行显示之后就算不准,输入一般的字符也是挺符合要求
- (void)textViewDidChange:(UITextView *)textView{
方法一
if(textView.text.length > 20)//一行最多多少字节
{
//TextView底面背景图片根据内容自动调整高度
//UIImage *img = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"inputbox" ofType:@"png"]];
//[BgImage setImage:[img stretchableImageWithLeftCapWidth:21 topCapHeight:14]];
UIFont *font = [UIFont systemFontOfSize:15];
CGSize size=
[textView.text sizeWithFont:font constrainedToSize:CGSizeMake(300, 1000) lineBreakMode:UILineBreakModeCharacterWrap];
//BgImage.frame = CGRectMake(0, 202-size.height+15, 320, size.height+28);
[textView setFrame:CGRectMake(0, 1, 320, size.height+10)];
}
方法二
CGRect frame = textView.frame;
CGSize size = [textView.text sizeWithFont:textView.font
constrainedToSize:CGSizeMake(280, 1000)
lineBreakMode:UILineBreakModeTailTruncation];
frame.size.height = size.height > 1 ? size.height + 20 : 64;
textView.frame = frame;
方法三
CGRect frame = textView.frame;
frame.size.height = textView.contentSize.height;
textView.frame = frame;
}