Setting alignment of NSTextView programmatically
-
Hi all,
I can't seem to grok exactly the right way to align a plain text
NSTextView (left, center or right). I modified the ButtonMadness
sample project to experiment by replacing the code that creates a code-
based segment control with a code-based text view like this that
should be right-aligned:
buttonFrame = [placeHolder5 frame];
codeBasedTextView = [[NSTextView alloc] initWithFrame:buttonFrame];
[codeBasedTextView setEditable:NO];
[codeBasedTextView setSelectable:YES];
[codeBasedTextView setDrawsBackground:NO];
[codeBasedTextView setRichText:NO];
[codeBasedTextView setImportsGraphics:NO];
[codeBasedTextView alignRight:nil];
[codeBasedTextView setString:@"a string"];
[segmentBox addSubview:codeBasedTextView];
[placeHolder5 removeFromSuperview]; // we are done with the place
holder, remove it from the window
but the string still shows up left aligned rather than right aligned.
I'm obviously not doing something right, but I'm a little new to all
the Cocoa APIs. Any help would be appreciated. My goal would be that
if the NSTextView auto-resizes and becomes wider, the text would
automatically follow the right edge.
Documentation for alignRight does say:
"This action method applies right alignment to selected paragraphs (or
all text if the receiver is a plain text object)."
and, as nearly as I can tell, the text view is set up to be plain
text, but maybe I don't understand that part either.
Thanks,
Ken
--
Ken Worley
Software Engineer, Tiberius, Inc. -
On Feb 13, 2008, at 2:18 PM, Ken Worley wrote:> [codeBasedTextView alignRight:nil];
> [codeBasedTextView setString:@"a string"];
What happens if you set the string before calling alignRight:? I
believe there may need to be some text for the method to set the
alignment of.
Douglas Davidson -
On Feb 13, 2008, at 3:26 PM, Douglas Davidson wrote:>
> On Feb 13, 2008, at 2:18 PM, Ken Worley wrote:
>
>> [codeBasedTextView alignRight:nil];
>> [codeBasedTextView setString:@"a string"];
>
> What happens if you set the string before calling alignRight:? I
> believe there may need to be some text for the method to set the
> alignment of.
Unfortunately, same thing - still left-aligned.
Ken
--
Ken Worley
Software Engineer, Tiberius, Inc. -
On 13 Feb '08, at 2:33 PM, Ken Worley wrote:> Unfortunately, same thing - still left-aligned.
Might be because -alignRight: operates on the selection, and there
isn't any selected text.
It might be better to go down to the NSAttributedString layer â get
the view's textStorage, and set the NSParagraphStyleAttributeName
attribute of the entire text to an NSParagraphStyle object with right
alignment.
âJens -
On Feb 13, 2008, at 3:18 PM, Ken Worley wrote:> Hi all,
>
> I can't seem to grok exactly the right way to align a plain text
> NSTextView (left, center or right). I modified the ButtonMadness
> sample project to experiment by replacing the code that creates a
> code-based segment control with a code-based text view like this
> that should be right-aligned:
>
> buttonFrame = [placeHolder5 frame];
>
> codeBasedTextView = [[NSTextView alloc] initWithFrame:buttonFrame];
> [codeBasedTextView setEditable:NO];
> [codeBasedTextView setSelectable:YES];
> [codeBasedTextView setDrawsBackground:NO];
> [codeBasedTextView setRichText:NO];
> [codeBasedTextView setImportsGraphics:NO];
> [codeBasedTextView alignRight:nil];
> [codeBasedTextView setString:@"a string"];
> [segmentBox addSubview:codeBasedTextView];
> [placeHolder5 removeFromSuperview]; // we are done with the place
> holder, remove it from the window
>
> but the string still shows up left aligned rather than right
> aligned. I'm obviously not doing something right, but I'm a little
> new to all the Cocoa APIs. Any help would be appreciated. My goal
> would be that if the NSTextView auto-resizes and becomes wider, the
> text would automatically follow the right edge.
>
> Documentation for alignRight does say:
>
> "This action method applies right alignment to selected paragraphs
> (or all text if the receiver is a plain text object)."
>
> and, as nearly as I can tell, the text view is set up to be plain
> text, but maybe I don't understand that part either.
Here's the solution I've got for now that works in the sample project:
Take out the alignRight message in the above and replace it with this:
NSParagraphStyle* tStyle = [NSParagraphStyle defaultParagraphStyle];
NSMutableParagraphStyle* tMutStyle = [tStyle mutableCopy];
[tMutStyle setAlignment:NSRightTextAlignment];
[codeBasedTextView setDefaultParagraphStyle:tMutStyle];
Oddly enough, it does not work if I start with [codeBasedTextView
defaultParagraphStyle]. Also, I still can't understand why simply
calling alignRight doesn't work since it's supposed to apply to all
text if the receiver is plain text...
Ken
--
Ken Worley
Software Engineer, Tiberius, Inc. -
On Feb 15, 2008, at 12:35 PM, Ken Worley wrote:> Here's the solution I've got for now that works in the sample project:
>
> Take out the alignRight message in the above and replace it with this:
>
> NSParagraphStyle* tStyle = [NSParagraphStyle defaultParagraphStyle];
> NSMutableParagraphStyle* tMutStyle = [tStyle mutableCopy];
> [tMutStyle setAlignment:NSRightTextAlignment];
> [codeBasedTextView setDefaultParagraphStyle:tMutStyle];
>
> Oddly enough, it does not work if I start with [codeBasedTextView
> defaultParagraphStyle]. Also, I still can't understand why simply
> calling alignRight doesn't work since it's supposed to apply to all
> text if the receiver is plain text...
Here is what is going on in this case:
The text view is set to be non-editable. The method -alignRight: is
the UI action for right-aligning text; since user actions do not
affect non-editable text, this method does not affect non-editable
text views. If you want to right-align programmatically, you can use
setAlignment:NSRightTextAlignment instead, or modify the text storage
directly.
Douglas Davidson -
On Feb 18, 2008, at 4:14 PM, Douglas Davidson wrote:>
> On Feb 15, 2008, at 12:35 PM, Ken Worley wrote:
>
>> Here's the solution I've got for now that works in the sample
>> project:
>>
>> Take out the alignRight message in the above and replace it with
>> this:
>>
>> NSParagraphStyle* tStyle = [NSParagraphStyle defaultParagraphStyle];
>> NSMutableParagraphStyle* tMutStyle = [tStyle mutableCopy];
>> [tMutStyle setAlignment:NSRightTextAlignment];
>> [codeBasedTextView setDefaultParagraphStyle:tMutStyle];
>>
>> Oddly enough, it does not work if I start with [codeBasedTextView
>> defaultParagraphStyle]. Also, I still can't understand why simply
>> calling alignRight doesn't work since it's supposed to apply to all
>> text if the receiver is plain text...
>
> Here is what is going on in this case:
>
> The text view is set to be non-editable. The method -alignRight: is
> the UI action for right-aligning text; since user actions do not
> affect non-editable text, this method does not affect non-editable
> text views. If you want to right-align programmatically, you can
> use setAlignment:NSRightTextAlignment instead, or modify the text
> storage directly.
>
> Douglas Davidson
>
Thanks very much for the help. The difference between those two
methods was lost on me before.
Ken
--
Ken Worley
Software Engineer, Tiberius, Inc.


