-3

点击这里查看图片。

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

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

1 回答 1

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
}
于 2018-05-19T04:43:37.267 回答