当我更改字距调整时,UITextView 样式被重置

UITextView style is reset when I change kerning

当我改变字距时样式被重置。例如,当我改变文本大小或颜色时,它被保存,但是当我改变字距时,样式 UITextView 被重置


@IBAction func sizeTextEdit(_ sender: Any) {

        self.textOne?.font = UIFont.systemFont(ofSize: CGFloat(sizeText.value * 1))
    }

@IBAction func kernTextEdit(_ sender: Any) {

    let textString = textOne.text
    let attrs: [NSAttributedString.Key : Any] = [.kern: kernText.value]
        textOne?.attributedText = NSAttributedString(string: textString!, attributes: attrs)
    }

正如你在第一个截图中看到的,我增加了字体,然后我增加了字母之间的距离,字体大小被重置

font属性只改变text属性的字体。 attributedText 是一个不同的 属性,因此您也需要为其定义 font

let attrs: [NSAttributedString.Key : Any] = [.kern: kernText.value,
                                             .font: UIFont.systemFont(ofSize: CGFloat(sizeText.value * 1))]
textOne?.attributedText = NSAttributedString(string: textString!, attributes: attrs)

附带说明一下,如果你想做这样的事情,最好坚持使用一个 属性,在本例中为 attributedText,否则你必须使它们保持同步。