NSAttributedString 中行尾的 NSKernAttributeName 空格

问题描述 投票:0回答:3

使用

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

结果如下所示:

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

ios swift nsattributedstring
3个回答
2
投票

这实际上是字距调整工作原理的定义;它调整字偶间距字符与下一个字符之间的间距。是否继续绘制下一个字符无关紧要。

标准属性

字距调整属性指示后续字符应从当前字符字体定义的默认偏移量移动多少;正紧排表示进一步移动,负紧排表示更靠近当前字符。

如果有帮助,请考虑在文字处理程序中执行此操作。如果启用了字距调整,并且您键入了一个字符,那么您希望插入点在哪里?预期的答案将是“从刚刚键入的字符偏移紧排量”,因为这就是紧排为 0 的默认情况下发生的情况,对吗?嗯,这正是这里发生的情况:如果您对字符串的最后一个字符进行紧排,则该字符串将包含最后一个紧排。

因此,这里正确的做法是将 dropLast() 逻辑包装在扩展中,然后就到此为止了。


0
投票

也许有帮助

textAttributes[.kern] = calculatedKern as AnyObject
            
let attributedString = NSMutableAttributedString(string: self.text, attributes: textAttributes)
// Remove kern attribute for last character
if self.text.count > 0 {
     let lastCharacterRange = NSRange(location: self.text.count - 1, length: 1)
     attributedString.removeAttribute(NSAttributedString.Key.kern, range: lastCharacterRange)
 }
            


self.attributedText = attributedString

-1
投票

创建扩展

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

已回答这里

© www.soinside.com 2019 - 2024. All rights reserved.