2
votes

I would like to draw my own TextField with 3 pictures.

  1. input_l.png - left side of the input
  2. input_m.png - middle part of the input - 1px wide - supposed to stretch or multiply to fill middle part
  3. input_r.png - right side

All images contain border, background, shadows, just everything i need to draw it only with those pictures.

But I cannot manage to draw it with subclass of NSTextField and these methods:

- (void)awakeFromNib
{
    [self setDrawsBackground:NO];
}

- (void)drawRect:(NSRect)rect
{
    NSDrawThreePartImage(rect, [NSImage imageNamed:@"input_l.png"], [NSImage imageNamed:@"input_m.png"], [NSImage imageNamed:@"input_r.png"], NO, NSCompositeSourceOut, 1.0, NO);

    [super drawRect:rect];
}

It draws black background with text/placeholder.

This is how it is supposed to look: http://d.pr/i/VLEN

If there are any other way, better, to draw this please tell me :) It just occured to me that I could use 1px wide background image without borders to draw this and in code make corners rounded and somehow draw even border. However I still would like to know how to do it the way I tried, for future use.

1
So I have tried to draw border with [NSBezierPath bezierPathWithRoundedRect:xRadius:yRadius] and it looks kinda ugly. Its like a little pixelated.Zdeněk Topič

1 Answers

1
votes

There can be many ways to do :

Subclass NSButton and if required also subclass NSButtonCell and override these methods

  1. -(void)drawRect:(NSRect)rect {

    }

  2. drawText:withFrame:inView:

  3. drawImage:withFrame:inView:.