The NSKernAttributeName space at the end of the line in NSAttributedString

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:

kerning text at the end of a line

You can see spaces at the end of each line.

+6
source share
2 answers

This is actually a definition of how kerning works; it adjusts the space between the nuclear symbol and the location of the next symbol. Whether the next character is drawn or not does not matter.

Standard Attributes

The kerning attribute indicates how far the next character should be offset from its default offset, as determined by the current character font; a positive core indicates a shift further, and a negative core indicates a shift closer to the current character.

If this helps, think about it in a word processor. If kerning is turned on and you enter a character, where would you expect the entry point to be then? The expected answer will be "offset from the character just entered by the number of cores", as is the case with the default kernel of 0, right? So what exactly happens here: if you loop the last character of a string, the string therefore includes the last core.

So, the right thing to do is to wrap the dropLast () logic in the extension and call it day.

0
source

Create extension

 import UIKit extension UILabel { @IBInspectable var kerning: Float { get { var range = NSMakeRange(0, (text ?? "").characters.count) guard let kern = attributedText?.attribute(NSKernAttributeName, atIndex: 0, effectiveRange: &range), value = kern as? NSNumber else { return 0 } return value.floatValue } set { var attText:NSMutableAttributedString? if let attributedText = attributedText { attText = NSMutableAttributedString(attributedString: attributedText) } else if let text = text { attText = NSMutableAttributedString(string: text) } else { attText = NSMutableAttributedString(string: "") } let range = NSMakeRange(0, attText!.length) attText!.addAttribute(NSKernAttributeName, value: NSNumber(float: newValue), range: range) self.attributedText = attText } } } 

This was answered here.

0
source

All Articles