为UITextView添加通知..来检测UITextView内容的改变
2015-09-30 17:23
447 查看
self.mTextView =[[UITextView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH - 100, 28)];
self.mTextView.backgroundColor =[UIColor whiteColor];
self.mTextView.delegate = self;
self.mTextView.text = @"我来说几句....";
[self.mSecondaryBackView addSubview:self.mTextView];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChanged:) name:UITextViewTextDidChangeNotification object:nil];
- (void)textDidChanged:(NSNotification *)notif //监听文字改变 换行时要更改输入框的位置
{
CGSize contentSize = self.mTextView.contentSize;
UILabel *holdPlace = (UILabel *)[self.mTextView viewWithTag:1500];
if (holdPlace) {
holdPlace.text = @"";
}
if (contentSize.height > 140){
return;
}
CGFloat minus = 3;
CGRect selfFrame = self.mBackView.frame;
CGFloat selfHeight = self.mTextView.superview.frame.origin.y * 2 + contentSize.height - minus + 2 * 2;
CGFloat selfOriginY = selfFrame.origin.y - (selfHeight - selfFrame.size.height);
selfFrame.origin.y = selfOriginY;
selfFrame.size.height = selfHeight;
self.mBackView.frame = selfFrame;
self.mSecondaryBackView.frame =CGRectMake(10, 10, 230, selfHeight-20);
NSLog(@"文字改变");
}
self.mTextView.backgroundColor =[UIColor whiteColor];
self.mTextView.delegate = self;
self.mTextView.text = @"我来说几句....";
[self.mSecondaryBackView addSubview:self.mTextView];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChanged:) name:UITextViewTextDidChangeNotification object:nil];
- (void)textDidChanged:(NSNotification *)notif //监听文字改变 换行时要更改输入框的位置
{
CGSize contentSize = self.mTextView.contentSize;
UILabel *holdPlace = (UILabel *)[self.mTextView viewWithTag:1500];
if (holdPlace) {
holdPlace.text = @"";
}
if (contentSize.height > 140){
return;
}
CGFloat minus = 3;
CGRect selfFrame = self.mBackView.frame;
CGFloat selfHeight = self.mTextView.superview.frame.origin.y * 2 + contentSize.height - minus + 2 * 2;
CGFloat selfOriginY = selfFrame.origin.y - (selfHeight - selfFrame.size.height);
selfFrame.origin.y = selfOriginY;
selfFrame.size.height = selfHeight;
self.mBackView.frame = selfFrame;
self.mSecondaryBackView.frame =CGRectMake(10, 10, 230, selfHeight-20);
NSLog(@"文字改变");
}
相关文章推荐
- 获取手机的唯一标示uuid
- bones ui
- request.getAttribute( "result");和request.setAttribute("result",username);
- NSData与UIImage之间的转换
- 《SICP》读书笔记--Chapter 2 Building Abstraction with Data
- CSU 1515 - Sequence (莫队)
- SpriteBuilder物理对象的父子关系
- SpriteBuilder物理对象的父子关系
- SpriteBuilder物理对象的父子关系
- UGUI 代码 动态添加 Event Trigger 的事件
- 利用StoryBoard修改placeholder颜色(UITextField的placeholder)
- Google Guice 依赖注入框架:简明教程
- poj2299:Ultra-QuickSort
- Parse query on pointer column
- UITableView使用selectRowAtIndexPath不能默认选中cell的问题
- UICollectionViewController 版本新特性
- 安装VMM 2012 R2累积更新1可能遇到的问题
- IOS开发-CALayer和UIView详细汇总
- c笔记05---逻辑表达式,break,continue 区别
- 《SICP》读书笔记--Chapter 1 Building Abstraction with Procedures