本文转载至 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. python 保留两位小数方法

    原博客连接:https://blog.csdn.net/Jerry_1126/article/details/85009810 保留两位小数,并做四舍五入处理 方法一:使用字符串格式化 a = 12. ...

  2. docker下使用DB2

    1.查询可安装的db2镜像 benjamin@docker:~$ docker images |grep -i db2 ibmcom/db2express-c latest 7aa154d9b73c ...

  3. extern "C"解析

    转自大牛的解析(非常具体详细)http://www.cnblogs.com/skynet/archive/2010/07/10/1774964.html 我做个简单的标注方便以后自己查看: 在用C++ ...

  4. 洛谷——P2205 [USACO13JAN]画栅栏Painting the Fence

    题目描述 Farmer John has devised a brilliant method to paint the long fence next to his barn (think of t ...

  5. [深入浅出iOS库]之数据库 sqlite

    一,sqlite 简介 前面写了一篇博文讲如何在 C# 中使用 ADO 访问各种数据库,在移动开发和嵌入式领域也有一个轻量级的开源关系型数据库-sqlite.它的特点是零配置(无需服务器),单磁盘文件 ...

  6. Android无需权限显示悬浮窗, 兼谈逆向分析app

    前言 最近UC浏览器中文版出了一个快速搜索的功能, 在使用其他app的时候, 如果复制了一些内容, 屏幕顶部会弹一个窗口, 提示一些操作, 点击后跳转到UC, 显示这个悬浮窗不需要申请android. ...

  7. MATLAB基础操作符与数据格式显示

    1.冒号":" 基本使用如下: X=1:10:表示生成向量[1,2,3,4,5,6,7,8,9,10] X=J:i:k ;表示向量[j,j+i,j+2i,...,k]; A(:,j ...

  8. span设置padding无效

    <span style="display:inline-block;padding-top:10px">测试<span> 给span加属性 display: ...

  9. DB2和MySQL常用SQL整理

    1.Truncate删除表中所有数据 truncate table USER immediate; 说明:Truncate是一个能够快速清空资料表内所有资料的SQL语法.并且能针对具有自动递增值的字段 ...

  10. 2017.2.12 开涛shiro教程-第七章-与Web集成

    2017.2.9 开涛shiro教程-第七章-与Web集成(一) 原博客地址:http://jinnianshilongnian.iteye.com/blog/2018398 根据下载的pdf学习. ...