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. 分布式事务的管理--atomikos

    在一些业务场景及技术架构下,跨库的事务时不可避免的,这时候如何统一管理事务,保证事务的强一致性是整个系统稳定.可用基石.一些中间件如tuxedo.cics就是凭借这个能力占据了金融.电信.银行等很大的 ...

  2. 跨域使用jsonp 获取天气预报

    <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server">    ...

  3. 手动添加 memcached.jar包

    由于目前java memcached client没有官方的maven repository可供使用,因此使用时需要手动将其安装到本地repository. java memcached client ...

  4. strlen的C/C+++实现

    2013-07-05 11:36:05 小结: 本函数给出了几种strlen的实现,有ugly implementation,也有good implementation.并参考标准库中的impleme ...

  5. mac terminal终端ls命令参数详解

    原文:https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/ls.1.html -a ...

  6. poj1195Mobile phones(二维树状数组)

    http://poj.org/problem?id=1195 模版题 i写成k了 找了一个多小时没找出来.. #include <iostream> #include<cstring ...

  7. poj 1062 昂贵的聘礼(最短路 dijk+枚举)

    终于A 了,这题做着真麻烦 题目:http://poj.org/problem?id=1062 dijk 一般用于正权有向图 此题的关键在于等级限制的处理,最好的办法是采用枚举,即假设酋长等级为5,等 ...

  8. bzoj1426

    偷个懒,转自hzwer [“这种煞笔题怎么总有人问”,被吧主D了... 用f[i]表示已经拥有了i张邮票,则期望还需要购买的邮票数 则f[n]=0 f[i]=f[i]*(i/n)+f[i+1]*((n ...

  9. [.NET WebAPI系列01] WebAPI 简单例子

    [源] 来自微软WebAPI官方视频,Introduction to the ASP.NET Web API --Uniform Interface -- Demo-Using convention ...

  10. Deployed component GUIs and figures have different look and feel than MATLAB desktop

    原文:http://www.mathworks.com/support/bugreports/1293244 Description Deployed GUIs and figures look an ...