0

I am trying to change the character spacing of uitextview. However it changes whole appearance. Screenshots:

Before adding the code

Before adding the code

After adding the code

After adding the code

Code (as extension):

extension UITextView {

    func addCharacterSpacing(kernValue: Double = 1.15) {
        if let textValue = text, textValue.count > 0 {
            let attributedString = NSMutableAttributedString(string: textValue)
            attributedString.addAttribute(NSAttributedStringKey.kern, value: kernValue, range: NSRange(location: 0, length: attributedString.length - 1))
            attributedText = attributedString
        }
    }

}
slavoo
  • 5,798
  • 64
  • 37
  • 39
  • https://stackoverflow.com/questions/27535901/ios-8-change-character-spacing-on-uilabel-within-interface-builder – Rehan Meo Sep 03 '18 at 11:26

1 Answers1

0

If you are using NSMutableAttributedString, so you should customize it fully in the same way with paragraph style and fonts for it or any other settings.

From the screenshots I see a problem with paragraph style, setup it manually to the textView:

let paragraph = NSMutableParagraphStyle()
paragraph.alignment = .center

textView.attributedText = NSAttributedString(
     string: "string",
     attributes: [.paragraphStyle: paragraph])
regina_fallangi
  • 2,080
  • 2
  • 18
  • 38
biloshkurskyi.ss
  • 1,358
  • 3
  • 15
  • 34
  • Thank you but the font still is smaller than it should be. –  Sep 03 '18 at 11:28
  • @AnnoThra what about: `let myAttribute = [ NSAttributedStringKey.font: UIFont(name: "Chalkduster", size: 18.0)!]` and add it too the `attributes`. Please check example: https://stackoverflow.com/a/32269975/1278744 – biloshkurskyi.ss Sep 03 '18 at 11:35