简体   繁体   English

如何向 UILabel 添加填充(和背景)

[英]How can I add padding (and a background) to a UILabel

This has been asked before on Stackoverflow but the solutions don't work for me.之前在 Stackoverflow 上已经问过这个问题,但这些解决方案对我不起作用。

I'm building an app in SwiftUI but I need to use MapKit for maps, so I'm creating some views in UIKit (programmatically, no interface builder).我正在 SwiftUI 中构建一个应用程序,但我需要将 MapKit 用于地图,所以我在 UIKit 中创建了一些视图(以编程方式,没有界面构建器)。 My current problem is that I want to make a capsule background with some padding around a UILabel.我目前的问题是我想在 UILabel 周围制作一个带有一些填充的胶囊背景。 It needs to accommodate arbitrary text, so I can't hardcode the sizes, but I can't find a way to determine the innate size of the UILabel text at runtime.它需要容纳任意文本,因此我无法对大小进行硬编码,但我找不到在运行时确定 UILabel 文本的固有大小的方法。 I've tried UIEdgeInsets without success.我试过 UIEdgeInsets 没有成功。

In the attached code, I am showing a SwiftUI version of what I'm trying to achieve, and then the UIKit attempt.在附加的代码中,我展示了我正在尝试实现的 SwiftUI 版本,然后是 UIKit 尝试。 I'd like to follow best practices, so please feel free to tell me any better ways of achieving this.我想遵循最佳实践,所以请随时告诉我实现这一目标的任何更好方法。

(Screenshot of what the code produces) (代码生成的屏幕截图)

import SwiftUI

struct SwiftUICapsuleView: View {
    var body: some View {
        Text("Hello, World!")
            .padding(6)
            .background(Color.gray)
            .cornerRadius(15)
    }
}

struct UIKitCapsuleView: UIViewRepresentable {
    func makeUIView(context: Context) -> UIView {
        let view = UIView(frame: .zero)
        view.translatesAutoresizingMaskIntoConstraints = false

        let label = UILabel(frame: .zero)
        label.numberOfLines = 0
        label.font = UIFont.systemFont(ofSize: 17)
        label.text = "Goodbye, World!"
        label.layer.cornerRadius = 15
        label.layer.backgroundColor = UIColor.gray.cgColor
        label.translatesAutoresizingMaskIntoConstraints = false
        
        view.addSubview(label)
        label.heightAnchor.constraint(equalTo: view.heightAnchor).isActive = true
        label.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true

        return view
    }

    func updateUIView(_ view: UIView, context: Context) {
    }
}

struct ExperimentView_Previews: PreviewProvider {
    static var previews: some View {
        VStack {
            SwiftUICapsuleView()
            UIKitCapsuleView()
        }
    }
}

You probably want to set the background color and rounded corners of the view , not the label .您可能想要设置view的背景颜色和圆角,而不是label

You also should use a full set of constraints.您还应该使用一整套约束。

Give this a try:试试这个:

struct UIKitCapsuleView: UIViewRepresentable {
    func makeUIView(context: Context) -> UIView {
        let view = UIView(frame: .zero)
        view.translatesAutoresizingMaskIntoConstraints = false
        
        // set the view's background color
        view.backgroundColor = .cyan

        // set the cornerRadius on the view's layer
        view.layer.cornerRadius = 15
        
        let label = UILabel(frame: .zero)
        label.numberOfLines = 0
        label.font = UIFont.systemFont(ofSize: 17)
        label.text = "Goodbye, World!"
        
        label.translatesAutoresizingMaskIntoConstraints = false
        
        view.addSubview(label)

        // you can adjust padding here
        let padding: CGFloat = 6
        
        // use full constraints
        NSLayoutConstraint.activate([
            label.topAnchor.constraint(equalTo: view.topAnchor, constant: padding),
            label.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: padding),
            label.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -padding),
            label.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -padding),
        ])

        return view
    }
    
    func updateUIView(_ view: UIView, context: Context) {
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM