Re: Hanging Indentation in NSTextField
Re: Hanging Indentation in NSTextField
- Subject: Re: Hanging Indentation in NSTextField
- From: "Greg Wiseman" <email@hidden>
- Date: Sun, 2 Apr 2006 12:49:13 +0200
On 3/30/06, Douglas Davidson <email@hidden> wrote:
>
>
> On Mar 30, 2006, at 8:05 AM, Ryan Stevens wrote:
>
> I would use NSAttributedStrings instead. Make a NSMutableParagraphStyle with
> 5 or 10 for its headIndent and reuse/apply that to each NSAttributedString
> before giving them to your text fields...
>
> NSParagraphStyle, -headIndent ...
> Returns the distance in points from the leading margin of a text container
> to the beginning of lines other than the first.
>
> Sounds like it'll do exactly what you want. :D
...
> Typically a textfield would contain only one paragraph of text, and if
> that's the case then modifying the paragraph style should produce the effect
> you are looking for. It's not impossible for a text field to contain more
> than one paragraph--for example, opt-return can be used to insert a \n in
> one--but it's not common. Depending on the contents of your text fields,
> the paragraph style solution could work for you.
Thank you Ryan and Douglas, the NSParagraphStyle solution should suit
my needs just fine, I don't expect my users to be inserting newlines.
However, I'm having a few issues trying to get this working.
First of all, the paragraph style only seems to be applied if I set my
textfields to be uneditable (setEditable:NO), which is problematic. Am
I missing something? I don't care if the style isn't applied during
actual editing (and I could use my previous NSTextContainer subclass
to do this anyway, I suppose) but when the fields are just being
displayed, I don't understand why they aren't using the style properly
unless they're not editable. (Simply changing the flag from NO to YES
and not changing any other code makes my textfields no longer indent
the lines).
Secondly, the algorithm I was using to calculate the number of lines
that are in the text view no longer works with this solution. The way
I'm doing this might possibly be causing the first problem I
mentioned, but I'm not sure.
The way I've been calculating lines is as follows: I have a category
for NSTextView that is based very closely on the one in the TipWrapper
example (http://developer.apple.com/samplecode/TipWrapper/listing7.html),
with only the first method (numberOfLines).
To properly lay out my NSTextFields, I need to know how many lines
each of them has. To do this, I use the following class method in a
utility class:
+ (unsigned int)numberOfLinesForTextField:(NSTextField *)field;
{
unsigned numberOfLines;
NSResponder *oldFirstResponder = [[field window] firstResponder];
[[field window] makeFirstResponder:field];
numberOfLines = [(NSTextView *)[[field window] fieldEditor:YES
forObject:field] numberOfLines];
[[field window] makeFirstResponder:oldFirstResponder];
return numberOfLines;
}
and loop through each of my (programatically created) textfields,
passing them to the method.
It's pretty ugly and a little slow but I couldn't manage to get
correct line counts by simply setting the fields to be the delegates
of the field editor; making each field the first responder was the
only way I could find that worked properly. Unfortunately, it now
always reports that the textfields only have one line. Do I need to
de-apply and then reapply my paragraphstyle? If so, it seems to me
that the correct number of lines wouldn't be counted (because the
indentation wouldn't be taken into account).
> Greg, for a professed newcomer you seem to have quite a grasp of the text system.
Thank you for the kind words, but as this post perhaps made clear I am
in fact quite lost :)
I suppose I should also mention that I'm passing my NSTextFields the
NSAttributedStrings in two places: first, when they are initially
created (i.e. a new 'widget' is selected from a control) and also in
controlTextDidChange (though I'm thinking I should maybe move it into
controlTextDidEndEditing, as it doesn't seem quite right to be
constantly reapplying the paragraph style on every keystroke. The fact
that a range is passed in to the string had me a little confused).
Thanks once again,
-Greg
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden