Search code examples
iosswiftxcodeuilabelnsattributedstring

How to Bend or distortion a UILabel Text from one side


I have used attributed String and Libraries but unable to find any proper solution for Label Text effect.

Can Anyone please suggest me how i can achieve this functionality.?

i want label effect like this image

Thanks


Solution

  • Adapting the code from How do I apply a perspective transform to a UIView?, here is some sample Swift 4 code that applies perspective to a label giving a result similar to your image.

    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
    

    Run this in the playground and view pview.

    Play with the value being assigned to transform.m34 and the rotation angle in CATransform3DRotate. A negative rotation angle (as shown above) makes the left side smaller and the right side larger. A positive angle does the opposite.

    enter image description here