From e93ede2fdbd718688f33136168d2ed336c339aea Mon Sep 17 00:00:00 2001 From: Roman Efimov Date: Thu, 11 Apr 2013 10:51:19 -0500 Subject: [PATCH] TextView fixes --- .../Cells/RETableViewLongTextCell.h | 3 +- .../Cells/RETableViewLongTextCell.m | 16 +++- RETableViewManager/REPlaceholderTextView.h | 19 +++++ RETableViewManager/REPlaceholderTextView.m | 77 +++++++++++++++++++ .../project.pbxproj | 8 +- .../Classes/Controllers/RootViewController.m | 5 +- 6 files changed, 121 insertions(+), 7 deletions(-) create mode 100644 RETableViewManager/REPlaceholderTextView.h create mode 100644 RETableViewManager/REPlaceholderTextView.m diff --git a/RETableViewManager/Cells/RETableViewLongTextCell.h b/RETableViewManager/Cells/RETableViewLongTextCell.h index b487284..0acbe03 100644 --- a/RETableViewManager/Cells/RETableViewLongTextCell.h +++ b/RETableViewManager/Cells/RETableViewLongTextCell.h @@ -8,10 +8,11 @@ #import "RETableViewCell.h" #import "RELongTextItem.h" +#import "REPlaceholderTextView.h" @interface RETableViewLongTextCell : RETableViewCell @property (strong, readwrite, nonatomic) RELongTextItem *item; -@property (strong, readwrite, nonatomic) UITextView *textView; +@property (strong, readwrite, nonatomic) REPlaceholderTextView *textView; @end diff --git a/RETableViewManager/Cells/RETableViewLongTextCell.m b/RETableViewManager/Cells/RETableViewLongTextCell.m index 6dd701f..65944f5 100644 --- a/RETableViewManager/Cells/RETableViewLongTextCell.m +++ b/RETableViewManager/Cells/RETableViewLongTextCell.m @@ -33,12 +33,11 @@ { [super cellDidLoad]; - _textView = [[UITextView alloc] initWithFrame:CGRectNull]; + _textView = [[REPlaceholderTextView alloc] initWithFrame:CGRectNull]; _textView.inputAccessoryView = self.actionBar; _textView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; - _textView.frame = self.contentView.frame; - _textView.backgroundColor = [UIColor lightGrayColor]; + _textView.backgroundColor = [UIColor clearColor]; _textView.delegate = self; [self.contentView addSubview:_textView]; } @@ -55,8 +54,9 @@ { [super cellWillAppear]; + _textView.frame = CGRectMake(self.contentView.frame.origin.x + 2, self.contentView.frame.origin.y + 2, self.contentView.frame.size.width - 4, self.contentView.frame.size.height - 4); _textView.text = self.item.value; - // _textView.placeholder = self.item.placeholder; + _textView.placeholder = self.item.placeholder; _textView.font = self.tableViewManager.style.textFieldFont; _textView.autocapitalizationType = self.item.autocapitalizationType; _textView.autocorrectionType = self.item.autocorrectionType; @@ -76,6 +76,14 @@ - (void)layoutSubviews { [super layoutSubviews]; + + /*CGFloat cellOffset = UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone ? 10 : 60; + //CGFloat fieldOffset = UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone ? 10 : 40; + //CGFloat width = 0; + CGRect frame = CGRectMake(0, self.tableViewManager.style.textFieldPositionOffset.height, 10, self.frame.size.height - self.tableViewManager.style.textFieldPositionOffset.height); + frame.origin.x = cellOffset + self.tableViewManager.style.textFieldPositionOffset.width; + frame.size.width = self.frame.size.width - frame.origin.x - cellOffset; + _textView.frame = frame;*/ } #pragma mark - diff --git a/RETableViewManager/REPlaceholderTextView.h b/RETableViewManager/REPlaceholderTextView.h new file mode 100644 index 0000000..8010372 --- /dev/null +++ b/RETableViewManager/REPlaceholderTextView.h @@ -0,0 +1,19 @@ +// +// REPlaceholderTextView.h +// RETableViewManagerExample +// +// Created by Roman Efimov on 4/11/13. +// Copyright (c) 2013 Roman Efimov. All rights reserved. +// + +#import + +@interface REPlaceholderTextView : UITextView + +@property (strong, readonly, nonatomic) UILabel *placeHolderLabel; +@property (copy, readwrite, nonatomic) NSString *placeholder; +@property (strong, readwrite, nonatomic) UIColor *placeholderColor; + +- (void)textChanged:(NSNotification*)notification; + +@end diff --git a/RETableViewManager/REPlaceholderTextView.m b/RETableViewManager/REPlaceholderTextView.m new file mode 100644 index 0000000..63cdd4b --- /dev/null +++ b/RETableViewManager/REPlaceholderTextView.m @@ -0,0 +1,77 @@ +// +// REPlaceholderTextView.m +// RETableViewManagerExample +// +// Created by Roman Efimov on 4/11/13. +// Copyright (c) 2013 Roman Efimov. All rights reserved. +// + +#import "REPlaceholderTextView.h" + +@implementation REPlaceholderTextView + +- (void)awakeFromNib +{ + [super awakeFromNib]; + self.placeholder = @""; + self.placeholderColor = [UIColor lightGrayColor]; + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChanged:) name:UITextViewTextDidChangeNotification object:nil]; +} + +- (id)initWithFrame:(CGRect)frame +{ + if ((self = [super initWithFrame:frame])) { + self.placeholder = @""; + self.placeholderColor = [UIColor lightGrayColor]; + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChanged:) name:UITextViewTextDidChangeNotification object:nil]; + } + return self; +} + +- (void)textChanged:(NSNotification *)notification +{ + if (self.placeholder.length == 0) { + return; + } + + if (self.text.length == 0) { + [[self viewWithTag:999] setAlpha:1]; + } else { + [[self viewWithTag:999] setAlpha:0]; + } +} + +- (void)setText:(NSString *)text +{ + [super setText:text]; + [self textChanged:nil]; +} + +- (void)drawRect:(CGRect)rect +{ + if (self.placeholder.length > 0) { + if (!_placeHolderLabel) { + _placeHolderLabel = [[UILabel alloc] initWithFrame:CGRectMake(8, 8, self.bounds.size.width - 16, 0)]; + _placeHolderLabel.lineBreakMode = NSLineBreakByWordWrapping; + _placeHolderLabel.numberOfLines = 0; + _placeHolderLabel.font = self.font; + _placeHolderLabel.backgroundColor = [UIColor clearColor]; + _placeHolderLabel.textColor = self.placeholderColor; + _placeHolderLabel.alpha = 0; + _placeHolderLabel.tag = 999; + [self addSubview:_placeHolderLabel]; + } + + _placeHolderLabel.text = self.placeholder; + [_placeHolderLabel sizeToFit]; + [self sendSubviewToBack:_placeHolderLabel]; + } + + if (self.text.length == 0 && self.placeholder.length > 0) { + [[self viewWithTag:999] setAlpha:1]; + } + + [super drawRect:rect]; +} + +@end diff --git a/RETableViewManagerExample/RETableViewManagerExample.xcodeproj/project.pbxproj b/RETableViewManagerExample/RETableViewManagerExample.xcodeproj/project.pbxproj index 963ee3e..840e67e 100644 --- a/RETableViewManagerExample/RETableViewManagerExample.xcodeproj/project.pbxproj +++ b/RETableViewManagerExample/RETableViewManagerExample.xcodeproj/project.pbxproj @@ -7,6 +7,7 @@ objects = { /* Begin PBXBuildFile section */ + 30082D1A17170EDD008B0DB5 /* REPlaceholderTextView.m in Sources */ = {isa = PBXBuildFile; fileRef = 30082D1917170EDD008B0DB5 /* REPlaceholderTextView.m */; }; 300E5A26170F486800910013 /* REFloatItem.m in Sources */ = {isa = PBXBuildFile; fileRef = 300E5A25170F486800910013 /* REFloatItem.m */; }; 300E5A29170F48EC00910013 /* RETableViewFloatCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 300E5A28170F48EC00910013 /* RETableViewFloatCell.m */; }; 300E5A2C170F621400910013 /* REDateTimeItem.m in Sources */ = {isa = PBXBuildFile; fileRef = 300E5A2B170F621400910013 /* REDateTimeItem.m */; }; @@ -49,6 +50,8 @@ /* End PBXBuildFile section */ /* Begin PBXFileReference section */ + 30082D1817170EDD008B0DB5 /* REPlaceholderTextView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = REPlaceholderTextView.h; sourceTree = ""; }; + 30082D1917170EDD008B0DB5 /* REPlaceholderTextView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = REPlaceholderTextView.m; sourceTree = ""; }; 300E5A24170F486800910013 /* REFloatItem.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = REFloatItem.h; sourceTree = ""; }; 300E5A25170F486800910013 /* REFloatItem.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = REFloatItem.m; sourceTree = ""; }; 300E5A27170F48EC00910013 /* RETableViewFloatCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RETableViewFloatCell.h; sourceTree = ""; }; @@ -188,19 +191,21 @@ 30DB065216E934AD006C9530 /* Items */, 30DB065D16E934AD006C9530 /* REActionBar.h */, 30DB065E16E934AD006C9530 /* REActionBar.m */, + 30082D1817170EDD008B0DB5 /* REPlaceholderTextView.h */, + 30082D1917170EDD008B0DB5 /* REPlaceholderTextView.m */, 30DB065F16E934AD006C9530 /* RETableViewCell.h */, 30DB066016E934AD006C9530 /* RETableViewCell.m */, 30DB066116E934AD006C9530 /* RETableViewCellStyle.h */, 30DB066216E934AD006C9530 /* RETableViewCellStyle.m */, 30DB066316E934AD006C9530 /* RETableViewItem.h */, 30DB066416E934AD006C9530 /* RETableViewItem.m */, + 30DB066516E934AD006C9530 /* RETableViewManager.bundle */, 30DB066616E934AD006C9530 /* RETableViewManager.h */, 30DB066716E934AD006C9530 /* RETableViewManager.m */, 30F41316170E07BB0004EBAE /* RETableViewOptionsController.h */, 30F41317170E07BB0004EBAE /* RETableViewOptionsController.m */, 30DB066816E934AD006C9530 /* RETableViewSection.h */, 30DB066916E934AD006C9530 /* RETableViewSection.m */, - 30DB066516E934AD006C9530 /* RETableViewManager.bundle */, ); name = RETableViewManager; path = ../RETableViewManager; @@ -419,6 +424,7 @@ 300E5A2F170F63B600910013 /* RETableViewDateTimeCell.m in Sources */, 300E5B3F17170AE100910013 /* RELongTextItem.m in Sources */, 300E5B4217170B0200910013 /* RETableViewLongTextCell.m in Sources */, + 30082D1A17170EDD008B0DB5 /* REPlaceholderTextView.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/RETableViewManagerExample/RETableViewManagerExample/Classes/Controllers/RootViewController.m b/RETableViewManagerExample/RETableViewManagerExample/Classes/Controllers/RootViewController.m index 48c7a89..47847c3 100644 --- a/RETableViewManagerExample/RETableViewManagerExample/Classes/Controllers/RootViewController.m +++ b/RETableViewManagerExample/RETableViewManagerExample/Classes/Controllers/RootViewController.m @@ -47,7 +47,10 @@ [weakSelf.navigationController pushViewController:[[ListViewController alloc] initWithStyle:UITableViewStylePlain] animated:YES]; }]]; - [section addItem:[RELongTextItem itemWithTitle:@"" value:@""]]; + RETextItem *fullLengthField = [RETextItem itemWithTitle:nil value:nil placeholder:@"Full length text field"]; + [section addItem:fullLengthField]; + + [section addItem:[RELongTextItem itemWithValue:@"Full length text field" placeholder:@"Enter text"]]; } @end