title: UITextView 相关知识
date: 2015-12-7 15:20
categories: IOS
tags: UILabel
小小程序猿
我的博客:http://daycoding.com
uitextview 小键盘完成回调
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range
replacementText:(NSString *)text {
if ([text isEqualToString:@"\n"]) {
[textView resignFirstResponder];
return NO;
}
return YES;
}
uitextview 计算动态高度
textview需要将scrollenabled
设置为no
_textView.scrollEnabled = NO;
_textView.showsVerticalScrollIndicator = NO;
_textView.showsHorizontalScrollIndicator = NO;
//方式一
#pragma mark - uitextview 代理
- (void)textViewDidChange:(UITextView *)theTextView
{
float oldheight = theTextView.frame.size.height;
float height = [self.tv_layer_title sizeThatFits:CGSizeMake(self.tv_layer_title.frame.size.width, FLT_MAX)].height ;
if (fabs(height - oldheight) > 0.01) {
[self.bg_title mas_updateConstraints:^(MASConstraintMaker *make) {
make.height.mas_equalTo(height+20);
}];
[self.tv_layer_title mas_updateConstraints:^(MASConstraintMaker *make) {
make.height.mas_equalTo(height);
}];
}
}
//方式二 计算放入文字放入控件后应有的高度
//这里的20 是放置控件时与父视图左右边距空留出来的20
//self.caluteCell.textView 即是要放入文字的控件
CGSize infoSize = [self.caluteCell.textView sizeThatFits:CGSizeMake(SCREEN_WIDTH-20, FLT_MAX)];
调整uitextview 默认内边距
_textView.contentInset = UIEdgeInsetsMake(0, -5, 0, 0);
uitextView 调整视图中心点位置
//注册监听
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(changeContentViewPoint:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(changeContentViewPoint:)
name:UIKeyboardWillHideNotification
object:nil];
// 根据键盘状态,调整_mainView的位置
- (void) changeContentViewPoint:(NSNotification *)notification{
NSDictionary *userInfo = [notification userInfo];
NSValue *value = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
CGFloat keyBoardEndY = value.CGRectValue.origin.y; // 得到键盘弹出后的键盘视图所在y坐标
NSNumber *duration = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSNumber *curve = [userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey];
// 添加移动动画,使视图跟随键盘移动
[UIView animateWithDuration:duration.doubleValue animations:^{
[UIView setAnimationBeginsFromCurrentState:YES];
// [UIView setAnimationCurve:curve];
self.center = CGPointMake(self.center.x, keyBoardEndY -64- self.bounds.size.height/2.0); // keyBoardEndY的坐标包括了状态栏的高度,要减去
}];
}
获取view所在的controller
UIResponder *responder = self;
while ((responder = [responder nextResponder])){
if ([responder isKindOfClass: [EditMarker2ViewController class]])
{
EditMarker2ViewController* controller =(EditMarker2ViewController *)responder;
[controller.navigationController popViewControllerAnimated:YES];
}
}
UITextView的包涵inset高度
设置_textView.contentInset = UIEdgeInsetsMake(10,0,-10,0);
即上边距和底边距各10像素
但是使用fdcell
利用约束动态计算cell高度时测量的是不包含inset的uitextview高度所以需要额外+20个单位像素
```objc
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section==1) {
return 108;
}else
{
float height = [tableView fd_heightForCellWithIdentifier:@"cell" configuration:^(EditMarkerExpandableTextCellTableViewCell* cell) {
MarkerAttr* attr = [self.attrArray objectAtIndex:indexPath.row];
cell.textView.text = [self.dataDic objectForKey:attr.key];
}];
//额外添加10+10 上边距+下边距 inset
return height+20.0f;
}
return 100;
}
### 失去焦点 使键盘消失
```objc
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
点击空白处隐藏键盘
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.view endEditing:YES];
}
```