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; |
[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键都可以达到退出键盘的效果。
(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]; |
|
[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)]; |
|
(6)UITextView自定选择文字后的菜单
在ViewDidLoad中加入:
UIMenuItem *menuItem = [[UIMenuItem alloc]initWithTitle:@"分享到新浪微博" action:@selector(changeColor:)]; |
UIMenuController *menu = [UIMenuController sharedMenuController]; |
[menu setMenuItems:[NSArray arrayWithObject:menuItem]]; |
当然上面那个@selector里面的changeColor方法还是自己写吧,也就是说点击了我们自定义的菜单项后会触发的方法。
然后还得在代码里加上一个方法:
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender |
if(action == @selector(changeColor:)) |
if(textView.selectedRange.length>0) |
实现后如下图:

- UITextView 详解
UITextView 边框的设置 设置光标的位置 导入QuartzCote框架: #import <QuartzCore/QuartzCore.h> textView.layer. ...
- UITextView的使用详解
//初始化并定义大小 UITextView *textview = [[UITextView alloc] initWithFrame:CGRectMake(20, 10, 280, 30)]; te ...
- 【转】UITextView的使用详解
//初始化并定义大小 UITextView *textview = [[UITextView alloc] initWithFrame:CGRectMake(20, 10, 280, 30)]; te ...
- 【iOS 开发】基本 UI 控件详解 (UIButton | UITextField | UITextView | UISwitch)
博客地址 : http://blog.csdn.net/shulianghan/article/details/50051499 ; 一. UI 控件简介 1. UI 控件分类 UI 控件分类 : 活 ...
- iOS中 HTTP/Socket/TCP/IP通信协议详解
// OSI(开放式系统互联), 由ISO(国际化标准组织)制定 // 1. 应用层 // 2. 表示层 // 3. 会话层 // 4. 传输层 // 5. 网络层 // 6. 数据链接层 // 7. ...
- iOS应用开发详解
<iOS应用开发详解> 基本信息 作者: 郭宏志 出版社:电子工业出版社 ISBN:9787121207075 上架时间:2013-6-28 出版日期:2013 年7月 开本:16开 ...
- IOS 触摸事件分发机制详解
欢迎大家前往云+社区,获取更多腾讯海量技术实践干货哦~ 作者:MelonTeam 前言 很多时候大家都不关心IOS触摸事件的分发机制的实现原理,当遇到以下几种情形的时候你很可能抓破头皮都找不到解决方案 ...
- iOS中 HTTP/Socket/TCP/IP通信协议详解 韩俊强的博客
每日更新关注:http://weibo.com/hanjunqiang 新浪微博 简单介绍: // OSI(开放式系统互联), 由ISO(国际化标准组织)制定 // 1. 应用层 // 2. 表示层 ...
- iOS学习——(转)UIResponder详解
本文转载自:ios开发 之 UIResponder详解 我们知道UIResponder是所有视图View的基类,在iOS中UIResponder类是专门用来响应用户的操作处理各种事件的,包括触摸事件( ...
随机推荐
- BZOJ 2553 禁忌
首先我们要考虑给定一个串,如何将他划分,使得他有最多的禁忌串 我们只需要按里面出现的禁忌串们的出现的右端点排序然后贪心就可以啦 我们建出AC自动机,在AC自动机等价于走到一个包含禁忌串的节点就划分出一 ...
- ANDROID_MARS学习笔记_S01_002View、监听器初步
一.View.监听器介绍 二.在Activity中获取view和设置属性,设置button的监听器 1.activity_main.xml <LinearLayout xmlns:android ...
- UVA548——Tree(中后序建树+DFS)
Tree You are to determine the value of the leaf node in a given binary tree that is the terminal nod ...
- linux进程模型总结
Linux进程通过一个task_struct结构体描述,在linux/sched.h中定义,通过理解该结构,可更清楚的理解linux进程模型. 包含进程所有信息的task_struct数据 ...
- world符号大全
■特殊符号:·⊙①⊕◎Θ⊙●○¤㊣㈱@の■□★☆◆◇◣◢ ◤◥▲△▼▽⊿◢ ▂ ▃ ▄ ▅ ▆ ▇ █ ▉ ▊▋▌▍▎▏■ ▓ 回 □ 〓≡↑↓→←↘↙♀♂┇┅|$ @ * & # ※ 卍 卐 ...
- C# 字符串加密解密方法
这个是加密的算法的命名空间,使用加密算法前要引用该程序集 System.Security.Cryptography using System;using System.Data;using Syst ...
- 函数buf_LRU_block_remove_hashed_page
/******************************************************************//** Takes a block out of the LRU ...
- poj3692
首先这道题很容易想到二分图相关(给的很明确了): 但是我们发现,男孩之间都互相认识,女孩之间也互相认识 这样是不能划分点集的 但是男孩之间都互相认识,女孩之间也互相认识,所以男孩和男孩,女孩和女孩之间 ...
- 浏览器兼容问题系列---使IE支持CSS3 Media Quary
兼容是一件很让前端攻城师头疼的事情,笔者今天在做一个Demo的时候就碰到了一个问题(大牛就不要拍砖了,谢谢!) 经常做移动互联网前端的攻城师想必对于css3 media quary已经很熟悉了,但是碰 ...
- windows获取窗口句柄
1.使用FindWindow函数获取窗口句柄 示例:使用FindWindow函数获取窗口句柄,然后获得窗口大小和标题,并且移动窗口到指定位置. #include <Windows.h> # ...