您的位置:首页 > 产品设计 > UI/UE

利用KVO实现 UITextView 通过代码方式设置内容后,自动滚动到最后一行

2016-03-30 15:14 621 查看
关键代码如下:

<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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: