【问题标题】:How to change view for label如何更改标签的视图
【发布时间】:2019-05-01 16:34:10
【问题描述】:

我有一个UILabel,我想更改它的背景视图。有人建议我使用贝塞尔曲线,但我想要一个简单的变体。

我的标签:

它应该是什么样子:

【问题讨论】:

  • 最简单的非代码解决方案是创建图像并将其用作背景。对于非固定宽度,您可以拥有三个图像:左、中(可拉伸)、右并将它们放在水平堆栈视图中,其中标签将位于中间图像视图内,从而确定宽度。

标签: swift animation storyboard xib


【解决方案1】:

由于这些不是很复杂的形状,我建议创建两个白色的UIViews,将它们旋转 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)

【讨论】:

  • 而不是单元格高度应该是我的标签名称?
  • myLabel.frame.height
  • swift 如何理解这应该发生在标签的边缘
  • 哦,对不起,我最初误读了您的问题。是的,右边和左边的方块需要有widthheightmyLabel.frame.height
  • 我有 2 个标签
猜你喜欢
  • 1970-01-01
  • 2021-08-09
  • 2011-02-26
  • 2018-06-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-16
相关资源
最近更新 更多