Not a complete answer, but GOTCHA and FIX.
GOTCHA: Using character spacing also adds a space to the end of the text. This error / error means that the text aligned in the center will not display correctly.
FIX: when creating a Range for attribute text, subtract 1 from text.count (ignoring the last character in the string for spaces.)
e.g. incorrect centering due to extra space:

fixed:

[Edit]
As for the note, if you use EdgeInsets to indent text, your UIButton subclass UIButton have to override intrinsicContentsSize:
override open var intrinsicContentSize: CGSize { let size = super.intrinsicContentSize let insets = self.titleEdgeInsets let width = size.width + insets.left + insets.right let height = size.height + insets.top + insets.bottom return CGSize(width: width, height: height) }
source share