旋转按钮标题标签文本

时间:2017-11-01 10:40:18

标签: ios swift uibutton

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

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 个答案:

答案 0 :(得分:2)

我在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

答案 1 :(得分:1)

为按钮“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
    }
}

答案 2 :(得分:1)

请考虑我们有以下按钮:

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()方法中实现了上述代码,该方法还应为单元格中的按钮提供所需的输出...

答案 3 :(得分:0)

试试这个:

cell.btnApply.titleLabel?.transform = view.transform.rotated(by: .pi)
相关问题