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

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

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

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

代码如下:

 
- (void)textViewDidBeginEditing:(UITextView *)textView {  
 
   UIBarButtonItem *done =    [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(leaveEditMode)] autorelease];  
 
   self.navigationItem.rightBarButtonItem = done;      
 
}
 
- (void)textViewDidEndEditing:(UITextView *)textView {  
 
    self.navigationItem.rightBarButtonItem = nil;  
 
}
 
- (void)leaveEditMode {  
 
    [self.textView resignFirstResponder];  
 
}

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

 
#pragma mark - UITextView Delegate Methods   
 
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text  
{  
    if ([text isEqualToString:@"\n"]) {  
        [textView resignFirstResponder];  
        return NO;  
    }  
    return YES;  
}

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

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

代码如下:

 
UIToolbar * topView = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 30)];  
    [topView setBarStyle:UIBarStyleBlack];  
    UIBarButtonItem * helloButton = [[UIBarButtonItem alloc]initWithTitle:@"Hello" style:UIBarButtonItemStyleBordered target:self action:nil];        
    UIBarButtonItem * btnSpace = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];  
    UIBarButtonItem * doneButton = [[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(dismissKeyBoard)];  
    NSArray * buttonsArray = [NSArray arrayWithObjects:helloButton,btnSpace,doneButton,nil];  
    [doneButton release];  
    [btnSpace release];  
    [helloButton release];  
    [topView setItems:buttonsArray];  
    [tvTextView setInputAccessoryView:topView];  
-(IBAction)dismissKeyBoard  
{  
    [tvTextView resignFirstResponder];  
}

(4)设置UITextView圆角问题

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

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

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

 
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!";  
CGSize  size = [desc sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(240, 2000) lineBreakMode:UILineBreakModeWordWrap];

然后需要定义UITextView的numberoflines为0,即不做行数的限制。如下:

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

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

在ViewDidLoad中加入:

 
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详解的更多相关文章

  1. UITextView 详解

    UITextView 边框的设置   设置光标的位置   导入QuartzCote框架: #import <QuartzCore/QuartzCore.h> textView.layer. ...

  2. UITextView的使用详解

    //初始化并定义大小 UITextView *textview = [[UITextView alloc] initWithFrame:CGRectMake(20, 10, 280, 30)]; te ...

  3. 【转】UITextView的使用详解

    //初始化并定义大小 UITextView *textview = [[UITextView alloc] initWithFrame:CGRectMake(20, 10, 280, 30)]; te ...

  4. 【iOS 开发】基本 UI 控件详解 (UIButton | UITextField | UITextView | UISwitch)

    博客地址 : http://blog.csdn.net/shulianghan/article/details/50051499 ; 一. UI 控件简介 1. UI 控件分类 UI 控件分类 : 活 ...

  5. iOS中 HTTP/Socket/TCP/IP通信协议详解

    // OSI(开放式系统互联), 由ISO(国际化标准组织)制定 // 1. 应用层 // 2. 表示层 // 3. 会话层 // 4. 传输层 // 5. 网络层 // 6. 数据链接层 // 7. ...

  6. iOS应用开发详解

    <iOS应用开发详解> 基本信息 作者: 郭宏志    出版社:电子工业出版社 ISBN:9787121207075 上架时间:2013-6-28 出版日期:2013 年7月 开本:16开 ...

  7. IOS 触摸事件分发机制详解

    欢迎大家前往云+社区,获取更多腾讯海量技术实践干货哦~ 作者:MelonTeam 前言 很多时候大家都不关心IOS触摸事件的分发机制的实现原理,当遇到以下几种情形的时候你很可能抓破头皮都找不到解决方案 ...

  8. iOS中 HTTP/Socket/TCP/IP通信协议详解 韩俊强的博客

    每日更新关注:http://weibo.com/hanjunqiang  新浪微博 简单介绍: // OSI(开放式系统互联), 由ISO(国际化标准组织)制定 // 1. 应用层 // 2. 表示层 ...

  9. iOS学习——(转)UIResponder详解

    本文转载自:ios开发 之 UIResponder详解 我们知道UIResponder是所有视图View的基类,在iOS中UIResponder类是专门用来响应用户的操作处理各种事件的,包括触摸事件( ...

随机推荐

  1. Linux系统下如何配置SSH?如何开启SSH?

    SSH作为Linux远程连接重要的方式,如何配置安装linux系统的SSH服务,如何开启SSH?下面来看看吧(本例为centos系统演示如何开启SSH服务). 查询\安装SSH服务 1.登陆linux ...

  2. zip文件解压或压缩

    <span style="font-size:18px;">/** * lsz */ public final class ZipUtil { /** * 解压zip文 ...

  3. caffe简易上手指南(三)—— 使用模型进行fine tune

    之前的教程我们说了如何使用caffe训练自己的模型,下面我们来说一下如何fine tune. 所谓fine tune就是用别人训练好的模型,加上我们自己的数据,来训练新的模型.fine tune相当于 ...

  4. xp宿主机和VMware下Ubuntu12.04共享文件夹

    VMware下Windows与Linux共享文件的方法有很多,比如Samba等等,我这里介绍简单地通过设置VMware来达到共享的目的. 打开VMware的设置,在"options" ...

  5. 如何写mysql的定时任务

     什么是事件: 一组SQL集,用来执行定时任务,跟触发器很像,都是被动执行的,事件是因为时间到了触发执行,而触发器是因为某件事件(增删改)触发执行: 查看是否开启: show variables li ...

  6. poj2482Stars in Your Window(线段树+离散化+扫描线)

    http://poj.org/problem?id=2482 类似于上一篇 这题转化的比较巧妙 将一个点转化为一个矩形(x,y, x+w,y+h),扫描线入值为正 出值为负 也就是一根线过去 每进入一 ...

  7. Codeforces 380A - Sereja and Prefixes

    原题地址:http://codeforces.com/problemset/problem/380/A 让期末考试整的好久没有写题, 放假之后由于生病也没怎么做,新年的第一场CF也不是那么在状态,只过 ...

  8. [原]Unity3D深入浅出 - 认识开发环境中的RenderSettings面板

    点击菜单栏的Edit项里的RenderSettings即可打开该面板. Fog:在Scene中开启雾效果 Fog Color:雾的颜色 Fog Mode:雾效果的模式,Linear(线性雾效果) Ex ...

  9. [转] 字符串模式匹配算法——BM、Horspool、Sunday、KMP、KR、AC算法一网打尽

    字符串模式匹配算法——BM.Horspool.Sunday.KMP.KR.AC算法一网打尽 转载自:http://dsqiu.iteye.com/blog/1700312 本文内容框架: §1 Boy ...

  10. POJ 3204 Ikki's Story I-Road Reconstruction (网络流关键边)

    [题意]给定一个N个节点M条边的网络流,求有多少条边,使得当增其中加任何一个边的容量后,整个网络的流将增加. 挺好的一道题,考察对网络流和增广路的理解. [思路] 首先关键边一定是满流边.那么对于一个 ...