首页 > 其他 > 详细

UITextView自适应高度的解决方案

时间:2014-03-09 08:41:36      阅读:467      评论:0      收藏:0      [点我收藏+]

UITextView自适应高度的解决方案

UITextView想要自适应高度有一个比较简单的解决方法,就是利用textView的contentSize属性。
新建一个类继承UITextView,重载setContent方法,将自身的高度设置为当前的contentSize这就已经做到了textView的高度自适应,当然我们可能还需要附加一些功能,我们可以实现一个在
高度改变时候调用的委托这样就能方便一些特殊的处理。

这是我写的代码。

bubuko.com,布布扣
 1 #import <UIKit/UIKit.h>
 2 
 3 @class VTAutoHeightTextView;
 4 
 5 @protocol VTAutoHeightTextViewDelegate <NSObject>
 6 
 7 @optional
 8 - (void)textView:(VTAutoHeightTextView *)textView heightChanged:(NSInteger)height;
 9 
10 @end
11 
12 @interface VTAutoHeightTextView : UITextView
13 
14 @property (assign, nonatomic) id<UITextViewDelegate, VTAutoHeightTextViewDelegate> delegate;
15 
16 @end
17   
bubuko.com,布布扣

 

 

bubuko.com,布布扣
 1 #import "VTAutoHeightTextView.h"
 2 
 3 @implementation VTAutoHeightTextView
 4 
 5 - (void)setContentSize:(CGSize)contentSize
 6 {
 7   CGSize oriSize = self.contentSize;
 8   [super setContentSize:contentSize];
 9 
10   if(oriSize.height != self.contentSize.height)
11   {
12     CGRect newFrame = self.frame;
13     newFrame.size.height = self.contentSize.height;
14     self.frame = newFrame;
15     if([self.delegate respondsToSelector:@selector(textView:heightChanged:)])
16     {
17       [self.delegate textView:self heightChanged:self.contentSize.height - oriSize.height];
18     }
19   }
20  }
21 
22 @end
bubuko.com,布布扣

 



UITextView自适应高度的解决方案,布布扣,bubuko.com

UITextView自适应高度的解决方案

原文:http://www.cnblogs.com/vitoz/p/3589496.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!