UIiOS语法技巧ios开发

UITextView随着输入动态调整高度

2015-03-16  本文已影响4643人  大灰灰iOS

火柴盒发话题的输入框,之前写的效果一直不是很好,
昨天折腾到两点多,终于弄出了还可以的效果。

全局变量

//暂存键盘的偏移量
@property(nonatomic,assign)CGFloatkeyboardTy;
//输入后输入框增加的高度
@property(nonatomic,assign)CGFloatTextViewAddHeight;

键盘通知

- (void)keyBoardWillShow:(NSNotification *)note
{
    CGRect rect = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGFloat ty = rect.size.height; 
    self.keyboardTy = ty;
   ……
}
- (void)keyBoardWillHide:(NSNotification *)note
{
    self.keyboardTy = 0;
    ……
}

设置偏移量方法

-(void)setFrameOringin
{
    CGFloat ty = self.keyboardTy;
    CGFloat addHeight = self.TextViewAddHeight;
    self.buttomView.hidden = NO;
    self.buttomView.userInteractionEnabled = YES;
    self.pullTableView.frame = CGRectMake(0, ty, COMMENT_TABLE_WIDTH, COMMENT_TABLE_HEIGHT-ty-addHeight);
    self.buttomView.frame  = CGRectMake(0,COMMENT_TABLE_HEIGHT-addHeight,COMMENT_TABLE_WIDTH,44+addHeight);
    self.textField.frame = CGRectMake(17, 5, SCREEN_WIDTH-70, 34+addHeight);
    self.placeHolderLabel.frame = CGRectMake(10, 2, self.textField.frame.size.width-10, self.textField.frame.size.height-2);

}

获取键盘

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    DLog( @"textViewShouldChange");
    //这个方法是将改变的值
    
    NSInteger state = 0;
    if ([text isEqualToString:@"\n"]) {
        //如果这个字符是换行,立刻改变frame的大小
        state = 1;
    }
    if([text isEqualToString:@""]&& textView.text.length>0 )
    {
        //这个字符是删除,且删除掉的是换行
        if ([[textView.text substringWithRange:NSMakeRange(textView.text.length -1,1)] isEqualToString:@"\n"]) {
            state = 2;
        };
    }
    
    NSString *textStr = @"";
    if ([text isEqualToString:@""]&& textView.text.length>0) {
        //删除操作
        textStr = [textView.text substringWithRange:NSMakeRange(0,textView.text.length -1)];
    }
    else
    {
        //正常
        textStr = [NSString stringWithFormat:@"%@%@",textView.text,text];
    }
    
    
    [self textViewChange:textStr state:state];
    
    return YES;
}

改变高度的方法

- (void)textViewChange:(NSString *)text state:(NSInteger)state {   
    NSString *tString = @"";
    tString = self.textField.text;
    NSInteger height = 0;
    height = [GetLabelSize getStringRect:tString WithWidth:self.textField.frame.size.width andFont:self.textField.font].height;
    DLog(@"textField中文字的高度是:%ld",(long)height);
    //FONT15的情况下,一行17,后面每行都是17+18×(n-1);
    
    //多于一行的时候,增加的高度
    float addHeight;
    if (height < 18) {
        addHeight = 0;
    }
    else if(height > 17+18*4)
    {
        addHeight = 17+18*4;
    }else
    {
        addHeight = height - 17;
    }
    
    if (state == 1)
    {
        //如果最后一个字符是换行,立刻改变frame的大小
        addHeight = addHeight + 18;
        if (addHeight > 17+18*4)
        {
            addHeight = 17+18*4;
        }
    }
    
    if(state == 2)
    {
        //如果最后一个字符是删除,且删除了换行,立刻改变frame的大小
        if (addHeight < 18)   { 
            addHeight = 0;
        }else
        {
            addHeight = addHeight - 18;
        }
    }
   
    if (addHeight != self.TextViewAddHeight) {
        self.TextViewAddHeight = addHeight;
        [self setFrameOringin];
    }
    
}

还存在要优化的地方:

粘贴过来多行的没办法很好的显示。可以在

-(void)textViewDidChange:(UITextView*)textView

这个方法里优化

简书新手渣排版见谅~~

简书已经弃用,欢迎移步我的小专栏:
https://xiaozhuanlan.com/dahuihuiiOS

上一篇下一篇

猜你喜欢

热点阅读