Skip navigation.
 
mlRe: Changing the line height of an NSTextField?
FROM : Nicholas J Humfrey
DATE : Wed Mar 26 23:10:09 2008

Ah, interesting, thanks for this tip. As a note for anyone else who 
wants to try and do this:

In the end I subclased the NSTextField class and overrid the drawRect 
function. I then split the string up into its lines and drew them 
myself using drawRect. This is some of the first Cocoa code I have 
written, so please don't hurt me if what I am doing is crazy!


-(void)drawRect:(NSRect)r
{
   NSArray *lines = [[self stringValue] 
componentsSeparatedByString:@"\n"];
   NSDictionary* attributes = [self textAttributes];
   int n;
   
   // Use a black pen to draw the text
   [[NSColor blackColor] set];

   // Draw the text to the view, line by line
   for (n=0; n<[lines count]; n++) {
       NSString *line = [lines objectAtIndex:n];
       NSPoint point;
       point.x = 0;
       point.y = (n*10);
       
       // Should the text be centered?
       if ([self alignment] == NSCenterTextAlignment) {
           NSSize size = [line sizeWithAttributes:attributes];
           point.x += (r.size.width - size.width)/2;
       }
       
       // Draw it!
       [line drawAtPoint: point withAttributes: attributes];
   }
}


On 26 Mar 2008, at 16:58, John Stiles wrote:
> I've found that adjusting the font matrix to include a negative Y 
> transformation has the effect of decreasing the line height. This 
> seemed like a bug to me when I discovered it, since I just wanted 
> the text to be translated upwards in its cell, not shrink the line 
> height. I wasn't sure if it was a bug or desired functionality. If 
> this is the correct behavior, it's one way to do it.
>
>
> Nicholas J Humfrey wrote:

>>
>> Hello Cocoa Developers,
>>
>> I am trying to squeeze an extra line of text into a multiline 
>> NSTextField, but I can't work out how to adjust the line height? 
>> What is the easiest way of adjusting this?
>>
>>
>> The purpose of this is that I'm actually using several NSTextFields 
>> on an NSView to lay text out for printing on to a sheet of labels, 
>> which has been working out very nicely up until this point! Perhaps 
>> there is a better way of doing this?
>>
>>
>> Thanks,
>>
>> nick.
>>

Related mailsAuthorDate
mlChanging the line height of an NSTextField? Nicholas J Humfrey Mar 26, 11:41
mlRe: Changing the line height of an NSTextField? John Stiles Mar 26, 17:58
mlRe: Changing the line height of an NSTextField? Nicholas J Humfrey Mar 26, 23:10
mlRe: Changing the line height of an NSTextField? Douglas Davidson Mar 26, 23:17
mlRe: Changing the line height of an NSTextField? Nicholas J Humfrey Mar 26, 23:48