美文网首页iOS实战开发
iOS-textView文本换行高度自动适应(3段代码)

iOS-textView文本换行高度自动适应(3段代码)

作者: 暖游丶 | 来源:发表于2016-06-03 10:55 被阅读1357次

    简述:

    由于开发中需要需要个类似QQ空间的一个评论功能,首先要做的输入框肯定是会换行的,因为iOS中(TextField)是单行组件,所以只能使用(TextView)这个多行组件了,可是在不做处理的情况下这个组件的文字在换行的时候会顶出输入框

    解决思路:

    1.通过Block 在TextView代理方法中把文字内容的ContentSize回调

    //改变根据文字改变TextView的高度

    typedef void (^ContentSizeBlock)(CGSize contentSize);

    @interface ReplyInputView : UIView

    @property (nonatomic,weak)UITextView *textfd;

    -(void)setContentSizeBlock:(ContentSizeBlock) block;

    @end

    #pragma mark - textView delegate

    -(void)textViewDidChange:(UITextView *)textView

    {//根据字长度判断是否隐藏占位符Label

    self.lblPlaceholder.hidden = (textView.text.length > 0);

    CGSize contentSize = self.textfd.contentSize;

    self.sizeBlock(contentSize);

    }

    2.在Vc中实例化我们输入框View的时候使用block调用更新约束的方法

    #pragma mark - 初始化textview

    -(void)initReplyInputView{

    ReplyInputView *replyInputView =

    [[ReplyInputView alloc]initWithFrame:

    CGRectMake(0,KDeviceH-42-64, KDeviceW, 42) andAboveView:self.view];

    replyInputView.textdelegate = self;

    [self.view  addSubview:replyInputView];

    self.replyInputView = replyInputView;

    __weak typeof(self) weakSelf = self;

    //使用block

    [replyInputView setContentSizeBlock:^(CGSize contentSize) {

    //更新约束

    [weakSelf updateHeight:contentSize];

    }];

    }

    //更新replyView的高度约束

    -(void)updateHeight:(CGSize)contentSize

    {

    float height = contentSize.height +20;

    CGRect frame = self.replyInputView.frame;

    frame.origin.y -= height - frame.size.height;  //高度往上拉伸

    frame.size.height = height;

    self.replyInputView.frame = frame;

    }

    这样即可实现了

    效果图:

    相关文章

      网友评论

      • iamgaoshuai:有实现代码吗? 857348334@qq.com 谢了

      本文标题:iOS-textView文本换行高度自动适应(3段代码)

      本文链接:https://www.haomeiwen.com/subject/odnkdttx.html