In my app I want to change the line height, I am using this string extension :
extension String {
func addLineHeightWith(alignement: NSTextAlignment) -> N
The problem is with your use of NSRange(location: 0, length: self.count)
.
self.count
is the proper number of characters in the Swift String
. But the NSAttributedString
is based on NSString
and its use of UTF-16 encoded characters. You end up applying the style to only about half of the actual string. In fact, it splits one of the characters in half.
The easy fix is to get the length of the string as an NSString
.
Replace:
NSRange(location: 0, length: self.count)
with:
NSRange(location: 0, length: (self as NSString).length))