我的代码根据文本动态调整。当我们按下回车键时它会增加高度并增加宽度,除非我们按下回车键。
但是我该怎么做
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
CGFloat height = [textView.text sizeWithFont:textView.font].height;
CGFloat width = [textView.text sizeWithFont:textView.font].width;
if ([[NSCharacterSet newlineCharacterSet] characterIsMember:[text characterAtIndex:text.length-1]])
{
self.textViewHeight += height;
[textView setFrame:CGRectMake(self.contentView.frame.origin.x, self.contentView.frame.origin.y, width, self.textViewHeight)];
self.contentView.frame = textView.frame;
self.frame = textView.frame;
[_editButton setFrame:CGRectMake(self.contentView.frame.size.width - 14, 0, 28, 28)];
}
else{
int charsAsInt = (int)(textView.text.length);
CGFloat charWidth = width / charsAsInt;
[textView setFrame:CGRectMake(self.contentView.frame.origin.x, self.contentView.frame.origin.y, width+ charWidth, self.textViewHeight)];
self.contentView.frame = textView.frame;
self.frame = textView.frame;
[_editButton setFrame:CGRectMake(self.contentView.frame.size.width - 14, 0, 28, 28)];
}
但即使我按下回车它也会增加宽度。如何防止这种情况。