ios - swift 和 UILabel : How to add padding and margin in Swift programmatically?

标签 ios swift uikit storyboard

<分区>

我使用 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
    }
}

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

期待您的来信。谢谢。

最佳答案

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

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

为了让 UIButton 不充当按钮,只需设置 button.isUserInteractionEnabled = false。

默认情况下,按钮中的文本位于中心,但它的位置很容易通过 contentHorizo​​ntalAlignment 和 contentVerticalAlignment 改变

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

更新。

Could you give me a simple example? I tried that way but I didn't get the result I expected. – Punreach Rany

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

当然,如果愿意,您可以使用 Storyboard来完成。

关于ios - swift 和 UILabel : How to add padding and margin in Swift programmatically?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67734304/

相关文章:

android - 在 react-native 中包含 react-vr

swift - 动画 View 以隐藏/缩放到其右下角

ios - 为什么我的图像没有缓存在 AlamofireImage 中?

swift - 如何在 Xcode 源代码编辑器扩展中获取当前工作区路径?

ios - 为什么我的 Auth0 token 从 iOS 发送到 node.js 服务器时总是返回无效?

ios - 将自定义 cordova 插件添加到 IBM Worklight 6.1

ios - 如何在用户返回主屏幕后每次重新启动 iOS 应用程序?

ios - UIKit 中的大型弹出 View 叫什么?

ios - 如何将 subview 放在不同屏幕的正确位置

ios - 如何使用 CFTimeInterval 为动画指定开始时间?