Permalink
Browse files

add placeholder in mutextview

  • Loading branch information...
1 parent a89ec43 commit da905d72b0173aca2eb2c07a1b477430a33fe0d5 @YuriyBosov YuriyBosov committed Sep 23, 2012
View
BIN .DS_Store
Binary file not shown.
@@ -18,10 +18,16 @@
MUValidator* validator;
MUTextView_Holder* delegateHolder;
MUFilter *filter;
+
+ NSString* placeholder;
+ UIColor* placeholderColor;
}
@property (nonatomic, assign) id<UITextViewDelegate> mudelegate;
@property (nonatomic, copy) NSString* observedText;
@property (nonatomic, retain) MUFilter *filter;
+@property (nonatomic, retain) NSString* placeholder;
+@property (nonatomic, retain) UIColor* placeholderColor;
+
@end
@@ -24,7 +24,10 @@ @interface MUTextView_Holder : NSObject <UITextViewDelegate>
//==========================================================================================
@interface MUTextView ()
+@property (nonatomic, retain) UITextView* lbPlaceholder;
+
- (void) setup;
+- (void) setHiddenPlaceHolder:(BOOL)hidden;
@end
@@ -36,6 +39,11 @@ @implementation MUTextView
@synthesize observedText;
@synthesize filter;
+@synthesize placeholder;
+@synthesize placeholderColor;
+
+@synthesize lbPlaceholder = _lbPlaceholder;
+
//==========================================================================================
- (id) init
{
@@ -74,6 +82,9 @@ - (void) dealloc
[validator release];
[filter release];
+ [placeholder release];
+ [placeholderColor release];
+
[super dealloc];
}
@@ -82,6 +93,9 @@ - (void) setup
{
delegateHolder = [MUTextView_Holder new];
delegateHolder.holded = self;
+
+ placeholderColor = [[UIColor grayColor] retain];
+
super.delegate = delegateHolder;
}
@@ -121,6 +135,63 @@ - (BOOL) validate
return (validator) ? ([validator validate]) : (YES);
}
+#pragma mark - Placeholder
+//==========================================================================================
+- (UITextView*)lbPlaceholder
+{
+ if (!_lbPlaceholder)
+ {
+ _lbPlaceholder = [[[UITextView alloc] initWithFrame:self.bounds] autorelease];
+ _lbPlaceholder.backgroundColor = [UIColor clearColor];
+ _lbPlaceholder.text = placeholder;
+ _lbPlaceholder.textColor = placeholderColor;
+ _lbPlaceholder.font = self.font;
+ _lbPlaceholder.userInteractionEnabled = NO;
+ _lbPlaceholder.hidden = [self.text length] > 0;
+ _lbPlaceholder.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
+ [self addSubview:_lbPlaceholder];
+ }
+ return _lbPlaceholder;
+}
+
+//==========================================================================================
+- (void)setPlaceholder:(NSString *)aPlaceholder
+{
+ [placeholder release];
+ placeholder = [aPlaceholder retain];
+ self.lbPlaceholder.text = placeholder;
+}
+
+//==========================================================================================
+- (void)setPlaceholderColor:(UIColor *)aPlaceholderColor
+{
+ [placeholderColor release];
+ placeholderColor = [aPlaceholderColor retain];
+ self.lbPlaceholder.textColor = placeholderColor;
+}
+
+//==========================================================================================
+- (void)setText:(NSString *)text
+{
+ [super setText:text];
+ if (placeholder)
+ self.lbPlaceholder.hidden = [self.text length] > 0;
+}
+
+//==========================================================================================
+- (void)setFont:(UIFont *)font
+{
+ [super setFont:font];
+ if (placeholder)
+ self.lbPlaceholder.font = font;
+}
+
+//==========================================================================================
+- (void)setHiddenPlaceHolder:(BOOL)hidden
+{
+ self.lbPlaceholder.hidden = hidden;
+}
+
#pragma mark - UITextViewDelegate
//==========================================================================================
- (void) setDelegate:(id<UITextViewDelegate>)delegate
@@ -200,6 +271,16 @@ - (BOOL)textView:(MUTextView *)textView shouldChangeTextInRange:(NSRange)range r
if([holded.mudelegate respondsToSelector:@selector(textView:shouldChangeTextInRange:replacementText:)])
result = [holded.mudelegate textView:textView shouldChangeTextInRange:range replacementText:text];
+ if (([textView.text length] == 1 && [text length] == 0) ||
+ ([textView.text length] == range.length && range.length != 0))
+ {
+ [textView setHiddenPlaceHolder:NO];
+ }
+ else
+ {
+ [textView setHiddenPlaceHolder:[textView.text length] > 0 || [text length] > 0];
+ }
+
return result;
}
@@ -24,4 +24,7 @@
@property (nonatomic, retain) MUValidator *validator;
@property (nonatomic, retain) MUFilter *filter;
+@property (nonatomic, retain) NSString* placeholder;
+@property (nonatomic, retain) UIColor* placeholderColor;
+
@end
@@ -22,6 +22,9 @@ @implementation MUCellDataTextView
@synthesize validator;
@synthesize filter;
+@synthesize placeholder;
+@synthesize placeholderColor;
+
#pragma mark - Init/Dealloc
//==============================================================================
- (id) initWithObject:(NSObject *)aObject key:(NSString *)aKey
@@ -83,6 +83,11 @@ - (void) setupCellData:(MUCellData *)aCellData
textView.validator = cellDataTextView.validator;
textView.filter = cellDataTextView.filter;
+
+ if (cellDataTextView.placeholder)
+ textView.placeholder = cellDataTextView.placeholder;
+ if (cellDataTextView.placeholderColor)
+ textView.placeholderColor = cellDataTextView.placeholderColor;
// title
titleLabel.frame = CGRectMake(10, 0, self.bounds.size.width - 20, titleHeight);
@@ -102,7 +102,7 @@ - (void)viewDidLoad
MUCellDataTextView* cdTextView = [[[MUCellDataTextView alloc] initWithObject:testObject key:@"strValue"] autorelease];
cdTextView.title = @"Title";
-// cdTextView.enableEdit = NO;
+ cdTextView.placeholder = @"Test place holder";
[section addCellData:cdTextView];
MUCellDataSwitch *cellDataSwitch = [[[MUCellDataSwitch alloc] initWithObject:nil key:nil] autorelease];

0 comments on commit da905d7

Please sign in to comment.