UITextView 的使用
直接上代码:
//
// RootViewController.m
// UIText_test
//
//
#import "RootViewController.h"
#import <QuartzCore/QuartzCore.h> /// 用户视觉反馈
@interface RootViewController ()<UITextViewDelegate>
@property (nonatomic, strong) UIToolbar *toolBar ;
@property (nonatomic, strong) UITextView *textView ;
@end
@implementation RootViewController
- (UIToolbar *)toolBar {
if (!_toolBar) {
self.toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 375, 30)];
[_toolBar setBarStyle:UIBarStyleBlack];
UIBarButtonItem *oneButton = [[UIBarButtonItem alloc] initWithTitle:@"開始" style:UIBarButtonItemStyleDone target:self action:nil];
UIBarButtonItem *twoButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:self action:nil];
UIBarButtonItem *threeButton = [[UIBarButtonItem alloc] initWithTitle:@"完毕" style:UIBarButtonItemStyleDone target:self action:@selector(handleThreeBtn)];
NSArray *buttonArray = [NSArray arrayWithObjects:oneButton, twoButton, threeButton, nil];
[_toolBar setItems:buttonArray]; // 加入到 toolBar 上
}
return _toolBar;
}
- (UITextView *)textView {
if (!_textView) {
self.textView = [[UITextView alloc] initWithFrame:CGRectMake(5, 200, 375, 190)];
_textView.textColor = [UIColor redColor];
_textView.font = [UIFont systemFontOfSize:20];
_textView.delegate = self;
[_textView.layer setCornerRadius:10]; // 设置成圆角
_textView.backgroundColor = [UIColor cyanColor];
_textView.text = @"sfjlegjklaeyg a";
_textView.scrollEnabled = YES;
_textView.autoresizingMask = UIViewAutoresizingFlexibleHeight ; // 自适应高度
[_textView setInputAccessoryView:self.toolBar]; // 设置辅助视图
}
return _textView;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self.view addSubview:self.textView];
// 自己定义菜单项
UIMenuItem *menuItem = [[UIMenuItem alloc] initWithTitle:@"分享到新浪微博" action:@selector(changeColor)];
UIMenuController *menu = [UIMenuController sharedMenuController];
[menu setMenuItems:[NSArray arrayWithObject:menuItem]]; //加入
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Action -
- (void)handleThreeBtn {
[self.textView resignFirstResponder]; // 撤销第一次响应
}
- (void)changeColor {
self.view.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1];
}
- (void)handleDone {
[self.textView resignFirstResponder]; // 撤销第一响应
}
#pragma mark - UITextViewDelegate -
// 完毕開始编辑
- (void)textViewDidBeginEditing:(UITextView *)textView {
UIBarButtonItem *done = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(handleDone)];
self.navigationItem.rightBarButtonItem = done;
}
// 完毕结束编辑
- (void)textViewDidEndEditing:(UITextView *)textView {
self.navigationItem.rightBarButtonItem = nil; /// 导航条按钮设置为空
}
// 当文本发生改变时
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
if ([text isEqual:@"\n"]) {
[textView resignFirstResponder];
return NO;
}
return YES;
}
@end
UITextView 的使用的更多相关文章
- UITextView 输入字数限制
本文介绍了UITextView对中英文还有iOS自带表情输入的字数限制,由于中文输入会有联想导致字数限制不准确所以苦恼好久,所以参考一些大神的博客终于搞定,欢迎大家参考和指正. 对于限制UITextV ...
- iOS 之UITextFiled/UITextView小结
一:编辑被键盘遮挡的问题 参考自:http://blog.csdn.net/windkisshao/article/details/21398521 1.自定方法 ,用于移动视图 -(void)mov ...
- UITextView 点击添加文字 光标处于最后方
#import "ViewController.h" @interface ViewController ()<UITextViewDelegate> @end @im ...
- UI控件(UITextView)
@implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //UITextView与UITextField主要 ...
- 实现UITextView的placeholder
我们知道在iOS开发时,控件UITextField有个placeholder属性,UITextField和UITextView使用方法基本类似,有两个小区别:1.UITextField单行输入,而UI ...
- UITextView回收或关闭键盘
iOS开发中,发现UITextView没有像UITextField中textFieldShouldReturn:这样的方法,那么要实现UITextView关闭键盘,就必须使用其他的方法,下面是可以使用 ...
- [转]iOS7中UITextView contentsize改变时机
在iOS7以下版本中,对UITextView设置了text属性,则contentsize就会变化,从而可以根据contentsize的变化来改变UITextView高度来做出TextView高度随着输 ...
- UITextView 显示不全的问题
//设置UITextView的内边距 textView.contentInset = UIEdgeInsetsMake(0, 0, 20, 0);
- UITextView: 响应键盘的 return 事件(收回键盘)
UITextView: 响应键盘的 return 事件(收回键盘) 此篇文章将要介绍UITextView: 响应键盘的 return 事件(收回键盘)的相关介绍,具体实例请看下文 UITextView ...
- 你真的了解UITextView吗?
一:首先查看一下关于UITextView的定义 NS_CLASS_AVAILABLE_IOS(2_0) @interface UITextView : UIScrollView <UITextI ...
随机推荐
- Java类实例化原理
Java对象的创建过程包括 类初始化(JVM类加载机制)和类实例化两个阶段. 一.Java对象创建时机 (1)使用new关键字创建对象 (2)反射创建对象 使用Class类的newInstance方法 ...
- ThinkPHP -- 去除URL中的index.php
原路径是 http://localhost/test/index.php/index/add 想获得的地址是 http://localhost/test/index/add 那么如何去掉index.p ...
- Spring---介绍
核心容器:Core.Beans.Context.EL模块 1. Core模块:封装了框架依赖的最底层部分,包括访问资源.类型转换及一些常用工具类 2. Beans模块:提供了框架的基础 ...
- [转]HorizontalScrollView介绍--支持水平滚动的android布局容器
类概述 用 于布局的容器,可以放置让用户使用滚动条查看的视图层次结构,允许视图结构比手机的屏幕大.HorizontalScrollView是一种 FrameLayout(框架布局),其子项被滚动查看时 ...
- [转]STRUTS2中的OGNL
OGNL表达式是(Object-Graph Navigation Language)是对象图形化导航语言.OGNL是一个开源的项目,struts2中默认使用OGNL表达式语言来显示数据.与serlve ...
- HDU 5640 King's Cake GCD
King's Cake 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5640 Description It is the king's birthd ...
- 2015 UESTC 搜索专题F题 Eight Puzzle 爆搜
Eight Puzzle Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/61 ...
- ExtJS 表单 submit时错误处理
这里不提success,提提Extjs 表单提交的failure方法. 在表单的提交中,当发生异常行为时通常分为三种情况 1. 无法连接到服务器 2. 表单验证错误 3. 业务逻辑错误 对应下面的代码 ...
- SpringMVC访问静态页面
Spring MVC显示静态页面 在前面搭建spring MVC环境时,我们设置了spring-mvc配置,通过tomcat来访问了index.jsp 页面,但是当我将页面换成.thml的静态面之后就 ...
- Android 开源项目android-open-project解析之(四) ColorPickView,GraphView,UI Style,Other
十三.ColorPickView ColorPickerView 颜色选择器,支持PopupWindows或新的Activity中打开 项目地址:https://code.google.com/p/c ...