Vinodha Sundaramoorthy Vinodha Sundaramoorthy - 4 months ago 83
iOS Question

Extra line shown in UILabel Swift-3

I am setting uilabel layer cornerRadius and sometimes the label has extra line in some corners / edges of the label , is there a way to avoid this ?

Please find below the screen shot .

enter image description here

code :

//Message view
let messageLabel = UILabel()
messageLabel.backgroundColor = #someColor
messageLabel.textColor = #someColor
messageLabel.font = UIFont.systemFont(ofSize: 14)
messageLabel.lineBreakMode = NSLineBreakMode.byWordWrapping
messageLabel.attributedText = attributedString
messageLabel.numberOfLines = 0
messageLabel.textAlignment = .center
messageLabel.shadowColor = UIColor.clear
messageLabel.layer.cornerRadius = 3.0

let maskPath = UIBezierPath(roundedRect: messageLabel.bounds,
byRoundingCorners: [.topLeft, .topRight, .bottomRight],
cornerRadii: CGSize(width: 18.0, height: 0.0))
let maskLayer = CAShapeLayer()
maskLayer.path = maskPath.cgPath
messageLabel.layer.mask = maskLayer


These kind of issues may happen if frame has fractional x/y/width/height values.

If this is your case, try to round all values with one of these functions: round(), ceil() or floor()