Skip to content

Commit

Permalink
Merge pull request twitter-archive#28 from joshaber/label-style
Browse files Browse the repository at this point in the history
Label style
  • Loading branch information
Loren Brichter committed Aug 2, 2011
2 parents 62638a8 + 054ca33 commit 6c90f6d
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 4 deletions.
9 changes: 9 additions & 0 deletions lib/UIKit/TUILabel.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,20 @@
struct {
unsigned int selectable:1;
} _textLabelFlags;

NSString *_text;
TUIFont *_font;
TUIColor *_textColor;
TUITextAlignment _alignment;
}

@property(nonatomic,copy) NSString *text;
@property(nonatomic,retain) NSAttributedString *attributedString;

@property(nonatomic,getter=isSelectable) BOOL selectable;
@property(nonatomic, readonly) TUITextRenderer *renderer;
@property(nonatomic,retain) TUIFont *font;
@property(nonatomic,retain) TUIColor *textColor;
@property(nonatomic,assign) TUITextAlignment alignment;

@end
77 changes: 73 additions & 4 deletions lib/UIKit/TUILabel.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,17 @@
#import "TUINSView.h"
#import "TUIView+Private.h"

@interface TUILabel ()
- (void)_recreateAttributedString;
@end

@implementation TUILabel

@synthesize renderer;
@synthesize text=_text;
@synthesize font=_font;
@synthesize textColor=_textColor;
@synthesize alignment=_alignment;

- (id)initWithFrame:(CGRect)frame
{
Expand All @@ -35,6 +43,9 @@ - (id)initWithFrame:(CGRect)frame

- (void)dealloc
{
[_text release];
[_font release];
[_textColor release];
[renderer release];
[super dealloc];
}
Expand All @@ -61,20 +72,28 @@ - (void)copyText:(id)sender
}
- (void)drawRect:(CGRect)rect
{
if(renderer.attributedString == nil) {
[self _recreateAttributedString];
}

[super drawRect:rect]; // draw background
CGRect bounds = self.bounds;
renderer.frame = CGRectMake(0, 0, bounds.size.width, bounds.size.height);
[renderer draw];
}

- (NSAttributedString *)attributedString
- (void)_update
{
return renderer.attributedString;
[self setNeedsDisplay];
}

- (void)_update
- (NSAttributedString *)attributedString
{
[self setNeedsDisplay];
if(renderer.attributedString == nil) {
[self _recreateAttributedString];
}

return renderer.attributedString;
}

- (void)setAttributedString:(NSAttributedString *)a
Expand All @@ -83,6 +102,17 @@ - (void)setAttributedString:(NSAttributedString *)a
[self _update];
}

- (void)_recreateAttributedString
{
if(_text == nil) return;

TUIAttributedString *newAttributedString = [TUIAttributedString stringWithString:_text];
if(_font != nil) newAttributedString.font = _font;
if(_textColor != nil) newAttributedString.color = _textColor;
newAttributedString.alignment = _alignment;
self.attributedString = newAttributedString;
}

- (BOOL)isSelectable
{
return _textLabelFlags.selectable;
Expand All @@ -93,4 +123,43 @@ - (void)setSelectable:(BOOL)b
_textLabelFlags.selectable = b;
}

- (void)setText:(NSString *)text
{
if(text == _text) return;

[_text release];
_text = [text copy];

self.attributedString = nil;
}

- (void)setFont:(TUIFont *)font
{
if(font == _font) return;

[_font release];
_font = [font retain];

self.attributedString = nil;
}

- (void)setTextColor:(TUIColor *)textColor
{
if(textColor == _textColor) return;

[_textColor release];
_textColor = [textColor retain];

self.attributedString = nil;
}

- (void)setAlignment:(TUITextAlignment)alignment
{
if(alignment == _alignment) return;

_alignment = alignment;

self.attributedString = nil;
}

@end

0 comments on commit 6c90f6d

Please sign in to comment.