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 ...
随机推荐
- C程序运行的背后(2)
话说上回说到,C程序运行之前,必须要加载到其进程地址空间中.今儿咱就扯扯这个加载到底是怎么加载的. 一图胜前言,这个图简单说明了可执行文件加载过程的逻辑流,在此只做粗粒度概要说明.需要准确描述的,请出 ...
- JDK源码(1.7) -- java.util.Collection<E>
java.util.Collection<E> 源码分析(JDK1.7) -------------------------------------------------------- ...
- 2015 UESTC 数据结构专题G题 秋实大哥去打工 单调栈
秋实大哥去打工 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/59 Descr ...
- [原]Redis详细配置介绍
Redis详细配置介绍 # redis 配置文件示例 # 当你需要为某个配置项指定内存大小的时候,必须要带上单位, # 通常的格式就是 1k 5gb 4m 等酱紫: # # 1k => 1000 ...
- iOS开发经验总结——基础工程
iOS开发经验总结--依赖库 这篇博客,我想说一下开发中经常遇到的一个问题,虚拟个场景描述一下的话,应该是这样的. 项目经理:今天我们正式开始一个新项目,iOSer你负责把苹果端的APP完成,有没有问 ...
- Java过滤任意(script,html,style)标签符,返回纯文本--封装类
import java.util.regex.Pattern; /** * 过滤标签字符串,返回纯文本 * */ public class ChangePlainText { ...
- 用matplotlib绘制带误差的条形图及中英文字体设置
#!/usr/bin/env python3 ## 以下是一个带误差条的条形图的例子,演示了误差条形图的绘制及中英文字体设置 import numpy as np import matplotlib ...
- GB2312简体中文编码表
GB2312简体中文编码表 code +0 +1 +2 +3 +4 +5 +6 +7 +8 +9 +A +B +C +D +E +F A1A0 . . · ˉ ˇ ¨ " 々 — - | … ...
- 解析天气预报JSON数据
解析天气预报JSON数据 JSON字符串 constjson2 = '{' + #13#10 +'"error":0,' + #13#10 +'"status" ...
- 史上最全的Unity面试题(持续更新总结。。。。。。) 包含答案的Unity面试题
这个是我刚刚整理出的Unity面试题,为了帮助大家面试,同时帮助大家更好地复习Unity知识点,如果大家发现有什么错误,(包括错别字和知识点),或者发现哪里描述的不清晰,请在下面留言,我会重新更新,希 ...