TwoTerm/Views/TextLabel.m
Kelvin Sherlock 2bff238150 forgot dealloc method.
git-svn-id: svn://qnap.local/TwoTerm/trunk@3137 5590a31f-7b70-45f8-8c82-aa3a8e5f4507
2016-09-17 17:36:38 +00:00

96 lines
1.7 KiB
Objective-C

//
// TextLabel.m
// 2Term
//
// Created by Kelvin Sherlock on 7/7/2016.
//
//
#import "TextLabel.h"
#import "CharacterGenerator.h"
@implementation TextLabel
@synthesize text = _text;
@synthesize color = _color;
-(void) setText:(NSString *)text {
if (_text == text) return;
[_text release];
_text = [text retain];
[self setNeedsDisplay: YES];
}
-(void) setColor:(NSColor *)color {
if (_color == color) return;
[_color release];
_color = [color retain];
[self setNeedsDisplay: YES];
}
/*
-(BOOL)isFlipped {
return YES;
}
*/
-(BOOL)isOpaque {
return NO;
}
-(void)dealloc {
[_text release];
[_color release];
[super dealloc];
}
- (void)drawRect:(NSRect)dirtyRect {
[super drawRect:dirtyRect];
[[NSColor clearColor] setFill];
NSRectFill(dirtyRect);
NSUInteger length = [_text length];
if (!length) return;
if (!_color) return;
CharacterGenerator *gen = [CharacterGenerator generator];
NSSize sz = [gen characterSize];
NSRect frame = [self frame];
CGFloat width = sz.width * length;
NSPoint point = NSZeroPoint;
if (width < NSWidth(frame)) {
point.x = (NSWidth(frame) - width) * 0.5;
}
point.x = floor(point.x);
[_color setFill];
for (unsigned i = 0; i < length; ++i) {
unichar c = [_text characterAtIndex: i];
[gen drawCharacter: c atPoint: point];
point.x += sz.width;
if (point.x > NSWidth(frame)) break;
}
}
-(void)awakeFromNib {
[super awakeFromNib];
//[self setWantsLayer: YES];
if (!_text) _text = [@"Testing!" retain];
if (!_color) _color = [[NSColor greenColor] retain];
}
@end