如何在 UITextView 中丢失边距/填充 [英] How to lose margin/padding in UITextView

查看:30
本文介绍了如何在 UITextView 中丢失边距/填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 iOS 应用程序中有一个 UITextView,用于显示大量文本.

然后我将使用 UITextView 的偏移边距参数对文本进行分页.

我的问题是 UITextView 的填充混淆了我的计算,因为它似乎根据我使用的字体大小和字体而不同.

是否可以删除围绕 UITextView 内容的填充?

解决方案

2021 年更新

这是 iOS 中最愚蠢的错误之一.

此处给出的类 UITextViewFixed 被广泛使用,通常是整体上最合理的解决方案.

这是课程:

@IBDesignable class UITextViewFixed: UITextView {覆盖 func layoutSubviews() {super.layoutSubviews()设置()}功能设置(){textContainerInset = UIEdgeInsets.zerotextContainer.lineFragmentPadding = 0}}

不要忘记在检查器中关闭 scrollEnabled!

  1. 解决方案在故事板中正常工作

  2. 该解决方案在运行时正常运行

就是这样,你完成了.

一般来说,在大多数情况下,这应该就是您所需要的.

即使您正在动态地更改文本视图的高度UITextViewFixed 通常都能满足您的需求.

(动态更改高度的一个常见示例是在用户键入时更改高度.)

这是来自 Apple 的损坏的 UITextView...

这是UITextViewFixed:

请注意,您当然必须...

...在检查器中关闭 scrollEnabled!

(启用 scrollEnabled 意味着通过尽可能多地扩展底部边距,使此视图尽可能垂直扩展.")


一些进一步的问题

(1) 在一些非常不寻常的动态改变高度的情况下,Apple 做了一件奇怪的事情:他们在底部增加了额外的空间.

不,真的!这一定是 iOS 中最令人恼火的事情之一.

如果您遇到问题,这里是一个快速修复";这通常有帮助:

<代码>...textContainerInset = UIEdgeInsets.zerotextContainer.lineFragmentPadding = 0//这并不理想,但有时这种快速修复"//将解决底部的额外空间";疯狂:var b = 边界让 h = sizeThatFits(CGSize(宽度:bounds.size.width,高度:CGFloat.greatestFiniteMagnitude)).高度b.size.height = h界限 = b...

(2) 在极少数情况下,要解决 Apple 的另一个微妙问题,您必须添加:

override func setContentOffset(_ contentOffset: CGPoint, animation: Bool) {super.setContentOffset(contentOffset, animation: false)//原文如此}

(3) 可以说,我们应该添加:

contentInset = UIEdgeInsets.zero

就在 .lineFragmentPadding = 0 之后,在 UITextViewFixed 中.

然而……不管你信不信……在当前的 iOS 中根本行不通!(2021 年检查.)将来可能需要添加该行.

UITextView 在 iOS 中被破坏的事实是所有移动计算中最奇怪的事情之一.这个问题十周年了,还没解决!

最后,这里有一个类似 TextField 的提示:

如果你想让它消失",只需添加这个

override var internalContentSize: CGSize {var i = super.intrinsicContentSize打印(对于(文本)大小将是(i)")如果文本=="{ i.height = 1.0 }print("但是我们把它改成了(i)")返回我}

(我把它设为1"高度,所以很清楚那个演示中发生了什么,0"很好.)

UILabel 怎么样?

当只显示文本时,UILabel 比 UITextView 有很多优点.UILabel 不会遇到此问答页面中描述的问题.

这确实是我们通常放弃"的原因并且只使用 UITextView 是 UILabel 很难使用.特别是将填充正确地添加到 UILabel 是非常困难的.

事实上,这里是关于如何最终"完成的完整讨论.正确地向 UILabel 添加填充:向 UILabel 添加空间/填充.在某些情况下,如果您正在使用动态高度单元格进行复杂的布局,有时最好使用 UILabel 进行困难的布局.

I have a UITextView in my iOS application, which displays a large amount of text.

I am then paging this text by using the offset margin parameter of the UITextView.

My problem is that the padding of the UITextView is confusing my calculations as it seems to be different depending on the font size and typeface that I use.

Is it possible to remove the padding surrounding the content of the UITextView?

解决方案

Up-to-date for 2021

It is one of the silliest bugs in iOS.

The class given here, UITextViewFixed , is used widely, and is usually the most reasonable solution overall.

Here is the class:

@IBDesignable class UITextViewFixed: UITextView {
    override func layoutSubviews() {
        super.layoutSubviews()
        setup()
    }
    func setup() {
        textContainerInset = UIEdgeInsets.zero
        textContainer.lineFragmentPadding = 0
    }
}

Don't forget to turn off scrollEnabled in the Inspector!

  1. The solution works properly in storyboard

  2. The solution works properly at runtime

That's it, you're done.

In general, that should be all you need in most cases.

Even if you are changing the height of the text view on the fly, UITextViewFixed usually does all you need.

(A common example of changing the height on the fly, is changing it as the user types.)

Here is the broken UITextView from Apple...

Here is UITextViewFixed:

Note that of course you must...

...turn off scrollEnabled in the Inspector!

(Turning on scrollEnabled means "make this view expand as much as possible vertically by expanding the bottom margin as much as possible.")


Some further issues

(1) In some very unusual cases dynamically changing heights, Apple does a bizarre thing: they add extra space at the bottom.

No, really! This would have to be one of the most infuriating things in iOS.

If you encounter the problem, here is a "quick fix" which usually helps:

...
        textContainerInset = UIEdgeInsets.zero
        textContainer.lineFragmentPadding = 0

        // this is not ideal, but sometimes this "quick fix"
        // will solve the "extra space at the bottom" insanity:
        var b = bounds
        let h = sizeThatFits(CGSize(
           width: bounds.size.width,
           height: CGFloat.greatestFiniteMagnitude)
       ).height
       b.size.height = h
       bounds = b
 ...

(2) In rare cases, to fix yet another subtle mess-up by Apple, you have to add:

override func setContentOffset(_ contentOffset: CGPoint, animated: Bool) {
    super.setContentOffset(contentOffset, animated: false) // sic
}

(3) Arguably, we should be adding:

contentInset = UIEdgeInsets.zero

just after .lineFragmentPadding = 0 in UITextViewFixed.

However ... believe or not ... that just doesn't work in current iOS! (Checked 2021.) It may be necessary to add that line in the future.

The fact that UITextView is broken in iOS is one of the weirdest things in all of mobile computing. Ten year anniversary of this question and it's still not fixed!

Finally, here's a somewhat similar tip for TextField: Set the maximum character length of a UITextField in Swift

Completely random tip: how to add the "..." on the end

Often you are using a UITextView "like a UILabel". So you want it to truncate text using an ellipsis, "..."

If so, add:

 textContainer.lineBreakMode = .byTruncatingTail

Handy tip if you want zero height, when, there's no text at all

Often you use a text view to only display text. So, you use lines "0" to mean the text view will automatically change height depending on how many lines of text.

That's great. But if there is no text at all, then unfortunately you get the same height as if there is one line of text!!!! The text view never "goes away".

If you want it to "go away", just add this

override var intrinsicContentSize: CGSize {
    var i = super.intrinsicContentSize
    print("for (text) size will be (i)")
    if text == "" { i.height = 1.0 }
    print("   but we changed it to (i)")
    return i
}

(I made it '1' height, so it's clear what's going on in that demo, '0' is fine.)

What about UILabel?

When just displaying text, UILabel has many advantages over UITextView. UILabel does not suffer from the problems described on this Q&A page.

Indeed the reason we all usually "give up" and just use UITextView is that UILabel is difficult to work with. In particular it is ridiculously difficult to just add padding, correctly, to UILabel.

In fact here is a full discussion on how to "finally" correctly add padding to UILabel: Adding space/padding to a UILabel. In some cases if you are doing a difficult layout with dynamic height cells, it is sometimes better to do it the hard way with UILabel.

这篇关于如何在 UITextView 中丢失边距/填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆