~Onlooker 2009-04-14 07:42 采纳率: 0%
浏览 422
已采纳

如何在 UITextView 中丢失空白 / 填充?

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.

Therefore, I pose the question: Is it possible to remove the padding surrounding the content of the UITextView?

Look forward to hearing your responses!

转载于:https://stackoverflow.com/questions/746670/how-to-lose-margin-padding-in-uitextview

  • 写回答

19条回答 默认 最新

  • ~Onlooker 2017-02-19 22:45
    关注

    For 2018

    It is one of the silliest bugs in iOS.

    The class UITextViewFixed is a usually a reasonable solution.

    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 does work properly in storyboard

    2. The solution does work 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 this 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...

    screenshot of IB with UITextView

    Here is UITextViewFixed:

    screenshot of IB with UITextViewFixed

    Note that of course you must

    turn off scrollEnabled in the Inspector!

    Don't forget to turn off scrollEnabled! :)


    Some further issues

    (1) In some unusual cases - example, some cases of flexible cell height table views with dynamically changing height - Apple does the most infuriating thing in the world: they add extra space at the bottom. No, really! This would have to be one of the most infuriating things in iOS.

    Here is a "quick fix" to add which often helps with that madness.

    ...
            textContainerInset = UIEdgeInsets.zero
            textContainer.lineFragmentPadding = 0
    
            // this is not ideal, but you can sometimes use this
            // to fix the "space at bottom" insanity
            var b = bounds
            let h = sizeThatFits(CGSize(
               width: bounds.size.width,
               height: CGFloat.greatestFiniteMagnitude)
           ).height
           b.size.height = h
           bounds = b
     ...
    

    (2) Sometimes, to fix yet another subtle Apple mess-up, you have to add this:

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

    (3) Arguably, we should be adding :

    contentInset = UIEdgeInsets.zero
    

    just after .lineFragmentPadding = 0 in UITextViewFixed . However ... it just doesn't work in current iOS! It may be necessary to add that in the future.

    The fact that UITextView is totally broken in iOS is one of the weirdest things in all of mobile computing.

    Finally, here's a somewhat similar tip for TextField: https://stackoverflow.com/a/43099816/294884

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(18条)

报告相同问题?

悬赏问题

  • ¥15 C#调用python代码(python带有库)
  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B