uitextview 相关知识

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];
}
```


转载于:https://my.oschina.net/coolwxb/blog/631320

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值