NSAttributedString中行尾的NSKernAttributeName空格

时间:2016-06-02 14:27:26

标签: ios swift nsattributedstring

当使用NSKernAttributeName时,它会在每行末尾添加一个空格,有什么办法可以解决这个问题吗?我可以将属性设置为:

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

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

这是我正在使用的游乐场的测试代码

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

结果如下:

text with kerning at the end of a line

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

2 个答案:

答案 0 :(得分:1)

这实际上是字距调整的工作原理;它调整kerned字符和下一个字符所在位置之间的空间。是否要绘制下一个角色是无关紧要的。

Standard Attributes

  

kerning属性指示后续字符应从当前字符的字体定义的默认偏移量移位多少;正kern表示偏移越远,负kern表示更接近当前字符的偏移。

如果有帮助,请考虑在文字处理器中执行此操作。如果打开了字距,并且您键入了一个字符,那么您期望插入点在哪里?预期的答案将是“从刚刚输入的字符中克服kern的数量”,因为在默认情况下kern为0时会发生什么,对吗?好吧,这正是这里发生的事情:如果你克服了字符串的最后一个字符,那么字符串就包含了最后一个字符。

所以在这里做的正确的事情就是将你的dropLast()逻辑包装在一个扩展名中并将其称为一天。

答案 1 :(得分:0)

创建扩展程序

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