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 ...
随机推荐
- bzoj 1026
很久以前做过的一道数位DP,现在用一种新的解决数位DP的比较一般的方法. 数位DP裸题是:求[L,R]有多少个数. 先转化成求[0,R]有多少个数,然后区间相减即可. 对于[0,R]中的所有数,用0补 ...
- nginx+php-fpm 配置和错误总结
<strong>空白页面:</strong>需要这个参数: fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_scrip ...
- OC中NSLog函数输出格式详解
OC中NSLog函数输出格式详解 %@ 对象 • %d, %i 整数 • %u 无符整形 • %f 浮点/双字 • %x, %X 二进制整数 • %o 八进制整数 • %zu size_t • %p ...
- Cortex-M3 and Cortex-M4 Memory Organization
http://www.mikroe.com/download/eng/documents/compilers/mikropascal/pro/arm/help/memory_organization. ...
- 【spring cloud】子模块module -->导入一个新的spring boot项目作为spring cloud的一个子模块微服务,怎么做/或者 每次导入一个新的spring boot项目,IDEA不识别子module,启动类无法启动/右下角没有蓝色图标
如题:导入一个新的spring boot项目作为spring cloud的一个子模块微服务,怎么做 或者说每次导入一个新的spring boot项目,IDEA不识别,启动类无法启动,怎么解决 下面分别 ...
- [Linux] du-查看文件夹大小-并按大小进行排序
reference : http://blog.csdn.net/jiaobuchong/article/details/50272761# 某天,我想检查一下电脑硬盘的的使用情况,作为一个命令控,废 ...
- [Android 源码] Android源码下载
Android源码下载 为了能够顺利的下载Android的源码,同时也为了避免在网络上再次搜寻如何下载源码的麻烦,我把下载过程记录在这篇文档中. 官网中也有详细的介绍: http://source.a ...
- ZeroMQZeroMQ研究与应用分析
1 ZeroMQ概述 ZeroMQ是一种基于消息队列的多线程网络库,其对套接字类型.连接处理.帧.甚至路由的底层细节进行抽象,提供跨越多种传输协议的套接字.ZeroMQ是网络通信中新的一层,介于应用 ...
- 深度Linux Deepin系统安装教程使用体验
很早以前,试用过一次深度的OS,那时深度刚出自己的Linux修改版系统,过了有两年了,准备看看Deepin OS有什么变化,和雨林木风的Start OS比,有什么不同 1.安装的引导菜单和Start ...
- Virtualbox中Linux添加新磁盘并创建分区
原文:https://www.linuxidc.com/Linux/2017-01/139616.htm ----------------------------------------------- ...