【问题标题】:How can I add padding (and a background) to a UILabel如何向 UILabel 添加填充(和背景)
【发布时间】:2020-12-01 09:21:54
【问题描述】:

以前在 Stackoverflow 上已经问过这个问题,但解决方案对我不起作用。

我在 SwiftUI 中构建一个应用程序,但我需要使用 MapKit 来制作地图,所以我在 UIKit 中创建了一些视图(以编程方式,没有界面构建器)。我目前的问题是我想在 UILabel 周围制作一个带有一些填充的胶囊背景。它需要容纳任意文本,因此我无法对大小进行硬编码,但我找不到在运行时确定 UILabel 文本的固有大小的方法。我试过 UIEdgeInsets 没有成功。

在附加的代码中,我展示了我正在尝试实现的 SwiftUI 版本,然后是 UIKit 尝试。我想遵循最佳做法,所以请随时告诉我任何更好的方法来实现这一点。

(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()
        }
    }
}

【问题讨论】:

标签: ios swift swiftui uikit


【解决方案1】:

您可能想要设置view 的背景颜色和圆角,而不是label

您还应该使用一整套约束。

试试这个:

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) {
    }
}

【讨论】:

    猜你喜欢
    • 2023-03-24
    • 1970-01-01
    • 2015-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多