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

关键代码如下:

<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





  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值