When using NSKernAttributeName it puts a space at the end of each line, is there any way to fix this? I can set the attribute is in the range:
NSRange(location: 0, length: self.text!.characters.count-1)
But I do not want to set this for each line.
This is the test code on the playground where I use
//: Playground - noun: a place where people can play import UIKit import XCPlayground var text = "Hello, playground\nhow are you?" let paragraphStyle = NSMutableParagraphStyle() paragraphStyle.paragraphSpacing = 50 paragraphStyle.alignment = NSTextAlignment.Left paragraphStyle.lineBreakMode = NSLineBreakMode.ByTruncatingTail let attributes = [ NSParagraphStyleAttributeName: paragraphStyle , NSKernAttributeName: 20 ] let attributedString = NSAttributedString(string: text, attributes: attributes) let label = UILabel() label.attributedText = attributedString label.numberOfLines = 0 label.textColor = UIColor.greenColor() label.backgroundColor = UIColor.orangeColor() label.sizeToFit() label.center = CGPoint(x: 500, y: 100) var text2 = "What up\nWhere are you?" let attributedString2 = NSAttributedString(string: text2, attributes: attributes) let label2 = UILabel() label2.attributedText = attributedString2 label2.numberOfLines = 0 label2.textColor = UIColor.greenColor() label2.backgroundColor = UIColor.orangeColor() label2.sizeToFit() label2.center = CGPoint(x: 500, y: 250) var text3 = "Hello" let attributedString3 = NSAttributedString(string: text3, attributes: attributes) let label3 = UILabel() label3.attributedText = attributedString3 label3.numberOfLines = 0 label3.textColor = UIColor.greenColor() label3.backgroundColor = UIColor.orangeColor() label3.sizeToFit() label3.center = CGPoint(x: 500, y: 400) let holderView = UIView(frame: CGRect(x: 0, y: 0, width: 1000, height: 500)) holderView.backgroundColor = UIColor.lightGrayColor() holderView.addSubview(label) holderView.addSubview(label2) holderView.addSubview(label3) XCPlaygroundPage.currentPage.liveView = holderView
The result is as follows:

You can see spaces at the end of each line.
source share