#import "MainViewController.h"

@interface MainViewController () <UITextViewDelegate>
@property(nonatomic,retain) UITextView *textView;
@property(nonatomic,retain) UIButton *btn;
@property(nonatomic,retain) UIView *commentView; @end @implementation MainViewController
/*UITextView与UITextField的区别:
输入多行 可以滚动显示浏览全文
软件简介、内容详情显示、小说阅读显示、发表空间内容输入、说说文本框、评论文本框等 UITextView的使用有它本身的代理方法,也有继承于父类的方法。
本身的方法监听: 开始编辑 -------> 结束编辑 类似UITextField 主要继承于UIScrollView的方法
*/ /*
继承自:UIScrollView : UIView : UIResponder : NSObject
必须先实现UITextViewDelegate协议,都是optional修饰 关键步骤:
1.设置代理
2.字体大小
3.添加滚动区域
4.是否滚动
5.获得焦点 轻松搞定frame:⚡️
CGRectGetMinX(CGRect rect):指定控件的frame.origin.x
CGRectGetMidX(CGRect rect):指定控件的frame.origin.x + frame.size.width / 2
CGRectGetMaxX(CGRect rect):指定控件的frame.origin.x + frame.size.width
CGRectGetWidth(CGRect rect):指定控件的frame.size.width
*/ #pragma mark -结束加载-
- (void)viewDidLoad { [super viewDidLoad];
self.view.backgroundColor = [UIColor grayColor];
self.navigationItem.title = @"UITextView的使用"; [self setUpView]; #pragma mark ===监听键盘状态:打开或关闭===
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardHide:) name:UIKeyboardWillHideNotification object:nil]; } #pragma mark -打开键盘-
-(void)keyboardShow:(NSNotification *)note
{
CGRect keyBoardRect=[note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
// 获取键盘的高度
CGFloat deltaY=keyBoardRect.size.height;
[UIView animateWithDuration:[note.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue] animations:^{
_commentView.transform=CGAffineTransformMakeTranslation(, -deltaY);
}];
}
#pragma mark -关闭键盘-
-(void)keyboardHide:(NSNotification *)note
{
[UIView animateWithDuration:[note.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue] animations:^{
// 还原设置
_commentView.transform=CGAffineTransformIdentity;
} completion:^(BOOL finished) {
// 支付宝密码输入
// _textView.text=@"";
// [_commentView removeFromSuperview];
}];
} #pragma mark ======布局视图=====
- (void)setUpView{ #pragma mark -UIView视图上放置textView-
self.commentView = [[UIView alloc]initWithFrame:CGRectMake(, [UIScreen mainScreen].bounds.size.height - , [UIScreen mainScreen].bounds.size.width, )];
[self.view addSubview:_commentView];
_commentView.backgroundColor = [UIColor colorWithRed: / 255.0 green: / 255.0 blue:1.0 alpha:1.0];
[_commentView release];
NSLog(@"%f", [UIScreen mainScreen].bounds.size.height); #pragma mark -textView-
self.textView = [[UITextView alloc]initWithFrame:CGRectMake(, , [UIScreen mainScreen].bounds.size.width - , )];
_textView.layer.cornerRadius = ;
_textView.layer.borderColor = [UIColor blueColor].CGColor;
_textView.layer.borderWidth = ;
_textView.bounces = NO;
_textView.contentOffset = CGPointMake(CGRectGetMinX(_textView.frame) + , );
#pragma mark -关键步骤-
//设置代理
_textView.delegate = self;
//是否可编辑
_textView.editable = YES;
//是否允许滚动,默认是一行
_textView.scrollEnabled = YES;
//字体大小
_textView.font = [UIFont systemFontOfSize:];
//显示位置默认居左
_textView.textAlignment = NSTextAlignmentLeft;
//键盘类型
_textView.keyboardType = UIKeyboardTypeDefault; #pragma mark ===可输入内容区域=== //选中范围
// _textView.selectedRange = NSMakeRange(0, 3); //获得焦点, 即运行程序textView处于开始编辑状态
// [_textView becomeFirstResponder]; //有导航栏时,输入文本会下移,修复方法‼️‼️
self.automaticallyAdjustsScrollViewInsets = NO; //选中区域
// [_textView scrollRangeToVisible:_textView.selectedRange]; #pragma mark -实现placeholder功能的猥琐方法- _textView.text = @"请输入内容";
_textView.textColor = [UIColor grayColor]; #pragma ==数据类型检测==
/*
检测出来的是类型用浅蓝色显示
注意:editable设置为NO
*/
// _textView.editable = NO;
// _textView.dataDetectorTypes = UIDataDetectorTypeAll;
// _textView.text = @"我的手机号是:132 4567 9841,我的博客是: www.baidu.com, 我的邮箱是:zoujianguo130@163.com"; [_commentView addSubview:_textView];
[_textView release]; #pragma mark -按钮-
self.btn = [UIButton buttonWithType: UIButtonTypeRoundedRect];
_btn.frame = CGRectMake(CGRectGetMaxX(_textView.frame) + , CGRectGetMinY(_textView.frame), , );
_btn.layer.cornerRadius = ;
_btn.backgroundColor = [UIColor colorWithRed:0.0 green: / 255.0 blue: / 255.0 alpha:1.0];
[_btn setTitle:@"提交" forState:UIControlStateNormal];
[_btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[_commentView addSubview:_btn];
[self.btn addTarget:self action:@selector(handleSubmmit:) forControlEvents:UIControlEventTouchUpInside]; } #pragma mark -提交按钮的单击事件-
- (void)handleSubmmit:(UIButton *)sender{ } #pragma mark -UITextViewDelegate中可选择实现方法- - (BOOL)textViewShouldBeginEditing:(UITextView *)textView{
NSLog(@"将要开始编辑");
#pragma -placeholder功能-
if ([_textView.text isEqual:@"请输入内容"]) {
_textView.text = @"";
_textView.textColor = [UIColor blackColor];
}
return YES;
}
- (BOOL)textViewShouldEndEditing:(UITextView *)textView{ NSLog(@"将要结束编辑");
return YES;
} - (void)textViewDidBeginEditing:(UITextView *)textView{
NSLog(@"开始编辑。。。");
}
- (void)textViewDidEndEditing:(UITextView *)textView{
NSLog(@"编辑结束。。。");
#pragma mark -placeholder功能-
if (_textView.text.length < ) {
_textView.text = @"请输入内容";
_textView.textColor = [UIColor grayColor];
}
} #pragma mark -是否允许修改内容-
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{ #pragma ====控制输入长度❗️====
// if (range.location >= 6)
// {
// UIAlertView * alert=[[UIAlertView alloc] initWithTitle:@"提示" message:@"您已输入6个字" delegate:nil cancelButtonTitle:@"返回" otherButtonTitles: nil];
// [alert show];
// [alert release];
// }
// else
// {
// return YES;
// } //判断键盘是否有选中的内容,即禁止输入换行
if ([text isEqualToString:@"\n"]) {
[textView resignFirstResponder];
return NO;
}
NSLog(@"将要改变内容");
return YES;
} //内容改变时才触发,必须手动输入有效
- (void)textViewDidChange:(UITextView *)textView{ #pragma ==✅控制输入长度==(不能准确的识别中文的长度)
// if (textView.text.length >= 6) {
// textView.text = [textView.text substringToIndex:6];
// UIAlertView * alert=[[UIAlertView alloc] initWithTitle:@"提示" message:@"您已输入6个字" delegate:nil cancelButtonTitle:@"返回" otherButtonTitles: nil];
// [alert show];
// [alert release];
// } NSLog(@"内容已经更改。。。");
} //几乎所有操作都会触发,如:点击文本框,增加内容,删除内容。。。
//可以理解为只要和selectedRange有关都会触发(位置和长度)
- (void)textViewDidChangeSelection:(UITextView *)textView{
NSLog(@"选中内容 (焦点发生改变)");
} - (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange {
return YES;
} - (BOOL)textView:(UITextView *)textView shouldInteractWithTextAttachment:(NSTextAttachment *)textAttachment inRange:(NSRange)characterRange {
return YES;
} #pragma mark -单击空白处回收键盘-
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[self.textView resignFirstResponder];
}
#pragma mark -return回收键盘-
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
return YES;
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end
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. 2016 Multi-University Training Contest 7 solutions BY SYSU

    Ants 首先求出每个点的最近点. 可以直接对所有点构造kd树,然后在kd树上查询除本身以外的最近点,因为对所有点都求一次,所以不用担心退化. 也可以用分治做,同样是O(NlogN)的复杂度. 方法是 ...

  2. 【2018 Multi-University Training Contest 3】

    01:https://www.cnblogs.com/myx12345/p/9420198.html 02: 03: 04:https://www.cnblogs.com/myx12345/p/940 ...

  3. poj3635 FULL tank(TLE) 有限制的最短路(BFS搜索)。

    用的BFS+优先队列+二进制压缩状态判重+链式前向星, TLE,好像有人这样过了...好像要用A*算法,还不太会,所以暂时放弃.但是也学会了很多,学习了链式前向星,更深理解了BFS求最优的时候,什么时 ...

  4. JFinal Weixin 1.5 发布,微信极速 SDK

    原文:http://www.oschina.net/news/67980/jfinal-weixin-1-5-released JFinal Weixin 1.5 大幅完善了对微信公众平台API的支持 ...

  5. Eddy's AC难题--hdu2200(递推)

    Problem Description Eddy是个ACMer,他不仅喜欢做ACM题,而且对于Ranklist中每个人的ac数量也有一定的研究,他在无聊时经常在纸上把Ranklist上每个人的ac题目 ...

  6. MySQL查询在一个表而不在另一个表中的数据

    1.使用not in,容易理解,效率低 select distinct A.ID from A where A.ID not in (select ID from B) 2.使用left join.. ...

  7. Arcgis栅格时序地图制作---时间轴动态展示多期影像

    转自原文 Arcgis栅格时序地图制作---时间轴动态展示多期影像 效果如何???满意您go on,不满意咱 say goodbye··· 题外话: 为了在这里动态展示下制作结果,也是费了老劲了,转换 ...

  8. IOCP实现的任务队列

    unit IOCPQueue; interface uses windows, classes; type TOnQueueProc = procedure(sender: tobject; Para ...

  9. 【Nginx】如何建立新连接

    处理新连接事件的回调函数是ngx_event_accept,原型如下: void ngx_event_accept(ngx_event_t *ev) 具体流程如下: 1)首先调用accept方法试图建 ...

  10. [javase学习笔记]-9.2 单继承与多重继承

    这一节我们来看java中的单继承和多重继承. 在java语言中,支持的是单继承,不直接支持多继承,可是对C++中的多继承进行了改良. 那么什么是单继承和多继承呢? 单继承:一个子类仅仅能有一个直接父类 ...