Custom Label Effects in Swift 5.1

How to make custom UILabel Effects in Swift? | Label Extension?

Anand Nimje
3 min readMar 18, 2018
Source -pexels.com

Creating the custom label effects in your application it’s not a big deal you can easily make it your own way here I am showing you a few examples of UILabel extension. It’s pretty easy to use to make any label custom without the use of any Custom class and within very less and reusable code.

Make Custom label for the OutLine effect

let strokeTextAttributes = [ 
NSAttributedString.Key.strokeColor : UIColor.red,
NSAttributedString.Key.foregroundColor : UIColor.white,
NSAttributedString.Key.strokeWidth : -4.0,
NSAttributedString.Key.font : UIFont.boldSystemFont(ofSize: 30)]
as [NSAttributedString.Key : Any]
//Making outline here
labelOutLine.attributedText = NSMutableAttributedString(string: “Your outline text”, attributes: strokeTextAttributes)
Label with Outline and foreground color effects

Make Custom label for UnderLine effect

let attributedString = NSMutableAttributedString(string: “Your UnderLine text”)
attributedString.addAttribute(NSAttributedString.Key.underlineStyle, value: NSUnderlineStyle.single.rawValue, range: NSRange(location: 0, length: attributedString.length))
//Underline effect here
labelUnderLine.attributedText = attributedString
Underline UILabel Effects

Extension for UILabel

If you use the above code for make underline and outline effects then need to use everywhere the same code then it doesn’t make any sense. So we can reuse this code by making simple extension function. It’s very easy to use every time to giving effects to any UILabel.

extension UILabel{

func makeOutLine(oulineColor: UIColor, foregroundColor: UIColor{
let strokeTextAttributes = [
NSAttributedString.Key.strokeColor : oulineColor,
NSAttributedString.Key.foregroundColor : foregroundColor,
NSAttributedString.Key.strokeWidth : -4.0,
NSAttributedString.Key.font : font ?? UIFont.systemFontSize
] as [NSAttributedString.Key : Any]
self.attributedText = NSMutableAttributedString(string: self.text ?? "", attributes: strokeTextAttributes)
}

func underline() {
if let textString = text {
let attributedString = NSMutableAttributedString(string: textString)
attributedString.addAttribute( NSAttributedString.Key.underlineStyle,
value: NSUnderlineStyle.single.rawValue,
range: NSRange(location: 0,
length: attributedString.length))
attributedText = attributedString
}
}
}

Check here how to use this extension -

The extension uses in the project

You can find full project code from here. Only you need to drag and drop ExUILabel.swift inside your project.

Conclusion

Extension is very useful in Swift programming we can make easily for any existing class, structure, enumeration, or protocol type.😊 🎉

Thanks for reading 🙌🏼

If you having any queries regarding this tutorial? | If you think you can do more simple way or little bit more extra things with this stuff please let me know questions, feedback or comments -on Twitter

--

--