Problems with NSTextField and attributed strings
Problems with NSTextField and attributed strings
- Subject: Problems with NSTextField and attributed strings
- From: Tim Cartwright <email@hidden>
- Date: Wed, 21 Jul 2004 13:16:32 -0500
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?
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?
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:
[style setDefaultTabInterval: -1.0]; // disable extra tabs?
Before manipulating the tab stops, and nothing changed.
-- Tim C.
_______________________________________________
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.