NSTextView 在字符上换行,而不是在单词上换行
我有一个 NSTextView 用于编辑不带空格但带标点符号的长字符串。我希望它能够在行尾的任何字符处换行,而不是尝试将其拆分为找到标点符号的单词,这会导致行不均匀。
我认为只需创建 NSATSTypesetter 的子类即可重新实现此方法,如下所示:
- (BOOL) shouldBreakLineByWordBeforeCharacterAtIndex:(NSUInteger)charIndex {
return YES;
}
这对文本视图的布局没有任何影响。每个布局上的每个换行符都会被调用一次,但仅限于无论如何都会发生换行符的地方。
I have an NSTextView for editing a long string without spaces, but with punctuation characters. I'd like it to wrap at whatever character falls at the end of the line instead of trying to split it into words where it finds punctuation, which results in uneven lines.
I thought it would be as easy as making a subclass of NSATSTypesetter
to reimplement this method like so:
- (BOOL) shouldBreakLineByWordBeforeCharacterAtIndex:(NSUInteger)charIndex {
return YES;
}
This is not having any effect on the layout of the text view. It is being called once for every line break on every layout, but only where the line break would have occurred anyway.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
NSTextView 中的换行设置实际上是其 textStorage 中文本的一个属性。
您需要获取该视图的 NSTextStorage (NSMutableAttributedString),然后将该字符串的样式设置为您所需的换行符设置。
请记住,存储中的文本不断变化,并且文本本身包含设置,因此您需要在每次更改时继续设置它。
下面将此方法调用为 [self setLineBreakMode:NSLineBreakByCharWrapping forView:txtView];每次文本存储更改时,例如:KVO观察txtView.string
The line breaking setting in a NSTextView is actually a attribute of the text in it's textStorage.
You need to get the NSTextStorage of that view which is a NSMutableAttributedString then set that string's style to your needed line break setting.
Keep in mind the text in the storage keeps changing and the text itself contains the setting so you need to keep setting it on each change.
Call this method below as [self setLineBreakMode:NSLineBreakByCharWrapping forView:txtView]; every time the text storage is changed, e.g.: KVO observe txtView.string