I have a font which is super skinny. In photoshop, I have added stroke to the font which makes it look more appealing. When transferring it to iOS I couldn’t add a stroke unless I used NSAttributedString. By using the NSAttributedString I got one of my UILabels to look exactly how it looks in photoshop however the issue is that I will have hundreds of UILabels by the time my app is finished. Is there a way where I don’t have to manually connect each UILabel to its respective controller and set its attributedText one by one. Any suggestions would help.
3
Answers
Based on @AdamPro13 suggestion, you could create a protocol extension for your
NSAttributedString
:So, create
NSAttributedStringExtension.swift
with something like that:And you could make several different functions for different label types.
Just apply then on your label (rough code):
Note: the font and colour you could setup in your protocol extension function
If you’re using storyboard and want to use UILabels as normal, then you can try creating a UILabel subclass as shown below and using it in your storyboards as a custom class. This will effectively replace the text with the attributedText you want although you wont see it until runtime.
I was just searching and then I stumbled upon this question. I think you want a category for
UILabel
. I have one very helpful method from my old Cocoapod library called GPKit https://github.com/glennposadas/gpkit-ios that I made before. But this code below is updated and documented.You can use it in every label you have like so: