项目要求
1、UITextView的高度随文字的增加而增加,类似微信
2、多于3行不再增加UITextView的高度
3、是亲测78高度三行,根据文字大小的不同亲们自行定义,灵活运用
查了很多网站都是在
- (
BOOL
)textView:(
UITextView
*)textView shouldChangeTextInRange:(
NSRange
)range replacementText:(
NSString
*)text{
}
或
- (void)textViewDidChange:(UITextView *)textView{
}
中作处理,我试了一下,多多少少都有些问题,不是很完美,因此改变策略
使用通知:
[
[
NSNotificationCenter
defaultCenter
]
addObserver
:
self
selector
:
@selector
(textViewTextDidChange:)
name
:
UITextViewTextDidChangeNotification
object
:
self
.
textView
];
总算问题少了很多,亲测目前使用还行,欢迎大家发现问题留言
源码:
@interface
TextViewHeightController
()<
UITextViewDelegate
>
@property
(
nonatomic
,
strong
)
UITextView
*textView;
//
是否有滚动
@property
(
nonatomic
,
assign
,
getter
=scrollFlag)
BOOL
flag;
@end
- (
void
)viewDidLoad {
[
super
viewDidLoad
];
self
.
navBar
.
titleLabel
.
text