masonry TextView 高度自适应

2018-01-28  本文已影响705人  风___________

1. 不要设置高度约束

2. 关闭TextView滚动

代码如下

// 第一步
    commentTextView.scrollEnabled = NO;
// 第二部
[self.commentTextView mas_makeConstraints:^(MASConstraintMaker *make) {
        float height = PCH_BitMap_BY_SIZE(60);
        float leftOffset = PCH_BitMap_BY_SIZE(30);
        make.leading.equalTo(self).offset(leftOffset);
        make.trailing.equalTo(self).offset(-leftOffset);
        make.bottom.equalTo(self).offset(-topOffset);
        make.height.mas_greaterThanOrEqualTo(height);
    }];

追加点别的知识

  1. textView return按钮的点击监听事件
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
   // 换行按钮点击
    if([text isEqualToString:@"\n"] && self.returnBlock) {
        if(self.returnBlock)self.returnBlock(textView.text);
        return NO;
    }
    return YES;
}
  1. textView设置 placehold
    //  字体大小需要一致否则会出现错行现象~~~
    self.commentTextView.font = [UIFont systemFontOfSize:24];
    self.placeHoldLabel.font = [UIFont systemFontOfSize:24];
    [self.commentTextView addSubview:self.placeHoldLabel];
    [self.commentTextView setValue:self.placeHoldLabel forKey:@"_placeholderLabel"];
上一篇下一篇

猜你喜欢

热点阅读