动态调整 UITextView 的宽度和高度

Posted

技术标签:

【中文标题】动态调整 UITextView 的宽度和高度【英文标题】:dynamically resizing UITextView width and height 【发布时间】:2013-03-21 20:41:07 【问题描述】:

我的代码会根据文本动态调整。当我们按下回车键时,它会增加高度 除非我们按回车键,否则会增加宽度。

但是我该怎么做呢

- (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)];



但即使我按下回车它也会增加宽度。如何预防。

【问题讨论】:

【参考方案1】:

好的,宽度是所有行中整个文本的宽度。所以必须找到最大线长。

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text

CGFloat height = [textView.text sizeWithFont:textView.font].height;
//CGFloat lineWidth;

int charsAsInt;
 if ([[NSCharacterSet newlineCharacterSet] characterIsMember:[text characterAtIndex:text.length-1]])
 
     self.lineWidth = 0;
     newLine = @"";
     self.textViewHeight += height;
     NSLog(@"height: %f, width: %f, new Height: %f", height, self.textViewWidth, self.textViewHeight);
     [textView setFrame:CGRectMake(self.contentView.frame.origin.x, self.contentView.frame.origin.y, self.textViewWidth, self.textViewHeight)];
     self.contentView.frame = textView.frame;
     self.frame = textView.frame;
     [_editButton setFrame:CGRectMake(self.contentView.frame.size.width - 14, 0, 28, 28)];

     [super setFrame:textView.frame];
 
 else
     newLine = [newLine stringByAppendingString:text];
     self.lineWidth = [newLine sizeWithFont:textView.font].width;
     charsAsInt = (int)(newLine.length);
     CGFloat charWidth = self.lineWidth / charsAsInt;


     if(self.lineWidth >= self.textViewWidth)
         self.textViewWidth = self.lineWidth;
         [textView setFrame:CGRectMake(self.contentView.frame.origin.x, self.contentView.frame.origin.y, self.lineWidth, self.textViewHeight)];
     

     else
         [textView setFrame:CGRectMake(self.contentView.frame.origin.x, self.contentView.frame.origin.y, self.textViewWidth, self.textViewHeight)];
     
     self.contentView.frame = textView.frame;
     self.frame = textView.frame;
     [_editButton setFrame:CGRectMake(self.contentView.frame.size.width - 14, 0, 28, 28)];
     //[super setContentView:self.contentView];
     [super setFrame:textView.frame];


return YES;

发生的事情是我们有一个多行 UITextView 贴纸,所以当你在键盘上按回车键时,你有一个新行。我现在将其存储在一个新变量中。只有当新行的宽度大于旧行的宽度时,整个 textVIew 的宽度才会增加(self.textViewWidth)

【讨论】:

以上是关于动态调整 UITextView 的宽度和高度的主要内容,如果未能解决你的问题,请参考以下文章

如何动态调整 UITextView 的高度

如何根据内容调整 UITextView 的大小,宽度固定

使用自动布局的 UITableView 中的动态 UITextView 高度?

动态更改 UITextField 宽度取决于内容

在 UITableViewCell ios 中制作动态 UILabel 高度/宽度?

表视图内的 UITextView 未根据内容调整大小