UITextView根据内容适应大小【转】

oc

- (void)textViewDidChange:(UITextView *)textView{

CGFloat fixedWidth = textView.frame.size.width;

CGSize newSize = [textView sizeThatFits:CGSizeMake(fixedWidth, MAXFLOAT)];

CGRect newFrame = textView.frame;

newFrame.size= CGSizeMake(fmaxf(newSize.width, fixedWidth), newSize.height);

textView.frame= newFrame;

}

swift

let fixedWidth = textView.frame.size.widthtextView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.max))

let newSize = textView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.max))

var newFrame = textView.frame

newFrame.size= CGSize(width: max(newSize.width, fixedWidth), height: newSize.height)

textView.frame= newFrame;

查看原文

你可能感兴趣的:(UITextView根据内容适应大小【转】)