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

关键代码如下:

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <span style="font-size:14px;">-(PlaceholderTextView *)holderTextView  
  2. {  
  3.     if (!_holderTextView) {  
  4.           
  5.         _holderTextView=[[PlaceholderTextView alloc] initWithFrame:CGRectMake(1064, kDeviceWidth-2060)];  
  6.         _holderTextView.delegate=self;  
  7.         _holderTextView.font = kFontNormal_16;  
  8.         _holderTextView.placeholderFont = kFontNormal_16;  
  9.         _holderTextView.edgeInsets=UIEdgeInsetsMake(81000);  
  10.         _holderTextView.placeholder = @"可自填,多项请用逗号分隔";  
  11.         _holderTextView.layer.borderWidth=0.5;  
  12.         _holderTextView.layer.borderColor=[UIColor whiteColor].CGColor;  
  13.         _holderTextView.layoutManager.allowsNonContiguousLayout = YES;  
  14.         //修复在有导航栏的情况下输入文本框上下偏移问题  
  15.         self.automaticallyAdjustsScrollViewInsets = NO;  
  16.           
  17.         //KVO动态监听text的值变化  
  18.         [self.holderTextView addObserver:self forKeyPath:@"text" options:NSKeyValueObservingOptionNew context:nil];  
  19.           
  20.     }  
  21.       
  22.     return _holderTextView;  
  23. }</span>  


[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #pragma mark - 实现KVO回调方法  
  2. -(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(voidvoid *)context  
  3. {  
  4.     if([keyPath isEqualToString:@"text"])  
  5.     {  
  6.         NSString *text= [object valueForKey:@"text"];  
  7.           
  8.         if (text.length>0&&![text isEqualToString:@""]) {  
  9.               
  10.             //滚动到最后一行  
  11.             [self.holderTextView scrollRangeToVisible:NSMakeRange(self.holderTextView.text.length1)];  
  12.               
  13.         }  
  14.     }  
  15. }  


在PlaceholderTextView控件(原作者:gitBurning)上进行改造

支持设置placeholder的TextView哦

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <span style="font-size:14px;">#import <UIKit/UIKit.h>  
  2.   
  3. /** 
  4.  *  要是用非 arc。。。。。。//     -fno-objc-arc 
  5.  */  
  6. @interface PlaceholderTextView : UITextView  
  7.   
  8. @property(copy,nonatomic)   NSString *placeholder;  
  9. @property(strong,nonatomic) UIColor  *placeholderColor;  
  10. @property(strong,nonatomic) UIFont   *placeholderFont;  
  11. @property(strong,nonatomic) UILabel  *PlaceholderLabel;  
  12. //placeholder边距  
  13. @property(assign,nonatomic) UIEdgeInsets edgeInsets;  
  14.   
  15. @end</span>  


[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #import "PlaceholderTextView.h"  
  2.   
  3. @interface PlaceholderTextView()  
  4. //<UITextViewDelegate>  
  5.   
  6. @end  
  7.   
  8. @implementation PlaceholderTextView  
  9.   
  10. - (id) initWithFrame:(CGRect)frame {  
  11.     if ((self = [super initWithFrame:frame])) {  
  12.         [self awakeFromNib];  
  13.         self.edgeInsets=UIEdgeInsetsMake(0000);//默认边距  
  14.     }  
  15.     return self;  
  16. }  
  17.   
  18. - (void)awakeFromNib {  
  19.       
  20.       
  21.      [[NSNotificationCenter defaultCenter] addObserver:self  
  22.      selector:@selector(textDidChange:)  
  23.      name:UITextViewTextDidChangeNotification object:self];  
  24.       
  25.       
  26.       
  27.       
  28.     self.placeholderColor =[UIColor lightGrayColor];  
  29.     self.PlaceholderLabel =[[UILabel alloc] init];  
  30.     self.PlaceholderLabel.numberOfLines=0;  
  31.     [self addSubview:self.PlaceholderLabel];  
  32.       
  33.     //KVO动态监听text的值变化  
  34.     [self addObserver:self forKeyPath:@"text" options:NSKeyValueObservingOptionNew context:nil];  
  35.       
  36.       
  37. }  
  38.   
  39. //实现KVO回调方法  
  40. -(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(voidvoid *)context  
  41. {  
  42.     if([keyPath isEqualToString:@"text"])  
  43.     {  
  44.         NSString *text= [object valueForKey:@"text"];  
  45.           
  46.         if (text.length>0&&![text isEqualToString:@""]) {  
  47.             self.PlaceholderLabel.hidden=YES;  
  48.         }else{  
  49.             self.PlaceholderLabel.hidden=NO;  
  50.         }  
  51.     }  
  52. }  
  53.   
  54.  //监听文字的变化  
  55.  -(void)textDidChange:(NSNotification*)notification  
  56. {  
  57.      if (self.placeholder.length == 0 || [self.placeholder isEqualToString:@""]) {  
  58.          self.PlaceholderLabel.hidden=YES;  
  59.      }  
  60.        
  61.      if (self.text.length > 0) {  
  62.          self.PlaceholderLabel.hidden=YES;  
  63.      }else{  
  64.          self.PlaceholderLabel.hidden=NO;  
  65.      }  
  66.    
  67.  }  
  68.   
  69. //  
  70. //-(void)setPlaceholder:(NSString *)placeholder{  
  71. //    if (placeholder.length == 0 || [placeholder isEqualToString:@""]) {  
  72. //        self.PlaceholderLabel.hidden=YES;  
  73. //    }  
  74. //  
  75. //    _placeholder=placeholder;  
  76. //}  
  77.   
  78. -(void)layoutSubviews  
  79. {  
  80.     [super layoutSubviews];  
  81.       
  82.     //    NSLog(@"%s",__FUNCTION__);  
  83.     self.PlaceholderLabel.font=self.placeholderFont?self.placeholderFont:self.font;  
  84.     self.PlaceholderLabel.textColor=self.placeholderColor;  
  85.     self.PlaceholderLabel.text=self.placeholder;  
  86.       
  87.     CGFloat placeholderW=self.frame.size.width-self.edgeInsets.left*2;  
  88.     CGSize placeholderSize=  
  89.     [NSString sizeWithText:self.placeholder font:self.PlaceholderLabel.font maxW:placeholderW];  
  90.     self.PlaceholderLabel.frame=CGRectMake(self.edgeInsets.leftself.edgeInsets.top, placeholderSize.width, placeholderSize.height);  
  91. }  
  92.   
  93. -(void)dealloc{  
  94.       
  95.     NSLog(@"销毁%s",__FUNCTION__);  
  96.     [[NSNotificationCenter defaultCenter] removeObserver:self];  
  97.     //注销KVO监听  
  98.     [self removeObserver:self forKeyPath:@"text"];  
  99.     [self.PlaceholderLabel removeFromSuperview];  
  100. }  
  101.   
  102. @end  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值