插入点异常在NSTextView中的位置

时间:2019-07-27 06:46:47

标签: swift cocoa nstextview

enter image description here

此插入点确实反复无常。我不知道他什么时候来。 这似乎是一个偶然的问题,但它几乎每一次都经常发生。

我没有做很多工作。这可能是我的所有代码。

我想这可能与allowNonContiguousLayout = true有关,因为如果为假,似乎不会发生。

谢谢

class textView: NSTextView {
    override func awakeFromNib() {
        super.awakeFromNib();

        let paragraphStyle = NSMutableParagraphStyle();
        paragraphStyle.firstLineHeadIndent = 0;
        paragraphStyle.headIndent = 0;
        paragraphStyle.tailIndent = 0;
        paragraphStyle.lineSpacing = 0;
        paragraphStyle.lineHeightMultiple = 1.3;
        paragraphStyle.paragraphSpacing = 0;
        paragraphStyle.allowsDefaultTighteningForTruncation = true;
        self.defaultParagraphStyle = paragraphStyle;


        let layoutManager = NSLayoutManager();
        layoutManager.allowsNonContiguousLayout = true;
        self.textContainer?.replaceLayoutManager(layoutManager);

        let centent = NSMutableAttributedString(string: "\n\n\n\n\n1111j1呵11111\n1111s's'dsskj呵,112a11k11111abcd我试试e12111dddsss3", attributes: [.font: self.defaultTextFont]);

        self.textStorage?.replaceCharacters(in: NSRange(location: 0, length: 0), with: centent);
        self.textStorage?.addAttributes([NSAttributedString.Key.paragraphStyle : self.defaultParagraphStyle!], range: NSRange(location: 0, length: self.textStorage!.length))
        self.textStorage?.setAttributes([NSAttributedString.Key.font : NSFont.systemFont(ofSize: 38)], range: .init(location: 31, length: 1));
        self.textStorage?.setAttributes([NSAttributedString.Key.font : NSFont.systemFont(ofSize: 21)], range: .init(location: 45, length: 1));
        self.textStorage?.setAttributes([NSAttributedString.Key.font : NSFont.systemFont(ofSize: 32)], range: .init(location: 49, length: 1));
    }
}

0 个答案:

没有答案