NSKernAttributeName space 在 NSAttributedString 行尾

NSKernAttributeName space at end of line in an NSAttributedString

当使用 NSKernAttributeName 时,它会在每行的末尾放置一个 space,有什么办法可以解决这个问题吗?我可以将属性设置在以下范围内:

NSRange(location: 0, length: self.text!.characters.count-1)

但我不想为每一行都设置这个。

这是我正在使用的 playground 中的测试代码

//: 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's 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

结果如下:

您可以在每行的末尾看到 space。

这实际上是字距调整工作原理的定义;它调整紧缩字符和下一个字符所在位置之间的 space。是否继续绘制下一个字符是无关紧要的。

Standard Attributes

The kerning attribute indicates how much the following character should be shifted from its default offset as defined by the current character’s font; a positive kern indicates a shift farther along and a negative kern indicates a shift closer to the current character.

如果有帮助,请考虑在文字处理器中执行此操作。如果打开了字距调整,并且您键入了一个字符,那么您希望插入点在哪里?预期的答案是 "offset from the just typed character by the amount of kern",因为这是默认情况下 kern 为 0 的情况,对吗?好吧,这正是这里发生的事情:如果您对字符串的最后一个字符进行紧排,那么该字符串将包括最后一个紧排。

所以这里正确的做法是将您的 dropLast() 逻辑包装在一个扩展中,然后收工。

创建扩展程序

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
        }
    }
}

已回答here