如何更改标签的视图

时间:2018-11-29 15:06:35

标签: swift animation storyboard xib

我有标签,并且想要更改其视图。有人建议我使用贝塞尔曲线,但我想要简单的变体。

我的标签:

it is my lable

应该是什么样子

what should look like

1 个答案:

答案 0 :(得分:-1)

由于这些形状不是很复杂,因此建议创建两个白色UIView,将它们旋转45度,然后将它们作为子视图添加到UILabel中。代码看起来像这样:

    myLabel.clipsToBounds = true
    // create the triangle on the left side of the label
    let leftSquare = UIView(frame: CGRect(x: myLabel.frame.height / -2, y: 0, width: myLabel.frame.height, height: myLabel.frame.height))
    leftSquare.translatesAutoresizingMaskIntoConstraints = true
    leftSquare.isUserInteractionEnabled = false
    leftSquare.backgroundColor = UIColor.white
    leftSquare.transform = CGAffineTransform(rotationAngle: CGFloat(Double.pi / 4)) // 45 degree rotation
    // create the triangle on the right side of the cell
    let rightSquare = UIView(frame: CGRect(x: myLabel.bounds.width - (myLabel.frame.height / 2), y: 0, width: myLabel.frame.height, height: myLabel.frame.height))
    rightSquare.translatesAutoresizingMaskIntoConstraints = true
    rightSquare.isUserInteractionEnabled = false
    rightSquare.backgroundColor = UIColor.white
    rightSquare.transform = CGAffineTransform(rotationAngle: CGFloat(Double.pi / 4))
    // add squares to label
    myLabel.addSubview(leftSquare)
    myLabel.addSubview(rightSquare)
    myLabel.sendSubview(toBack: leftSquare)
    myLabel.sendSubview(toBack: rightSquare)