将NSTextView设置为以编程方式还是通过xcode通过ch

将NSTextView设置为以编程方式还是通过xcode通过ch

本文介绍了将NSTextView设置为以编程方式还是通过xcode通过char包装?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在线上有一些解决方案,但是它们要么已经使用了10年,不再起作用,要么是用目标C编写的(而且已经很过时了).一种解决方案涉及AttributedText,并且每次内容更新时都要更改换行样式,对我而言,每隔几分钟就会有数百次呼叫.当然,这不是最好的方法.我必须在这里丢失一些东西,必须有一种方法可以将NSTextView设置为永久由char换行.

There are a couple of solutions online but they are either 10 years old and no longer work or are written in objective C (And also very outdated). One solution involved AttributedText and changing the line break style every single time the content is updated, which for me would be hundreds of calls every few minutes. Surely this can't be the best way. I must be missing something here, there must be a way to set an NSTextView to wrap by char permanently.

我已经尝试过了,但是它不起作用,并且继续按单词换行:

I've tried this but it doesn't work and continues to wrap by word:

consoleTextView.textContainer?.lineBreakMode = NSLineBreakMode.byCharWrapping

我也尝试过在xcode中使用属性检查器.我终于找到了它的选项,但是单击它没有任何作用,它只是在换行"字段中继续说空选择":

I've also tried using the attributes inspector in xcode. I finally found the option for it but clicking it does nothing, it just continues to say 'empty selection' in the 'line breaking' field:

推荐答案

您应该在段落样式上设置换行模式.这是使其可行的最小示例:

You are supposed to set the line break modes on the paragraph styles. This is the smallest possible example to make it work:

let textView = NSTextView(frame: CGRect(x: 0, y: 0, width: 100, height: 200))

let paragraphStyle = NSMutableParagraphStyle.default.mutableCopy() as! NSMutableParagraphStyle
paragraphStyle.lineBreakMode = .byCharWrapping

textView.defaultParagraphStyle = paragraphStyle
textView.string = "12345 12345 12345"

view.addSubview(textView)

这篇关于将NSTextView设置为以编程方式还是通过xcode通过char包装?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 09:51