1. #import "MainViewController.h"
  2.  
  3. @interface MainViewController () <UITextViewDelegate>
  4. @property(nonatomic,retain) UITextView *textView;
  5. @property(nonatomic,retain) UIButton *btn;
  6. @property(nonatomic,retain) UIView *commentView;
  7.  
  8. @end
  9.  
  10. @implementation MainViewController
  11. /*UITextView与UITextField的区别:
  12. 输入多行 可以滚动显示浏览全文
  13. 软件简介、内容详情显示、小说阅读显示、发表空间内容输入、说说文本框、评论文本框等
  14.  
  15. UITextView的使用有它本身的代理方法,也有继承于父类的方法。
  16. 本身的方法监听: 开始编辑 -------> 结束编辑 类似UITextField
  17.  
  18. 主要继承于UIScrollView的方法
  19. */
  20.  
  21. /*
  22. 继承自:UIScrollView : UIView : UIResponder : NSObject
  23. 必须先实现UITextViewDelegate协议,都是optional修饰
  24.  
  25. 关键步骤:
  26. 1.设置代理
  27. 2.字体大小
  28. 3.添加滚动区域
  29. 4.是否滚动
  30. 5.获得焦点
  31.  
  32. 轻松搞定frame:⚡️
  33. CGRectGetMinX(CGRect rect):指定控件的frame.origin.x
  34. CGRectGetMidX(CGRect rect):指定控件的frame.origin.x + frame.size.width / 2
  35. CGRectGetMaxX(CGRect rect):指定控件的frame.origin.x + frame.size.width
  36. CGRectGetWidth(CGRect rect):指定控件的frame.size.width
  37. */
  38.  
  39. #pragma mark -结束加载-
  40. - (void)viewDidLoad {
  41.  
  42. [super viewDidLoad];
  43. self.view.backgroundColor = [UIColor grayColor];
  44. self.navigationItem.title = @"UITextView的使用";
  45.  
  46. [self setUpView];
  47.  
  48. #pragma mark ===监听键盘状态:打开或关闭===
  49. [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardShow:) name:UIKeyboardWillShowNotification object:nil];
  50. [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardHide:) name:UIKeyboardWillHideNotification object:nil];
  51.  
  52. }
  53.  
  54. #pragma mark -打开键盘-
  55. -(void)keyboardShow:(NSNotification *)note
  56. {
  57. CGRect keyBoardRect=[note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
  58. // 获取键盘的高度
  59. CGFloat deltaY=keyBoardRect.size.height;
  60. [UIView animateWithDuration:[note.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue] animations:^{
  61. _commentView.transform=CGAffineTransformMakeTranslation(, -deltaY);
  62. }];
  63. }
  64. #pragma mark -关闭键盘-
  65. -(void)keyboardHide:(NSNotification *)note
  66. {
  67. [UIView animateWithDuration:[note.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue] animations:^{
  68. // 还原设置
  69. _commentView.transform=CGAffineTransformIdentity;
  70. } completion:^(BOOL finished) {
  71. // 支付宝密码输入
  72. // _textView.text=@"";
  73. // [_commentView removeFromSuperview];
  74. }];
  75. }
  76.  
  77. #pragma mark ======布局视图=====
  78. - (void)setUpView{
  79.  
  80. #pragma mark -UIView视图上放置textView-
  81. self.commentView = [[UIView alloc]initWithFrame:CGRectMake(, [UIScreen mainScreen].bounds.size.height - , [UIScreen mainScreen].bounds.size.width, )];
  82. [self.view addSubview:_commentView];
  83. _commentView.backgroundColor = [UIColor colorWithRed: / 255.0 green: / 255.0 blue:1.0 alpha:1.0];
  84. [_commentView release];
  85. NSLog(@"%f", [UIScreen mainScreen].bounds.size.height);
  86.  
  87. #pragma mark -textView-
  88. self.textView = [[UITextView alloc]initWithFrame:CGRectMake(, , [UIScreen mainScreen].bounds.size.width - , )];
  89. _textView.layer.cornerRadius = ;
  90. _textView.layer.borderColor = [UIColor blueColor].CGColor;
  91. _textView.layer.borderWidth = ;
  92. _textView.bounces = NO;
  93. _textView.contentOffset = CGPointMake(CGRectGetMinX(_textView.frame) + , );
  94. #pragma mark -关键步骤-
  95. //设置代理
  96. _textView.delegate = self;
  97. //是否可编辑
  98. _textView.editable = YES;
  99. //是否允许滚动,默认是一行
  100. _textView.scrollEnabled = YES;
  101. //字体大小
  102. _textView.font = [UIFont systemFontOfSize:];
  103. //显示位置默认居左
  104. _textView.textAlignment = NSTextAlignmentLeft;
  105. //键盘类型
  106. _textView.keyboardType = UIKeyboardTypeDefault;
  107.  
  108. #pragma mark ===可输入内容区域===
  109.  
  110. //选中范围
  111. // _textView.selectedRange = NSMakeRange(0, 3);
  112.  
  113. //获得焦点, 即运行程序textView处于开始编辑状态
  114. // [_textView becomeFirstResponder];
  115.  
  116. //有导航栏时,输入文本会下移,修复方法‼️‼️
  117. self.automaticallyAdjustsScrollViewInsets = NO;
  118.  
  119. //选中区域
  120. // [_textView scrollRangeToVisible:_textView.selectedRange];
  121.  
  122. #pragma mark -实现placeholder功能的猥琐方法-
  123.  
  124. _textView.text = @"请输入内容";
  125. _textView.textColor = [UIColor grayColor];
  126.  
  127. #pragma ==数据类型检测==
  128. /*
  129. 检测出来的是类型用浅蓝色显示
  130. 注意:editable设置为NO
  131. */
  132. // _textView.editable = NO;
  133. // _textView.dataDetectorTypes = UIDataDetectorTypeAll;
  134. // _textView.text = @"我的手机号是:132 4567 9841,我的博客是: www.baidu.com, 我的邮箱是:zoujianguo130@163.com";
  135.  
  136. [_commentView addSubview:_textView];
  137. [_textView release];
  138.  
  139. #pragma mark -按钮-
  140. self.btn = [UIButton buttonWithType: UIButtonTypeRoundedRect];
  141. _btn.frame = CGRectMake(CGRectGetMaxX(_textView.frame) + , CGRectGetMinY(_textView.frame), , );
  142. _btn.layer.cornerRadius = ;
  143. _btn.backgroundColor = [UIColor colorWithRed:0.0 green: / 255.0 blue: / 255.0 alpha:1.0];
  144. [_btn setTitle:@"提交" forState:UIControlStateNormal];
  145. [_btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  146. [_commentView addSubview:_btn];
  147. [self.btn addTarget:self action:@selector(handleSubmmit:) forControlEvents:UIControlEventTouchUpInside];
  148.  
  149. }
  150.  
  151. #pragma mark -提交按钮的单击事件-
  152. - (void)handleSubmmit:(UIButton *)sender{
  153.  
  154. }
  155.  
  156. #pragma mark -UITextViewDelegate中可选择实现方法-
  157.  
  158. - (BOOL)textViewShouldBeginEditing:(UITextView *)textView{
  159. NSLog(@"将要开始编辑");
  160. #pragma -placeholder功能-
  161. if ([_textView.text isEqual:@"请输入内容"]) {
  162. _textView.text = @"";
  163. _textView.textColor = [UIColor blackColor];
  164. }
  165. return YES;
  166. }
  167. - (BOOL)textViewShouldEndEditing:(UITextView *)textView{
  168.  
  169. NSLog(@"将要结束编辑");
  170. return YES;
  171. }
  172.  
  173. - (void)textViewDidBeginEditing:(UITextView *)textView{
  174. NSLog(@"开始编辑。。。");
  175. }
  176. - (void)textViewDidEndEditing:(UITextView *)textView{
  177. NSLog(@"编辑结束。。。");
  178. #pragma mark -placeholder功能-
  179. if (_textView.text.length < ) {
  180. _textView.text = @"请输入内容";
  181. _textView.textColor = [UIColor grayColor];
  182. }
  183. }
  184.  
  185. #pragma mark -是否允许修改内容-
  186. - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
  187.  
  188. #pragma ====控制输入长度❗️====
  189. // if (range.location >= 6)
  190. // {
  191. // UIAlertView * alert=[[UIAlertView alloc] initWithTitle:@"提示" message:@"您已输入6个字" delegate:nil cancelButtonTitle:@"返回" otherButtonTitles: nil];
  192. // [alert show];
  193. // [alert release];
  194. // }
  195. // else
  196. // {
  197. // return YES;
  198. // }
  199.  
  200. //判断键盘是否有选中的内容,即禁止输入换行
  201. if ([text isEqualToString:@"\n"]) {
  202. [textView resignFirstResponder];
  203. return NO;
  204. }
  205. NSLog(@"将要改变内容");
  206. return YES;
  207. }
  208.  
  209. //内容改变时才触发,必须手动输入有效
  210. - (void)textViewDidChange:(UITextView *)textView{
  211.  
  212. #pragma ==✅控制输入长度==(不能准确的识别中文的长度)
  213. // if (textView.text.length >= 6) {
  214. // textView.text = [textView.text substringToIndex:6];
  215. // UIAlertView * alert=[[UIAlertView alloc] initWithTitle:@"提示" message:@"您已输入6个字" delegate:nil cancelButtonTitle:@"返回" otherButtonTitles: nil];
  216. // [alert show];
  217. // [alert release];
  218. // }
  219.  
  220. NSLog(@"内容已经更改。。。");
  221. }
  222.  
  223. //几乎所有操作都会触发,如:点击文本框,增加内容,删除内容。。。
  224. //可以理解为只要和selectedRange有关都会触发(位置和长度)
  225. - (void)textViewDidChangeSelection:(UITextView *)textView{
  226. NSLog(@"选中内容 (焦点发生改变)");
  227. }
  228.  
  229. - (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange {
  230. return YES;
  231. }
  232.  
  233. - (BOOL)textView:(UITextView *)textView shouldInteractWithTextAttachment:(NSTextAttachment *)textAttachment inRange:(NSRange)characterRange {
  234. return YES;
  235. }
  236.  
  237. #pragma mark -单击空白处回收键盘-
  238. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
  239. [self.textView resignFirstResponder];
  240. }
  241. #pragma mark -return回收键盘-
  242. - (BOOL)textFieldShouldReturn:(UITextField *)textField{
  243. return YES;
  244. }
  245.  
  246. - (void)didReceiveMemoryWarning {
  247. [super didReceiveMemoryWarning];
  248. // Dispose of any resources that can be recreated.
  249. }
  250.  
  251. @end
  252. UITextView demo

UITextView 学习代码

UI:UITextView的更多相关文章

  1. 工程日记之HelloSlide(2) : UITextView中如何根据给定的长宽,计算最合适的字体大小

    需求描述 一般的需求是将UITextview的大小自适应文本高度,会做出随文本内容增加,文字框不断增大的效果: 本文反其道而行之,在给定文字框大小的情况下:字数越多,字体越小: 需求来源: 考虑将文字 ...

  2. UI:UITableView 编辑、cell重用机制

    tableView编辑.tableView移动.UITableViewController tableView的编辑:cell的添加.删除. 使⽤场景: 删除⼀个下载好的视频,删除联系⼈: 插⼊⼀条新 ...

  3. UI:UIScrollView、UIPageControl

    一.UIScrollView的常⽤用属性 二.UIScrollView的常⽤用代理方法 三.UIPageControl的使⽤用 四.UIPageControl与UIScrollView的结合使⽤用 U ...

  4. UI:基础

    App的生命周期 参考 多态的使用 // // main.m #import <Foundation/Foundation.h> #import "SingleDog.h&quo ...

  5. Android UI:机智的远程动态更新策略

    问题描述 做过Android开发的人都遇到过这样的问题:随着需求的变化,某些入口界面通常会出现 UI的增加.减少.内容变化.以及跳转界面发生变化等问题.每次发生变化都要手动修改代码,而入口界面通常具有 ...

  6. Vue UI:Vue开发者必不可少的工具

    译者按: Vue开发工具越来越好用了! 原文: Vue UI: A First Look 译者: Fundebug 本文采用意译,版权归原作者所有 随着最新的稳定版本Vue CLI 3即将发布,是时候 ...

  7. android UI:Fragment碎片

    碎片(Fragment) 嵌入与活动中的UI片段,为了合理的分配布局而存在,这是我的简单理解.多用于兼顾手机与平板的UI,也适用于灵活高级的UI制作. Demo 简单的按键切换两片不同的Demo 新建 ...

  8. ios 开发UI篇—UITextView

    概述 UITextView可滚动的多行文本区域 UITextView支持使用自定义样式信息显示文本,并支持文本编辑.您通常使用文本视图来显示多行文本,例如在显示大型文本文档的正文时. UITextVi ...

  9. WPF 多线程 UI:设计一个异步加载 UI 的容器

    对于 WPF 程序,如果你有某一个 UI 控件非常复杂,很有可能会卡住主 UI,给用户软件很卡的感受.但如果此时能有一个加载动画,那么就不会感受到那么卡顿了.UI 的卡住不同于 IO 操作或者密集的 ...

随机推荐

  1. 【dp】codeforces C. Vladik and Memorable Trip

    http://codeforces.com/contest/811/problem/C [题意] 给定一个自然数序列,在这个序列中找出几个不相交段,使得每个段的异或值之和相加最大. 段的异或值这样定义 ...

  2. hdu 2579

    #include<stdio.h> #include<queue> #include<iostream> #include<string.h> #inc ...

  3. 银河英雄传说(codevs 1540)

    题目描述 Description 公元五八○一年,地球居民迁移至金牛座α第二行星,在那里发表银河联邦创立宣言,同年改元为宇宙历元年,并开始向银河系深处拓展. 宇宙历七九九年,银河系的两大军事集团在巴米 ...

  4. CodeForces - 320B Ping-Pong (Easy Version)

    题目最开始 完全不懂 配合案例也看不懂-_- 总之就是用传递性 问能否从a区间到b区间 dfs(x,y) 走遍与第x区间所有的 联通区间 最后检验 第y区是否被访问过 是一道搜索好题 搜索还需加强 # ...

  5. 【收藏】SSH原理与运用

    http://www.ruanyifeng.com/blog/2011/12/ssh_remote_login.html http://www.ruanyifeng.com/blog/2011/12/ ...

  6. tiles

    参考博客:https://blog.csdn.net/aosica321/article/details/68948915 https://blog.csdn.net/it_faquir/articl ...

  7. jquery的ajax提交时“加载中”提示的处理方法

    方法1:使用ajaxStart方法定义一个全局的“加载中...”提示 $(function(){    $("#loading").ajaxStart(function(){    ...

  8. Object_C 定义全局宏的颜色时,报“Expected identifier”的错误

    在定义全局颜色宏的时候,为了整齐把空格删了,写在了同一行里,调用的时候,出错提示“Expected identifier”,如下: 如果宏定义如上那样的话,在调用的时候,会出现如下的问题: 百思不得解 ...

  9. Codeforces 658D Bear and Polynomials【数学】

    题目链接: http://codeforces.com/contest/658/problem/D 题意: 给定合法多项式,改变一项的系数,使得P(2)=0,问有多少种方法? 分析: 暴力求和然后依次 ...

  10. PAT (Advanced Level) 1036. Boys vs Girls (25)

    简单题. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> ...