ios 怎样让ios textview等待文字里的文字从最顶开始显示

iOS&的TextView的常规用法
一、新建一个textView
UITextView *textView = [[[UITextView alloc] init] autorelease];
//设置代理 需在interface中声明UITextViewDelegate
textView.delegate =
//字体大小
textView.font = [UIFont systemFontOfSize:16];
//添加滚动区域
textView.contentInset = UIEdgeInsetsMake(-11, -6, 0, 0);
//是否可以滚动
textView.scrollEnabled = NO;
//获得焦点
[textView becomeFirstResponder];
[self.view addSubview:textView];
二、键盘操作
//返回键的类型
textView.returnKeyType = UIReturnKeyD
//键盘类型
textView.keyboardType = UIKeyboardTypeD
三、隐藏键盘的几种方式
个人还是认为最方便的是在键盘上加上一个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];
<img src="/blog7style/images/common/sg_trans.gif" real_src ="http://static.oschina.net/uploads/space/1735_hlRQ_735123.png" ALT="" STYLE="margin: 0 display:"
TITLE="iOS&的TextView的常规用法" />
还有几种也可隐藏键盘的方式
&#9313;用回车键,前提是你的textView中不需要用到回车键
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
if ([text isEqualToString:@"\n"])
[textView resignFirstResponder]; return NO;
return YES;
&#9314;触摸空白处隐藏键盘
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
//隐藏键盘
[textView resignFirstResponder];
四、使键盘不挡住输入框
在view中添加一个子view,设置此子view的tag值为1000,在此view上添加一个textView和一个发送按钮,如下图;我们要达到textView的键盘弹出时,整个View往上平移,键盘消失,view往下平移的效果,模拟发送短信的界面。
<img src="/blog7style/images/common/sg_trans.gif" real_src ="http://static.oschina.net/uploads/space/0937_IaJo_735123.png" ALT="" STYLE="margin: 0 display:"
TITLE="iOS&的TextView的常规用法" />
设置textView圆角
//设置textView圆角
[self.textView.layer setCornerRadius:10];
&#9312;、在viewWillAppear中添加键盘监听事件
//添加键盘的监听事件
//注册通知,监听键盘弹出事件
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
//注册通知,监听键盘消失事件
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHidden) name:UIKeyboardDidHideNotification object:nil];
&#9313;、完成&#9312;selector中键盘弹出keyboardDidShow:和消失keyboardDidHidden方法
& & 在.m文件#import后面添加
//动画时间
#define kAnimationDuration 0.2
//view高度
#define kViewHeight 56
// 键盘弹出时
-(void)keyboardDidShow:(NSNotification *)notification
//获取键盘高度
NSValue *keyboardObject = [[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardR
[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];
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。android中textview设置为多行文本时,如何让文字从最顶开始显示
时间: 14:14:11
&&&& 阅读:122
&&&& 评论:
&&&& 收藏:0
标签:&&&&&&&&&&&&&&&&&&&&&&&&&&&
&span style="white-space:pre"&
&/span&&EditText
android:layout_width="match_parent"
android:layout_height="200dp"
android:inputType="textMultiLine"
android:textColor="#4c4d51"
android:background="@null"
android:gravity="left|top"
android:textSize="13sp"/&
加上Android:gravity="left|top"这句即可。标签:&&&&&&&&&&&&&&&&&&&&&&&&&&&原文:/blosaa/p/6261898.html
教程昨日排行
&&国之画&&&& &&&&&&
&& &&&&&&&&&&&&&&
鲁ICP备号-4
打开技术之扣,分享程序人生!

我要回帖

更多关于 ios textview文字置顶 的文章

 

随机推荐