Skip navigation.
 
mlNSAttributedString in NSTextFieldCell (NSTableView)
FROM : Andrew Cooper
DATE : Sat Jun 24 10:08:27 2006

I'm having trouble adjusting the baseline offset of a string in an 
NSTextFieldCell. I'm designing a UI to be driven with a touchscreen 
so I need something bigger than normal and so I'm wanting the font 
and cells in my table to be larger than usual. Because of the larger 
cell height and wanting to keep white space around the text i've 
sized the cell larger than the text and it is vertically aligned to 
the top edge. I'd like to center this and I am having trouble doing so.

I've found that this has been discussed before on this list, but the 
suggested solution of using NSAttributedStrings with the 
NSBaselineOffsetAttributeName attribute set to a negative value does 
not work for me. I can set just about any other parameter font, 
color, underline, strikethrough, obliqueness, etc and they all work, 
but anything involving the baseline like 
NSBaselineOffsetAttributeName or NSSuperscriptAttributeName seems to 
do nothing at all.

I am using a custom formatter connected to the NSTextFieldCell 
prototype of my table's column, shown below.
The results of this are shown in http://hkcreations.org/
attributedStringResult.png
I've made the cells very tall to illustrate that the baseline is not 
being affected.
I've read that I can also subclass the cell to implement my own 
centering, but i'd like to use this solution if possible because it's 
much cleaner.

Thanks to anyone who has suggestions for me.

Andrew Cooper


@implementation AMCenterFormatter
- (id) init {
   self = [super init];
   if (self != nil) {
       NSNumber *blOffset = [NSNumber numberWithFloat:-15.0];
       NSFont *font = [NSFont systemFontOfSize:20];
       attributes = [[NSDictionary alloc] initWithObjectsAndKeys:
           font,NSFontAttributeName,
           blOffset,NSBaselineOffsetAttributeName,
           nil];
   }
   return self;
}

- (NSAttributedString *)attributedStringForObjectValue:(id)anObject
                                                                withDefaultAttributes:(NSDictionary *)attr
{
   NSString *str = [self stringForObjectValue:anObject];
   return [[[NSAttributedString alloc] initWithString:str 
attributes:attributes] autorelease];
}

- (BOOL)getObjectValue:(id *)anObject
                        forString:(NSString *)string
           errorDescription:(NSString **)error
{
   if (anObject)
       *anObject = [[string copy] autorelease];
   return YES;
}

- (NSString *)stringForObjectValue:(id)anObject
{
   return [anObject description];
}
@end

Related mailsAuthorDate
No related mails found.