UILabel 上的文本填充 [英] Text padding on UILabel

查看:26
本文介绍了UILabel 上的文本填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里,我试图在文本周围设置一个带有一些填充(左、右、上和下)的标签.这个问题在 SOF 上有相关的帖子,在阅读了其中的一些之后,我尝试使用

如果我将这一行添加到上面的两行中:

label.lineBreakMode = .byWordWrapping

结果是:

我还设置了一些限制条件.所有这些都在 95% 的情况下有效.谁能看出是什么问题?

解决方案

尝试调用 invalidateIntrinsicContentSize:

var padding: UIEdgeInsets = UIEdgeInsets.zero {已设置{self.invalidateIntrinsicContentSize()}}

我尝试了不同的选择.如果您使用 layoutSubviews 中的 intrinsicContentSize 更新 frame size ,但我不知道是否有更好的方法它:

override func layoutSubviews() {super.layoutSubviews()self.frame.size = self.intrinsicContentSize}

Here, I am trying to have a label with some padding (left, right, top and bottom) around the text. This issue has related post on SOF and after reading a few of them, I tried using a solution proposed here:

This is the code for my subclassing UILabel:

import UIKit

class LuxLabel: UILabel {
    //let padding: UIEdgeInsets
    var padding: UIEdgeInsets = UIEdgeInsets.zero {
        didSet {
            self.invalidateIntrinsicContentSize()
        }
    }    

    // Create a new PaddingLabel instance programamtically with the desired insets
    required init(padding: UIEdgeInsets = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 10)) {
        self.padding = padding
        super.init(frame: CGRect.zero)
    }

    // Create a new PaddingLabel instance programamtically with default insets
    override init(frame: CGRect) {
        padding = UIEdgeInsets.zero // set desired insets value according to your needs
        super.init(frame: frame)
    }

    // Create a new PaddingLabel instance from Storyboard with default insets
    required init?(coder aDecoder: NSCoder) {
        padding = UIEdgeInsets.zero // set desired insets value according to your needs
        super.init(coder: aDecoder)
    }

    override func drawText(in rect: CGRect) {
        super.drawText(in: UIEdgeInsetsInsetRect(rect, padding))
    }

    // Override `intrinsicContentSize` property for Auto layout code
    override var intrinsicContentSize: CGSize {
        let superContentSize = super.intrinsicContentSize
        let width = superContentSize.width + padding.left + padding.right
        let heigth = superContentSize.height + padding.top + padding.bottom
        return CGSize(width: width, height: heigth)
    }
}

It is based on PaddingLabel (cf. the above link).

It is mostly working well, but for some reasons that I do not understand, there are cases where things go wrong and the display gets truncated.

This is an example:

The string to put on the label is:

"It has a square shape and a blue color."

The code to create the label is:

let label = LuxLabel(padding: UIEdgeInsets(top: 5, left: 10, bottom: 5, right: 10))
label.numberOfLines = 0

and this is the result:

If I add this line to the two above:

label.lineBreakMode = .byWordWrapping

the result is:

I have also set some constraints. All this works 95% of the time. Can anyone see what is the problem?

解决方案

Try calling invalidateIntrinsicContentSize:

var padding: UIEdgeInsets = UIEdgeInsets.zero {
    didSet {
        self.invalidateIntrinsicContentSize()
    }
}

EDIT:

I have tried different options. If you update the frame size with the intrinsicContentSize in layoutSubviews that make the trick but I don't know if there is a better way to do it:

override func layoutSubviews() {
    super.layoutSubviews()
    self.frame.size = self.intrinsicContentSize
}

这篇关于UILabel 上的文本填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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