首页 > 解决方案 > UITextView 样式在我更改字距时重置

问题描述

当我更改字距时,样式会重置。例如,当我更改文本大小或颜色时,它被保存,但是当我更改字距时,样式 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)
    }

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

在此处输入图像描述

标签: swiftuitextviewkerningnsmutableparagraphstyle

解决方案


font属性仅更改该属性的字体text。TheattributedText是一个不同的属性,所以你也需要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,否则您必须使它们保持同步。


推荐阅读