Re: Problems with NSTextField and attributed strings
Re: Problems with NSTextField and attributed strings
- Subject: Re: Problems with NSTextField and attributed strings
- From: Glenn Andreas <email@hidden>
- Date: Wed, 21 Jul 2004 13:50:12 -0500
At 1:16 PM -0500 7/21/04, Tim Cartwright wrote:
I'm trying to use a NSTextField to display a multi-line, multi-font,
multi-language readout of some information. I set the value of
field to be a complex attributed string, which includes font
settings and paragraph styles. Mostly it works, but I'm seeing a
few anomalies.
First, I cannot seem to get the value of NSMutableParagraphStyle's
setParagraphSpacingBefore to affect the display. Here's the gist of
my code:
NSMutableAttributedString * fieldValue = [[NSMutableAttributedString
alloc] initWithString: @""];
NSMutableParagraphStyle * style = [[NSMutableParagraphStyle alloc] init];
[style setParagraphSpacingBefore: 20.0]; // among other settings
NSMutableAttributedString * line = [[NSMutableAttributedString
alloc] initWithString: @"A line of text\n"];
[line addAttribute: NSParagraphStyleAttributeName value: style
range: NSMakeRange(0, [line length])];
[fieldValue appendAttributedString: line]; // among others
[myTextField setAttributedStringValue: fieldValue];
Other attributes embedded in the text field value are displaying,
but no matter what I set for the paragraph spacing before, I don't
see a change in line spacing. Does "\n" not separate paragraphs?
What am I doing wrong?
Not sure on this, but I believe that "space before" isn't used for
the for the first paragraph (since it's at the "top", just like
"space after" on the last paragraph is ignored because there's
nothing after it).
OK, second: I originally had the NSTextField be selectable but not
editable, allowing users to copy the static text out and paste it
into other apps. It seemed the friendly thing to do. However, as
soon as I click in the NSTextField, it appears to lose all of its
attributes. That is, it seems to revert to a single font and all of
the default paragraph style attributes. Is there a way to allow
selecting and copying without having the attributes disappear?
I've never seen that.
And finally, third: I discovered that doing the following:
NSMutableParagraphStyle * style = [[NSMutableParagraphStyle alloc] init];
appears to initialize the style as though I had used
[NSParagraphStyle defaultParagraphStyle] -- in particular, it was
loaded with tab stops set every 28.0 points apart. So, to set my
own specific tab stops, I go through this chunk of code:
NSLog(@"Removing %d tab stops", [[style tabStops] count]);
for (i = 0; i < [[style tabStops] count]; ++i)
{
[style removeTabStop: [[style tabStops] objectAtIndex: i]];
}
NSTextTab * numberTab = [[NSTextTab alloc] initWithType:
NSRightTabStopType location: 30.0];
[style addTabStop: numberTab];
[numberTab release];
NSTextTab * textTab = [[NSTextTab alloc] initWithType:
NSLeftTabStopType location: 34.0];
[style addTabStop: textTab];
[textTab release];
The log statement confirms the intent to remove 12 tab stops.
However, debugging output immediately following this code shows that
my two tab stops are followed by 6 other tab stops, starting at 56.0
points and every 56.0 points thereafter. Why? I even added this
line of code:
Try:
for (i = 0; i < [[style tabStops] count]; ++i)
{
[style removeTabStop: [[style tabStops] objectAtIndex: 0]];
}
since your code otherwise only removes the first half the tabStops
(since it'll stop when i = count, which gets decremented each time
through your loop).
--
Glenn Andreas email@hidden
mondo blobbo, Cythera, Theldrow, oh my!
Mad, Bad, and Dangerous to Know
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.