【问题标题】:Aspect fit in cgsize programmatically in swift [closed]方面在 swift 中以编程方式适合 cgsize [关闭]
【发布时间】:2023-03-14 12:36:01
【问题描述】:

Click here for image.

需要帮助贴合标签,以便填充左右两侧。 代码在底部。

  @IBOutlet weak var header: UILabel!

override func viewDidLoad() {
    header.adjustsFontSizeToFitWidth = true

    let rectShape = CAShapeLayer()
    rectShape.bounds = self.header.frame
    rectShape.position = self.header.center
    rectShape.path = UIBezierPath(roundedRect: self.header.bounds, byRoundingCorners: [.bottomLeft , .bottomRight], cornerRadii: CGSize(width:300, height: 200)).cgPath

    self.header.layer.backgroundColor = UIColor.green.cgColor
    //Here I'm masking the textView's layer with rectShape layer
    self.header.layer.mask = rectShape


    super.viewDidLoad()

【问题讨论】:

  • 你想左右填充那个蓝色标题吗?

标签: swift xcode uilabel cgrect cgsize


【解决方案1】:

您可以通过将代码块放入viewDidAppear(_:) 来解决此问题。在这种方法中,尺寸会被修正。

@IBOutlet weak var header: UILabel!

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    header.adjustsFontSizeToFitWidth = true

    let rectShape = CAShapeLayer()
    rectShape.bounds = self.header.frame
    rectShape.position = self.header.center
    rectShape.path = UIBezierPath(roundedRect: self.header.bounds, byRoundingCorners: [.bottomLeft , .bottomRight], cornerRadii: CGSize(width:300, height: 200)).cgPath

    self.header.layer.backgroundColor = UIColor.green.cgColor
    //Here I'm masking the textView's layer with rectShape layer
    self.header.layer.mask = rectShape
}

【讨论】:

  • 谢谢!!这有帮助!
猜你喜欢
  • 1970-01-01
  • 2019-11-14
  • 1970-01-01
  • 2013-08-08
  • 2015-03-17
  • 1970-01-01
  • 2014-08-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多