首页 > 解决方案 > 如何从一侧弯曲或扭曲 UILabel 文本

问题描述

我使用了属性化字符串和库,但找不到任何适合标签文本效果的解决方案。

任何人都可以建议我如何实现此功能。?

我想要这张图片的标签效果

谢谢

标签: iosswiftxcodeuilabelnsattributedstring

解决方案


改编代码来自如何将透视变换应用于 UIView?,这里是一些示例 Swift 4 代码,它们将透视应用于标签,给出与您的图像相似的结果。

let pview = UIView(frame: CGRect(x: 0, y: 0, width: 400, height: 300))
pview.backgroundColor = .black
let plabel = UILabel(frame: CGRect(x: 0, y: 110, width: 250, height: 75))
pview.addSubview(plabel)
plabel.text = " ND420 "
plabel.textAlignment = .center
plabel.font = UIFont.boldSystemFont(ofSize: 72)
plabel.textColor = .orange
plabel.backgroundColor = .red
plabel.sizeToFit()
let layer = plabel.layer
var transform = CATransform3DIdentity
transform.m34 = 1.0 / -200
transform = CATransform3DRotate(transform, -45 * CGFloat.pi / 180, 0, 1, 0)
layer.transform = transform

在操场上运行它并查看pview.

使用分配给的值transform.m34和 中的旋转角度CATransform3DRotate。负旋转角度(如上图)使左侧变小而右侧变大。正角则相反。

在此处输入图像描述


推荐阅读