From 09c709b3ea788fb9bbdfbdbf07f984f63042647e Mon Sep 17 00:00:00 2001 From: Victor Barros Date: Wed, 27 Feb 2013 17:03:26 -0800 Subject: [PATCH] added possibility of change the background and text color --- EGOTextView/EGOTextView.h | 1 + EGOTextView/EGOTextView.m | 36 +++++++++++++++++++++++++++++------- 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/EGOTextView/EGOTextView.h b/EGOTextView/EGOTextView.h index 67974aa..df3e283 100644 --- a/EGOTextView/EGOTextView.h +++ b/EGOTextView/EGOTextView.h @@ -116,6 +116,7 @@ extern NSString * const EGOTextAttachmentPlaceholderString; @property(nonatomic,copy) NSAttributedString *attributedString; @property(nonatomic,copy) NSString *text; @property(nonatomic,retain) UIFont *font; // ignored when attributedString is not nil +@property(nonatomic,retain) UIColor *textColor; @property(nonatomic,getter=isEditable) BOOL editable; //default YES @property(nonatomic) NSRange selectedRange; @property(nonatomic) NSRange markedRange; diff --git a/EGOTextView/EGOTextView.m b/EGOTextView/EGOTextView.m index cb5b4ef..ec8f3cc 100644 --- a/EGOTextView/EGOTextView.m +++ b/EGOTextView/EGOTextView.m @@ -194,6 +194,7 @@ @implementation EGOTextView @synthesize attributedString=_attributedString; @synthesize text=_text; @synthesize font=_font; +@synthesize textColor=_textColor; @synthesize editable=_editable; @synthesize markedRange=_markedRange; @synthesize selectedRange=_selectedRange; @@ -268,6 +269,7 @@ - (void)dealloc { _textWindow=nil; [_font release], _font=nil; + [_textColor release], _textColor = nil; [_attributedString release], _attributedString=nil; [_caretView release], _caretView=nil; self.menuItemActions=nil; @@ -353,20 +355,40 @@ - (NSString *)text { return _attributedString.string; } +-(void)buildDefaultAttributes +{ + CTFontRef ctFont = CTFontCreateWithName((CFStringRef) self.font.fontName, self.font.pointSize, NULL); + NSDictionary *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:(id)ctFont, (NSString *)kCTFontAttributeName, (id)self.textColor.CGColor, kCTForegroundColorAttributeName, nil]; + self.defaultAttributes = dictionary; + [dictionary release]; + CFRelease(ctFont); + + [self textChanged]; +} + +-(void)setTextColor:(UIColor *)textColor +{ + UIColor *oldTextColor = _textColor; + _textColor = [textColor retain]; + [oldTextColor release]; + + [self buildDefaultAttributes]; +} + - (void)setFont:(UIFont *)font { UIFont *oldFont = _font; _font = [font retain]; [oldFont release]; - CTFontRef ctFont = CTFontCreateWithName((CFStringRef) self.font.fontName, self.font.pointSize, NULL); - NSDictionary *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:(id)ctFont, (NSString *)kCTFontAttributeName, (id)[UIColor blackColor].CGColor, kCTForegroundColorAttributeName, nil]; - self.defaultAttributes = dictionary; - [dictionary release]; - CFRelease(ctFont); - - [self textChanged]; + [self buildDefaultAttributes]; +} + +-(void)setBackgroundColor:(UIColor *)backgroundColor +{ + [super setBackgroundColor:backgroundColor]; + self.textInputView.backgroundColor = backgroundColor; } - (void)setText:(NSString *)text {