How to set the default writing direction for NSTextView?

985 Views Asked by At

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.

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? 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?

1

There are 1 best solutions below

1
On BEST ANSWER

NSTextView inherits from NSText. NSText has a baseWritingDirection property. Try setting that:

hebrewTextView.baseWritingDirection = NSWritingDirection.RightToLeft

It also inherits the action method makeBaseWritingDirectionRightToLeft() from NSResponder, which presumably what the contextual menu uses. 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:

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