利用KVO实现 UITextView 通过代码方式设置内容后,自动滚动到最后一行
2016-03-30 15:14
621 查看
关键代码如下:
在PlaceholderTextView控件(原作者:gitBurning)上进行改造
支持设置placeholder的TextView哦
<span style="font-size:14px;">-(PlaceholderTextView *)holderTextView { if (!_holderTextView) { _holderTextView=[[PlaceholderTextView alloc] initWithFrame:CGRectMake(10, 64, kDeviceWidth-20, 60)]; _holderTextView.delegate=self; _holderTextView.font = kFontNormal_16; _holderTextView.placeholderFont = kFontNormal_16; _holderTextView.edgeInsets=UIEdgeInsetsMake(8, 10, 0, 0); _holderTextView.placeholder = @"可自填,多项请用逗号分隔"; _holderTextView.layer.borderWidth=0.5; _holderTextView.layer.borderColor=[UIColor whiteColor].CGColor; _holderTextView.layoutManager.allowsNonContiguousLayout = YES; //修复在有导航栏的情况下输入文本框上下偏移问题 self.automaticallyAdjustsScrollViewInsets = NO; //KVO动态监听text的值变化 [self.holderTextView addObserver:self forKeyPath:@"text" options:NSKeyValueObservingOptionNew context:nil]; } return _holderTextView; }</span>
#pragma mark - 实现KVO回调方法 -(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { if([keyPath isEqualToString:@"text"]) { NSString *text= [object valueForKey:@"text"]; if (text.length>0&&![text isEqualToString:@""]) { //滚动到最后一行 [self.holderTextView scrollRangeToVisible:NSMakeRange(self.holderTextView.text.length, 1)]; } } }
在PlaceholderTextView控件(原作者:gitBurning)上进行改造
支持设置placeholder的TextView哦
<span style="font-size:14px;">#import <UIKit/UIKit.h> /** * 要是用非 arc。。。。。。// -fno-objc-arc */ @interface PlaceholderTextView : UITextView @property(copy,nonatomic) NSString *placeholder; @property(strong,nonatomic) UIColor *placeholderColor; @property(strong,nonatomic) UIFont *placeholderFont; @property(strong,nonatomic) UILabel *PlaceholderLabel; //placeholder边距 @property(assign,nonatomic) UIEdgeInsets edgeInsets; @end</span>
#import "PlaceholderTextView.h" @interface PlaceholderTextView() //<UITextViewDelegate> @end @implementation PlaceholderTextView - (id) initWithFrame:(CGRect)frame { if ((self = [super initWithFrame:frame])) { [self awakeFromNib]; self.edgeInsets=UIEdgeInsetsMake(0, 0, 0, 0);//默认边距 } return self; } - (void)awakeFromNib { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange:) name:UITextViewTextDidChangeNotification object:self]; self.placeholderColor =[UIColor lightGrayColor]; self.PlaceholderLabel =[[UILabel alloc] init]; self.PlaceholderLabel.numberOfLines=0; [self addSubview:self.PlaceholderLabel]; //KVO动态监听text的值变化 [self addObserver:self forKeyPath:@"text" options:NSKeyValueObservingOptionNew context:nil]; } //实现KVO回调方法 -(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { if([keyPath isEqualToString:@"text"]) { NSString *text= [object valueForKey:@"text"]; if (text.length>0&&![text isEqualToString:@""]) { self.PlaceholderLabel.hidden=YES; }else{ self.PlaceholderLabel.hidden=NO; } } } //监听文字的变化 -(void)textDidChange:(NSNotification*)notification { if (self.placeholder.length == 0 || [self.placeholder isEqualToString:@""]) { self.PlaceholderLabel.hidden=YES; } if (self.text.length > 0) { self.PlaceholderLabel.hidden=YES; }else{ self.PlaceholderLabel.hidden=NO; } } // //-(void)setPlaceholder:(NSString *)placeholder{ // if (placeholder.length == 0 || [placeholder isEqualToString:@""]) { // self.PlaceholderLabel.hidden=YES; // } // // _placeholder=placeholder; //} -(void)layoutSubviews { [super layoutSubviews]; // NSLog(@"%s",__FUNCTION__); self.PlaceholderLabel.font=self.placeholderFont?self.placeholderFont:self.font; self.PlaceholderLabel.textColor=self.placeholderColor; self.PlaceholderLabel.text=self.placeholder; CGFloat placeholderW=self.frame.size.width-self.edgeInsets.left*2; CGSize placeholderSize= [NSString sizeWithText:self.placeholder font:self.PlaceholderLabel.font maxW:placeholderW]; self.PlaceholderLabel.frame=CGRectMake(self.edgeInsets.left, self.edgeInsets.top, placeholderSize.width, placeholderSize.height); } -(void)dealloc{ NSLog(@"销毁%s",__FUNCTION__); [[NSNotificationCenter defaultCenter] removeObserver:self]; //注销KVO监听 [self removeObserver:self forKeyPath:@"text"]; [self.PlaceholderLabel removeFromSuperview]; } @end
相关文章推荐
- Value '0000-00-00 00:00:00' can not be represented as java.sql.Timestamp
- CSS中边框border属性、border-raduis圆角与border-image详解
- UIKit性能调优实战讲解
- KVC(key value coding)/ KVO(key value observing)
- MUI多端发布开发指南
- request获取json
- Gradle之使用BuildConfig自定义常量
- Educational Codeforces Round 10 E. Pursuit For Artifacts 强连通缩点+bfs
- Squirrel SQL Client连接hive安装部署
- 【Poj 2533】 Longest Ordered Subsequence 最长上升子序列
- CodeForces 645E Intellectual Inquiry(构造+贪心+dp)
- iOS边练边学--UITabBarController的简单使用
- 关于maven项目的build path编译错误
- IOS开发-UI学习-NSBundle和NSURL的区别(读取文件以及写入文件)
- UI自动化中关闭远程桌面连接,鼠标键盘失效的解决方案
- continue3.php
- Magnum Kuernetes源码分析(一)
- Vue.js
- UITextField - 文本框
- UEFI架构解读系列之EVENT