【问题标题】:iOS remove extra space below UITextView contentiOS 移除 UITextView 内容下方的额外空间
【发布时间】:2016-07-20 16:55:04
【问题描述】:

我有一个 UITextView 在情节提要视图控制器内启用了自动布局。我想根据UITextView 的文本动态增加它的高度。我可以增加它的大小,但文本下方有额外的空间。如何删除这个多余的空间?

我尝试了这些代码行:

[_descriptionText setText:string]; 
CGSize sizeThatFitsTextView = [_descriptionText sizeThatFits:CGSizeMake(_descriptionText.frame.size.width, MAXFLOAT)]
_descriptionTextHeight.constant = sizeThatFitsTextView.height;

这里_descriptionTextUITextView_descriptionTextHeight 是我用来调整UITextView 大小的高度限制。

我也试过这些代码行:

[_descriptionText setText:string];
_descriptionTextHeight.constant = [_descriptionText intrinsicContentSize].height;

我能够删除多余的空间,但 UITextView 的大小没有增加。 这里还要补充一点,我不想启用滚动。

【问题讨论】:

  • 感谢马特的建议。我可以减去,它的工作,但问题是文本不会被修复。它根据内容增加底部空间。假设如果有 10 个文本行,那么会有更多的空间。所以用常数值减去是无济于事的。

标签: ios objective-c uitextview


【解决方案1】:

试试这个,一定对你有用。

CGSize sizeThatFitsTextView = [txt sizeThatFits:CGSizeMake(txt.frame.size.width, MAXFLOAT)];

_descriptionText.contentOffset = CGPointZero;
_descriptionText.contentInset = UIEdgeInsetsMake(-6,0,0,0);
[txt layoutIfNeeded]; //added

NSLayoutConstraint *heightConstraint;
for (NSLayoutConstraint *constraint in _descriptionText.constraints) {
    if (constraint.firstAttribute == NSLayoutAttributeHeight) {
        heightConstraint = constraint;
        break;
    }
}
heightConstraint.constant = sizeThatFitsTextView.height-12;


[_descriptionText layoutIfNeeded];

【讨论】:

  • 它有助于删除额外的底部空间,但 UITextView 没有扩展。
  • 你解释的是描述和标题是关于:iOS在加载新字符串时删除UITextView内容下方的额外空间,调用此方法,它将显示新文本展开。你到底在找什么?添加长文本->从中显示短文本,然后在操作或其他内容时显示所有文本?是吗?
  • 如果您正在寻找字符串的动态高度,只需在 textView 中更新您的字符串后调用它。
  • 正如我在描述中提到的,我想删除文本下方的底部空间,并且我想根据内容大小扩展 UITextView。您的代码没有扩展 UITextView。它只显示第一行内容。
  • 我只是添加了一个按钮来通过增加字符串和扩展TextView来扩展。请检查这个wetransfer.com/downloads/…
【解决方案2】:

我不知道你为什么在这方面遇到困难。以下代码适用于我:

func adjustHeight(_ tv:UITextView) {
    self.heightConstraint.constant = ceil(tv.contentSize.height)
}

func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
    self.adjustHeight(textView)
    return true
}

【讨论】:

    【解决方案3】:

    创建 UITextView 的子类并添加以下代码:

    - (void)layoutSubviews
    {
        [super layoutSubviews];
    
        // Layout subviews gets called when the text view's text changes in a way that might
        // require scrolling.   We get its new intrinsic size and update the height constraint
        // to grow the text view.
        CGSize intrinsicSize = self.intrinsicContentSize;
        self.heightConstraint.constant = MAX(intrinsicSize.height, self.minimumHeight);
    
        // The text view has a tendency to scroll up once it's full, but before the
        // height constraint that increases its height takes effect.   This results
        // in a blank area at the bottom, with the first line pushed out of view.
        // We cancel the scroll action, but only if it has not reached its maximum height.
        if (intrinsicSize.height < self.maximumHeight) {
            self.contentOffset = CGPointMake(0, 0);
        }
    }
    

    您还需要创建这些属性。这些控制文本视图的高度,并设置它可以变得多短和多高的限制。

    /**
     The height constraint for the text view installed in the text view's superview.
     */
    @property (weak,nonatomic) NSLayoutConstraint *heightConstraint;
    
    /**
     The text view's minimum height.
     */
    @property (nonatomic) CGFloat minimumHeight;
    
    /**
     The text view's maximum height.
     */
    @property (nonatomic) CGFloat maximumHeight;
    

    像往常一样将文本视图添加到视图层次结构中,为文本视图的高度创建约束,并设置其最小和最大高度属性,一切就绪。

    【讨论】:

    • @Tech Immortal:你需要文本视图是可编辑的吗?如果您只希望它显示文本并且不需要它可编辑,请将其保留为可滚动但设置userInteractionEnabled = NO
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-15
    相关资源
    最近更新 更多