本文转载至 http://wuchaorang.2008.blog.163.com/blog/static/48891852201232014813990/

 
 

1.创建并初始化

创建UITextView的文件,并在.h文件中写入如下代码:

[csharp] view plaincopy

 
  1. #import <UIKit/UIKit.h>
  2. @interface TextViewController : UIViewController <UITextViewDelegate>
  3. {
  4. UITextView *textView;
  5. }
  6. @property (nonatomic, retain) UITextView *textView;
  7. @end

在.m文件中初始化这个textview,写入代码如下:

[csharp] view plaincopy

 
  1. self.textView = [[[UITextView alloc] initWithFrame:self.view.frame]autorelease]; //初始化大小并自动释放
  2. self.textView.textColor = [UIColor blackColor];//设置textview里面的字体颜色
  3. self.textView.font = [UIFont fontWithName:@"Arial" size:18.0];//设置字体名字和字体大小
  4. self.textView.delegate = self;//设置它的委托方法
  5. self.textView.backgroundColor = [UIColor whiteColor];//设置它的背景颜色
  6. self.textView.text = @"Now is the time for all good developers tocome to serve their country.\n\nNow is the time for all good developers to cometo serve their country.";//设置它显示的内容
  7. self.textView.returnKeyType = UIReturnKeyDefault;//返回键的类型
  8. self.textView.keyboardType = UIKeyboardTypeDefault;//键盘类型
  9. self.textView.scrollEnabled = YES;//是否可以拖动
  10. self.textView.autoresizingMask = UIViewAutoresizingFlexibleHeight;//自适应高度
  11. [self.view addSubview: self.textView];//加入到整个页面中

2. UITextView退出键盘的几种方式

因为你点击UITextView会出现键盘,如果你退出键盘,有如下几种方式:

(1)如果你程序是有导航条的,可以在导航条上面加多一个Done的按钮,用来退出键盘,当然要先实UITextViewDelegate。

代码如下:

[csharp] view plaincopy

 
  1. - (void)textViewDidBeginEditing:(UITextView *)textView {
  2. UIBarButtonItem *done =    [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(leaveEditMode)] autorelease];
  3. self.navigationItem.rightBarButtonItem = done;
  4. }
  5. - (void)textViewDidEndEditing:(UITextView *)textView {
  6. self.navigationItem.rightBarButtonItem = nil;
  7. }
  8. - (void)leaveEditMode {
  9. [self.textView resignFirstResponder];
  10. }

(2)如果你的textview里不用回车键,可以把回车键当做退出键盘的响应键。
代码如下:

[csharp] view plaincopy

 
  1. #pragma mark - UITextView Delegate Methods
  2. -(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
  3. {
  4. if ([text isEqualToString:@"\n"]) {
  5. [textView resignFirstResponder];
  6. return NO;
  7. }
  8. return YES;
  9. }

这样无论你是使用电脑键盘上的回车键还是使用弹出键盘里的return键都可以达到退出键盘的效果。

(3)还有你也可以自定义其他加载键盘上面用来退出,比如在弹出的键盘上面加一个view来放置退出键盘的Done按钮。

代码如下:

[csharp] view plaincopy

 
  1. UIToolbar * topView = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 30)];
  2. [topView setBarStyle:UIBarStyleBlack];
  3. UIBarButtonItem * helloButton = [[UIBarButtonItem alloc]initWithTitle:@"Hello" style:UIBarButtonItemStyleBordered target:self action:nil];
  4. UIBarButtonItem * btnSpace = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
  5. UIBarButtonItem * doneButton = [[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(dismissKeyBoard)];
  6. NSArray * buttonsArray = [NSArray arrayWithObjects:helloButton,btnSpace,doneButton,nil];
  7. [doneButton release];
  8. [btnSpace release];
  9. [helloButton release];
  10. [topView setItems:buttonsArray];
  11. [tvTextView setInputAccessoryView:topView];
  12. -(IBAction)dismissKeyBoard
  13. {
  14. [tvTextView resignFirstResponder];
  15. }

(4)设置UITextView圆角问题

做法是在 #import QuartzCore/QuartzCore.h 后,便能調用[textView.layer setCornerRadius:10]; 來把 UITextView 设定圓角

(5)UITextView根据文本大小自适应高度

通过实现文本字数来确定高度,如下:

[csharp] view plaincopy

 
  1. NSString * desc = @"Description it is  a test font, and don't become angry for which i use to do here.Now here is a very nice party from american or not!";
  2. CGSize  size = [desc sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(240, 2000) lineBreakMode:UILineBreakModeWordWrap];

只有UILabel需要定义的numberoflines为0,即不做行数的限制。如下:

[csharp] view plaincopy

 
  1. [label  setNumberOfLines:0];
  2. [label  setFrame:CGRectMake(40, 135, 240, size.height+10)];
  3. [label setText:desc];

(6)UITextView自定选择文字后的菜单

在ViewDidLoad中加入:

[csharp] view plaincopy

 
  1. UIMenuItem *menuItem = [[UIMenuItem alloc]initWithTitle:@"分享到新浪微博" action:@selector(changeColor:)];
  2. UIMenuController *menu = [UIMenuController sharedMenuController];
  3. [menu setMenuItems:[NSArray arrayWithObject:menuItem]];
  4. [menuItem release];

当然上面那个@selector里面的changeColor方法还是自己写吧,也就是说点击了我们自定义的菜单项后会触发的方法。

然后还得在代码里加上一个方法:

[csharp] view plaincopy

 
  1. -(BOOL)canPerformAction:(SEL)action withSender:(id)sender
  2. {
  3. if(action ==@selector(changeColor:))
  4. {
  5. if(textView.selectedRange.length>0)
  6. return YES;
  7. }
  8. return NO;
  9. }

实现后如下图:

今天的UITextView就讲到这里,主要讲了UITextView的初始化和开发中会遇到的一些问题和自定义等问题。谢谢大家支持哈。

更多:

http://www.cnblogs.com/likwo/archive/2010/05/29/1747117.html

http://www.cnblogs.com/nasa/archive/2010/10/14/1851505.html

http://www.cnblogs.com/otionsky/archive/2011/11/18/ios_disable_cut_copy_paste_replace.html

一步一步学ios UITextView(多行文本框)控件的用法详解(五5.8)的更多相关文章

  1. iOS:提示框(警告框)控件UIActionSheet的详解

    提示框(警告框)控件2:UIActionSheet 功能:当点击按钮或标签等时,弹出一个提示框,显示必要的提示,然后通过添加的按钮完成需要的功能.它与导航栏类似,它继承自UIView.   风格类型: ...

  2. IOS中UITextView(多行文本框)控件的简单用法

    1.创建并初始化 UITextView文本视图相比与UITextField直观的区别就是UITextView可以输入多行文字并且可以滚动显示浏览全文.UITextField的用处多,UITextVie ...

  3. iOS:提示框(警告框)控件UIAlertView的详解

    提示框(警告框)控件:UIAlertView   功能:当点击按钮或标签等时,弹出一个提示框,显示必要的提示,然后通过添加的按钮完成需要的功能.   类型:typedef NS_ENUM(NSInte ...

  4. iOS:图像选取器控制器控件UIImagePickerController的详解

    图像选择控制器:UIImagePickerController 功能:用于选取相册或相机等里面的照片. @interface UIImagePickerController : UINavigatio ...

  5. iOS:选择器控件UIPickerView的详解和演示

    选择器控件UIPickerView: 功能:它能够创建一个类似于密码锁式的单列或多列的选择菜单,用户可以通过它设置的代理来选择需要菜单中的任意的数据.例如创建日历.字体表(类型.大小.颜色).图库等. ...

  6. iOS:下拉刷新控件UIRefreshControl的详解

    下拉刷新控件:UIRefreshControl 1.具体类信息: @interface UIRefreshControl : UIControl //继承控制类 - (instancetype)ini ...

  7. iOS:网页视图控件UIWebView的详解

    网页视图控件:UIWebView 功能:它是继承于UIView的,是一个内置的浏览器控件,以用来浏览从网络下载下来的网页或者本地上加载下来的文档. 枚举: //网页视图导航类型 typedef NS_ ...

  8. iOS开发UI篇—手写控件,frame,center和bounds属性

    iOS开发UI基础—手写控件,frame,center和bounds属性 一.手写控件 1.手写控件的步骤 (1)使用相应的控件类创建控件对象 (2)设置该控件的各种属性 (3)添加控件到视图中 (4 ...

  9. iOS开发UI基础—手写控件,frame,center和bounds属性

    iOS开发UI基础—手写控件,frame,center和bounds属性 一.手写控件 1.手写控件的步骤 (1)使用相应的控件类创建控件对象 (2)设置该控件的各种属性 (3)添加控件到视图中 (4 ...

随机推荐

  1. uva 1442:Cave(贪心)

    题意:一个洞穴长n,告诉你每个位置的地面高度和顶部高度,让你往里灌水,要求水不能碰到天花板(但可以无限接近).求最多的水量.(洞穴两边视为封闭) 思路:如果知道一个位置向左看最高可以多高,向右看最高可 ...

  2. NOIP 2016 天天爱跑步 80分暴力

    题目描述 小c同学认为跑步非常有趣,于是决定制作一款叫做<天天爱跑步>的游戏.«天天爱跑步»是一个养成类游戏,需要玩家每天按时上线,完成打卡任务. 这个游戏的地图可以看作一一棵包含 个结点 ...

  3. iOS数组去重的方法,超级简单

    //最近新发现的一个数组去重,用不着循环,一句代码搞定 //去除数组中重复的 NSArray *oldArr = @[@"1",@"2",@"3&qu ...

  4. Google的新一代一致性哈希算法

    先看到了一篇中文文章:谷歌退出有界负载的一致性哈希算法 然后找到了它对应的英文原文 以及它的英文paper,24页 又找到一篇博客,发现已经有人将它在haproxy上实现了,haproxy1.7.5上 ...

  5. C# ASP.NET中Process.Start没有反应也没有报错的解决方法

    最近有一个很坑的需求,在ASP.NET中打开一个access,还要用process.start打开,调试时一切正常,到了发布后就没有反应,找了一下午,各种设文件夹权限也不行,最后把应用程序池改成管理员 ...

  6. 伪全栈工程师做的有点简陋的ui设计

    站酷:http://www.zcool.com.cn/work/ZMjEwMDIxMDA=.html 这个app 叫自我时间管理  是一个 工具  管理自己开会 购物 健身 记账等 的提醒与管理,还可 ...

  7. UIScrollView/UITableView 一直显示滚动条(ScrollBar Indicators)、滚动条Width(宽度)、滚动条Color(颜色)

    在 IOS 中,对 UIScrollView 的滚动条(ScrollBar Indicators)的自定义设置接口,一直都是很少的.除了能自定义简单的样式(UIScrollViewIndicatorS ...

  8. IOS开发~开机启动&无限后台运行&监听进程

    非越狱情况下实现: 开机启动:App安装到IOS设备设备之后,无论App是否开启过,只要IOS设备重启,App就会随之启动: 无限后台运行:应用进入后台状态,可以无限后台运行,不被系统kill: 监听 ...

  9. ThinkPHP示例:CURD

    完整的控制器文件: class IndexAction extends Action { // 查询数据 public function index() { $Form = M("Form& ...

  10. Error Code: 1055 incompatible with sql_mode=only_full_group_by

    OperationalError at / (1055, "Expression #1 of ORDER BY clause is not in GROUP BY clause and co ...