Aspect在swift中以编程方式适合cgsize

时间:2018-05-19 03:30:15

标签: swift xcode uilabel cgrect cgsize

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

1 个答案:

答案 0 :(得分:0)

您可以将代码块放入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
}