首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Swift & UILabel :如何以编程方式在Swift中添加填充和边距?

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

Stack Overflow用户
提问于 2021-05-28 07:07:13
回答 3查看 5.8K关注 0票数 3

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

代码语言:javascript
运行
复制
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)

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

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2021-05-28 09:26:06

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

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

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

默认情况下,按钮中的文本放在中心,但是它的位置很容易用contentHorizontalAlignment和contentVerticalAlignment更改。

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

UPD。

你能给我举个简单的例子吗?我试过了,但没有达到我所期望的结果。-普纳达·拉尼

代码语言:javascript
运行
复制
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 :)

当然,如果愿意的话,你可以用一个故事板来做。

票数 5
EN

Stack Overflow用户

发布于 2021-07-15 05:07:17

1.添加这个类

PaddingLabel.swift

代码语言:javascript
运行
复制
import UIKit

class PaddingLabel: UILabel {

    var edgeInset: UIEdgeInsets = .zero

    override func drawText(in rect: CGRect) {
        let insets = UIEdgeInsets.init(top: edgeInset.top, left: edgeInset.left, bottom: edgeInset.bottom, right: edgeInset.right)
        super.drawText(in: rect.inset(by: insets))
    }

    override var intrinsicContentSize: CGSize {
        let size = super.intrinsicContentSize
        return CGSize(width: size.width + edgeInset.left + edgeInset.right, height: size.height + edgeInset.top + edgeInset.bottom)
    }
}

将此代码添加到您的ViewController

代码语言:javascript
运行
复制
let label = PaddingLabel()

override func viewDidLoad() {
    super.viewDidLoad()
    
    label.backgroundColor = UIColor().colorFromHex(hex: "#2C333C", opacity: 0.4)
    
    //Setting the padding label
    label.edgeInset = UIEdgeInsets(top: 5, left: 10, bottom: 5, right: 10)   
}

下面链接的答案是,我基于故事板编写了相同的内容。

票数 1
EN

Stack Overflow用户

发布于 2021-08-12 08:34:15

我用textfield。设置文本字段中的填充和文本。也不允许编辑。

代码语言:javascript
运行
复制
extension UITextField {
    
    func addLeftPadding() {
        
        let paddingView = UIView(frame: CGRect(x: 0, y: 0, width: 12, height: self.frame.height))
        self.leftView = paddingView
        self.leftViewMode = ViewMode.always
    }
}
代码语言:javascript
运行
复制
//ViewController

@IBOutlet weak var myTextField: UITextField!

override func viewDidLoad() {
  super.viewDidLoad()
  myTextField.addLeftPadding()
  myTextField.isUserInteractionEnabled = false
  myTextField.text = "your label text"
}
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67734304

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档