Swift 和 UILabel:如何以编程方式在 Swift 中添加填充和边距?

Pun*_*any 3 storyboard uikit ios swift

我使用 UILabel 以编程方式创建了一个带有灰色背景的文本。现在我想添加padding到这一段/文本中。另外,如果您能向我展示如何添加margin到我的UILabel中,那就太好了。

import UIKit

final class SignUpViewController: UIViewController {
    
    public let identifier = "Sign Up"
    
    private let logoImage : UIImageView = {
        let imageView = UIImageView()
        imageView.layer.masksToBounds = true
        imageView.contentMode = .scaleAspectFit
        imageView.image = UIImage(named: "MyLogoWithTitle")
        imageView.clipsToBounds = true
        return imageView
    }()
    
    private let instructionText : UILabel = {
        let label = UILabel()
        label.text = "Please read terms and conditions below carefully before proceeding with the registration."
        label.backgroundColor = UIColor().colorFromHex(hex: "#2C333C", opacity: 0.4)
        label.numberOfLines = 0
        label.tintColor = .white
        return label
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view.
        view.addSubview(logoImage)
        view.addSubview(instructionText)
        view.backgroundColor = UIColor().colorFromHex(hex: "#141920", opacity: 1.0)
    
    }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        
        logoImage.frame = CGRect(x: 0,
                                 y: 0,
                                 width: 140,
                                 height: 60)
        logoImage.center = CGPoint(x: view.center.x, y: view.height/5)
        
        instructionText.frame = CGRect(
            x: 5,
            y: 5 + logoImage.bottom,
            width: view.width - 20,
            height: 50)
            .integral
        instructionText.layer.cornerRadius = 10
    }
}
Run Code Online (Sandbox Code Playgroud)

请注意,我创建了 UIColor 的扩展,以便我可以通过这种方式输入十六进制颜色 - UIColor().colorFromHex(hex: "#2C333C", opacity: 0.4)

我期待着您的回音。谢谢。

Dzm*_*kov 6

您可以将此 UILabel 插入到容器(任何 UIView)中并设置其在内部的位置。

\n

但最简单的技巧是使用 UIButton 而不是 UILabel。您可以配置 UIEdgeInsets 进行填充。

\n

这样 UIButton 就不会充当按钮,只需设置 button.isUserInteractionEnabled = false 即可。

\n

默认情况下,按钮中的文本放置在中心,但其位置很容易通过 contentHorizo​​ntalAlignment 和 contentVerticalAlignment 更改

\n

作为奖励,您可以在文本附近添加图标。:)

\n

UPD。

\n
\n

你能给我一个简单的例子吗?我尝试了这种方式,但没有得到我预期的结果。\xe2\x80\x93 庞里奇·兰尼

\n
\n
let buttonUsedAsLabel = UIButton()\n// Your question was about padding\n// It's it!\nbuttonUsedAsLabel.titleEdgeInsets = UIEdgeInsets(top: 5, left: 20, bottom: 5, right: 20)\n// Make it not user interactable as UILabel is\nbuttonUsedAsLabel.isUserInteractionEnabled = false\n// set any other properties\nbuttonUsedAsLabel.setTitleColor(.white, for: .normal)\nbuttonUsedAsLabel.contentVerticalAlignment = .top\nbuttonUsedAsLabel.contentHorizontalAlignment = .left\n// Set title propeties AFTER it was created with text because it's nullable\n// You can use attributed title also\n// Never use any (button.titleLabel) before its creation\n// for example: (button.titleLabel?.text = "zzz") do nothing here\nbuttonUsedAsLabel.setTitle("This is the text", for: .normal)\nbuttonUsedAsLabel.titleLabel?.font = .systemFont(ofSize: 20, weight: .medium)\nbuttonUsedAsLabel.titleLabel?.numberOfLines = 0\nbuttonUsedAsLabel.titleLabel?.lineBreakMode = .byWordWrapping\n// and so on\n//    ...\n// This is the tri\xd1\x81k :)\n
Run Code Online (Sandbox Code Playgroud)\n

当然,如果愿意的话,您可以使用故事板来完成。

\n