iOS中 UITextView文本视图 技术分享
UITextView:
文本视图相比与UITextField直观的区别就是UITextView可以输入多行文字并且可以滚动显示浏览全文。
UITextField的用处多,UITextView的用法也不少。常见UITextView使用在APP的软件简介、内容详情显示
小说阅读显示、发表空间内容输入、说说文本框、评论文本框等。UITextView的使用有它本身的代理方法,也有
继承于父类的方法。本身的方法有从开始编辑到结束编辑的整个过程的监听,继承的方法主要是继承于
UIScrollView的方法,因为关于滚动的控制都属于UIScrollView的。根据常用经验,个人添加了在有导航栏
的情况下可能输入文本框是下移的修复方法和添加文字时内容显示自动滚动到UITextView底部的实现方法。
#import "TextViewController.h"
@interface TextViewController ()<UITextViewDelegate>
@property(nonatomic,retain)UILabel *placeholderLabel;
@property(nonatomic,retain)UITextView *textView;
@end
@implementation TextViewController
- (void)dealloc
{
self.placeholderLabel = nil;
self.textView = nil;
[super dealloc];
}
- (void)viewDidLoad {
[super viewDidLoad];
//添加背景颜色
self.view.backgroundColor = [UIColor cyanColor];
//导航控制器名称
self.navigationController.title = @"UITextView的创建与使用";
//调用介绍TextView的相关属性
[self configureTextView];
}
介绍TextView的相关属性
- (void)configureTextView{
//创建TextView视图
self.textView = [[UITextView alloc]initWithFrame:CGRectMake(10, 80, self.view.frame.size.width - 80, 100)];
//添加到父视图
[self.view addSubview:self.textView];
//修复文本框的偏移量(下移)
self.automaticallyAdjustsScrollViewInsets = NO;
//设置UITextView的属性
//1.设置文本
// self.textView.text = @"你好,我是小韩哥";
//2.设置文字的对齐方式
self.textView.textAlignment = NSTextAlignmentCenter;
//3.设置文字字体相关属性
self.textView.font = [UIFont systemFontOfSize:18];
//等等和UITextField几乎是一样的
//设置背景颜色
self.textView.backgroundColor = [UIColor grayColor];
//设置边框颜色和宽度
self.textView.layer.borderColor = [[UIColor colorWithRed:200.0/255 green:50/255 blue:10/255 alpha:1] CGColor];
self.textView.layer.borderWidth = 2;
//7.设置编辑属性,是否允许编辑(为NO时,只用来显示,依然可以使用选择和拷贝功能)
// self.textView.editable = NO;
self.textView.editable = YES;
//模仿UITextField的placeholder属性
// 在textViewDidBeginEditing和textViewDidEndEditing内写实现方法。
self.placeholderLabel = [[UILabel alloc]initWithFrame:CGRectMake(5, 5, CGRectGetWidth(self.textView.frame), 20)];
//将UILabel的背景颜色设置为透明颜色clearColor
self.placeholderLabel.backgroundColor = [UIColor clearColor];
//设置UILabel的textColor属性为灰色grayColor
self.placeholderLabel.textColor = [UIColor grayColor];
//设置UILabel的text属性为需要的提示文字
self.placeholderLabel.text = @"请输入内容";
//设置UILabel的font属性和self.textView.font一致
self.placeholderLabel.font = self.textView.font;
//将UILabel添加到self.textView图层上
[self.textView addSubview:self.placeholderLabel];
//添加按钮及监听
UIButton *addButton = [UIButton buttonWithType:UIButtonTypeCustom];
addButton.frame = CGRectMake(CGRectGetMaxX(self.textView.frame)+10, 80, 50, 30);
addButton.backgroundColor = [UIColor lightGrayColor];
[addButton setTitle:@"添加" forState:UIControlStateNormal];
[addButton addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:addButton];
//添加代理协议
self.textView.delegate = self;
[self.placeholderLabel release];
[self.textView release];
}
#pragma mark- 按钮点击事件实现方法
- (void)btnClick:(UIButton*)sender{
NSLog(@"添加内容:欢迎来到韩俊强的CSDN博客");
self.textView.text = [self.textView.text stringByAppendingString:@"韩俊强的CSDN博客\n"];
//输入文字时自动滚动到底部
/*
1.拼接字符串赋值给self.textView.text;
2.计算NSRange自动滚动到底部。
NSRange是一个结构体,其中location是一个以0为开始的index,length是表示对象的长度。他们都是NSUInteger类型
*/
NSRange range = NSMakeRange([self.textView.text length]- 1, 1);
[self.textView scrollRangeToVisible:range];
// [self.view endEditing:YES];
}
#pragma mark- 实现协议里的方法
//1、将要开始编辑
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{
NSLog(@"将要开始编辑?");
return YES;
}
//2、将要完成编辑
- (BOOL)textViewShouldEndEditing:(UITextView *)textView{
NSLog(@"将要结束编辑?");
return YES;
}
//3、开始编辑
- (void)textViewDidBeginEditing:(UITextView *)textView{
NSLog(@"开始编辑。");
self.placeholderLabel.text = @"";
}
//4、完成编辑
- (void)textViewDidEndEditing:(UITextView *)textView{
NSLog(@"结束编辑。");
//模仿UTextField的placeholder属性
if (self.textView.text.length == 0) {
self.placeholderLabel.text = @"请输入内容";
}else{
self.placeholderLabel.text = @"";
}
}
//5、将要改变内容
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
NSLog(@"将要改变内容?");
return YES;
}
//6、内容完成改变,只有在内容改变时才触发,而且这个改变内容是手动输入有效,用本例中得按钮增加内容不触发这个操作
- (void)textViewDidChange:(UITextView *)textView{
NSLog(@"改变内容。");
}
//7、内容被选中,几乎所有操作都会触发textViewDidChangeSelection,包括点击文本框、增加内容删除内容
- (void)textViewDidChangeSelection:(UITextView *)textView{
NSLog(@"选中内容。");
}
#pragma mark- 回收键盘
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// [self.view endEditing:YES];
[self.textView resignFirstResponder];
}
最终效果:
iOS中 UITextView文本视图 技术分享的更多相关文章
- iOS中 UISearchController 搜索栏 UI技术分享
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; font-family: 'STHeiti Light' ...
- iOS中 WGAFN_网络监控 技术分享
需要用到第三方AFNetworking/SVProgressHUD 没有的可以关注我微博私信我.http://weibo.com/hanjunqiang AppDelegate.m #import & ...
- IOS中UITextView(多行文本框)控件的简单用法
1.创建并初始化 UITextView文本视图相比与UITextField直观的区别就是UITextView可以输入多行文字并且可以滚动显示浏览全文.UITextField的用处多,UITextVie ...
- 如何让IOS中的文本实现3D效果
本转载至 http://bbs.aliyun.com/read/181991.html?spm=5176.7114037.1996646101.25.p0So7c&pos=9 zh ...
- UITextView(文本视图) 学习之初体验
UITextView文本视图相比与UITextField直观的区别就是UITextView可以输入多行文字并且可以滚动显示浏览全文.常见UITextView使用在APP的软件简介.内容详情显示.小说阅 ...
- Gtk中的文本视图(GtkTexViewWidget)
Gtk中的文本视图(GtkTexViewWidget) Gtk中的文本视图(GtkTexView Widget) 在本章的Gtk+程序设计教程中,我们将重点介绍 GtkTexView 构件. GtkT ...
- 关于ios中的文本操作-简介
来源:About Text Handling in iOS 官方文档 iOS平台为我们提供了许多在app中展示文本和让用户编辑文本的方式.同时,它也允许你在app视图中展示格式化的文本和网页内容.你可 ...
- 关于iOS中的文本操作-管理text fields 和 text views
Managing Text Fields and Text Views 管理UITextField和UITextView实例 UITextField和UITextView的实例拥有两个最主要的功能:展 ...
- iOS中textbox文本框清除圆角
在iOS.Mac safari中,所有的textbox, select, checkbox都会被强制美化为圆角.但在特殊情况下需要清除圆角时发现iOS中使用以下传统的css无效: border-rad ...
随机推荐
- 论文笔记--PCN:Real-Time Rotation-Invariant Face Detection with Progressive Calibration Networks
关键词:rotation-invariant face detection, rotation-in-plane, coarse-to-fine 核心概括:该篇文章为中科院计算所智能信息处理重点实验室 ...
- Webpack 4 Tutorial: from 0 Conf to Production Mode
webpack 4 is out! The popular module bundler gets a massive update. webpack 4, what's new? A massive ...
- 使用Spring实现定时任务
一.分类 从实现的技术上来分类,目前主要有三种技术(或者说有三种产品): Java自带的java.util.Timer类,这个类允许你调度一个java.util.TimerTask任务.使用这种方式可 ...
- solr和solrcloud
Solr = Lucene + Http(Servlet/REST) + Schema.xml+Solrconfig.xml Solr = SolrSingle + Solr MutilCore = ...
- iOS开源加密相册Agony的实现(五)
简介 虽然目前市面上有一些不错的加密相册App,但不是内置广告,就是对上传的张数有所限制.本文介绍了一个加密相册的制作过程,该加密相册将包括多密码(输入不同的密码即可访问不同的空间,可掩人耳目).Wi ...
- 常用的DDL语句
create database mydb1; 创建一个名称为mydb1的数据库. use db_name; 切换数据库 ; show databases; 查看所有的数据库: select datab ...
- android Handler机制之ThreadLocal详解
概述 我们在谈Handler机制的时候,其实也就是谈Handler.Message.Looper.MessageQueue之间的关系,对于其工作原理我们不做详解(Handler机制详解). Messa ...
- Android N(7.0) 被美翻的新特性!
Tamic 专注移动开发!更多文章请关注 Csdn: http://blog.csdn.net/sk719887916/article/details/52612444 $ http://www.ji ...
- YCSB性能测试工具使用
在网上查In-Memory NoSQL性能测试的资料时,偶然间发现了这个性能测试工具YCSB,全称为"Yahoo! Cloud Serving Benchmark".它内置了对常见 ...
- MPAndroidChart——饼图
MPAndroidChart--饼图 MPAndroidChart是安卓下的一个开源图形库,很多效果,简单看几个效果图 Github地址:https://github.com/PhilJay/MPAn ...