代码之家  ›  专栏  ›  技术社区  ›  Deepti Raghav

旋转按钮标题标签文本

  •  1
  • Deepti Raghav  · 技术社区  · 8 年前

    我希望按钮上的标题看起来像:

    enter image description here

    到目前为止,我在表视图单元格中所做的是:

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        let cell  = tableView.dequeueReusableCell(withIdentifier: "CellJob") as!JobPostTbleCell
         cell.btnApply.titleLabel?.transform = CGAffineTransform(rotationAngle: CGFloat.pi / 2)
        cell.btnApply.titleLabel?.backgroundColor = UIColor.lightGray
        return cell
    
    }
    

    我明白了:

    enter image description here

    如何实现所需的输出?

    4 回复  |  直到 8 年前
        1
  •  2
  •   Deepti Raghav    8 年前

    我在cellForRowAt indexPath中添加了这段代码

     cell.btnApply.titleLabel?.transform = CGAffineTransform(rotationAngle: -(CGFloat.pi / 2))
        cell.btnApply.titleLabel?.textAlignment = .center
        cell.btnApply.titleLabel!.numberOfLines = 1
    

    enter image description here

        2
  •  1
  •   SPatel mash    8 年前

    为按钮“RotatableButton”设置自定义类

    @IBDesignable class RotatableButton: UIButton {
        @IBInspectable var angle:CGFloat = 0 {
            willSet {
                rotate(angle: newValue)
            }
        }
        func rotate(angle: CGFloat) {
            let radians = angle / 180.0 * CGFloat.pi
            let rotation = self.transform.rotated(by: radians)
            self.titleLabel?.transform = rotation
        }
    }
    
        3
  •  1
  •   Ahmad F    8 年前

    假设我们有以下按钮:

    enter image description here

    如您所见,按钮的宽度小于标题标签的宽度,但这在旋转高度时不适用,它应该以全宽度显示(即使旋转按钮后,按钮的高度也比标签的宽度大)。

    myBtn.titleLabel?.transform = CGAffineTransform(rotationAngle: -(CGFloat.pi / 2))
    myBtn.titleLabel!.numberOfLines = 0
    myBtn.titleLabel!.adjustsFontSizeToFitWidth = true
    

    输出为:

    enter image description here

    viewDidLoad() 方法,该方法还应为单元格中的按钮提供所需的输出。。。

        4
  •  0
  •   TechBee    8 年前

    试试这个:

    cell.btnApply.titleLabel?.transform = view.transform.rotated(by: .pi)