简体   繁体   English

如何设置NSTextView的默认书写方向?

[英]How to set the default writing direction for NSTextView?

I'm trying to set up a text view that a user can type Hebrew text into from right-to-left. 我试图建立一个文本视图,用户可以从右到左键入希伯来语文本。 Currently it defaults to a left-to-right text direction, which the user can manually change by right-clicking and selecting "Writing Direction > Right to Left", but what I need is for the text view to always default to this, without requiring the user to set it manually. 当前,它默认为从左到右的文本方向,用户可以通过右键单击并选择“书写方向>从右到左”来手动更改,但是我需要的是文本视图始终默认为该方向,而无需要求用户手动设置。

There's an option for setting this in Interface Builder, which is always ignored when I build and run my app. 在Interface Builder中有一个用于设置此选项的选项,当我构建和运行应用程序时,该选项总是被忽略。

I would be fine setting it from code, but I can't figure out how to use the method that looks like the closest thing to what I need: 我可以通过代码进行设置,但是我无法弄清楚如何使用看起来最接近我需要的方法:

hebrewTextView.setBaseWritingDirection(NSWritingDirection.RightToLeft, range: <#NSRange#>)

The range is stumping me. 范围让我很沮丧。 If it takes a range can this be the method I need? 如果需要一个范围,这可以是我需要的方法吗? Wouldn't the default behavior of a field be independent of any range of text? 字段的默认行为不会独立于任何文本范围吗?

Is there a way to set the default writing direction for a NSTextView? 有没有办法设置NSTextView的默认书写方向? Can this be done from the storyboard/interface builder, or from code? 可以从情节提要/界面构建器或代码中完成此操作吗? If setBaseWritingDirection is the method for this, what is the range value for and how would I set it for a field that is initially empty? 如果setBaseWritingDirection是用于此的方法,则其范围值是什么?如何为最初为空的字段设置范围值?

NSTextView inherits from NSText . NSTextView继承自NSText NSText has a baseWritingDirection property. NSText具有baseWritingDirection属性。 Try setting that: 尝试设置以下内容:

hebrewTextView.baseWritingDirection = NSWritingDirection.RightToLeft

It also inherits the action method makeBaseWritingDirectionRightToLeft() from NSResponder , which presumably what the contextual menu uses. 它还从NSResponder继承了操作方法makeBaseWritingDirectionRightToLeft() ,这大概是上下文菜单使用的方法。 So, you could call that. 所以,你可以打电话。

If neither of those works, you can set the text view's defaultParagraphStyle to one whose baseWritingDirection is right-to-left: 如果这些都baseWritingDirection则可以将文本视图的defaultParagraphStyle设置为baseWritingDirection为从右到左的baseWritingDirection

var style = hebrewTextView.defaultParagraphStyle.mutableCopy() as! NSMutableParagraphStyle
style.baseWritingDirection = NSWritingDirection.RightToLeft
hebrewTextView.defaultParagraphStyle = style

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM