TextView fixes

This commit is contained in:
Roman Efimov
2013-04-11 10:51:19 -05:00
parent 248f12cd80
commit e93ede2fdb
6 changed files with 121 additions and 7 deletions

View File

@@ -8,10 +8,11 @@
#import "RETableViewCell.h"
#import "RELongTextItem.h"
#import "REPlaceholderTextView.h"
@interface RETableViewLongTextCell : RETableViewCell <UITextViewDelegate>
@property (strong, readwrite, nonatomic) RELongTextItem *item;
@property (strong, readwrite, nonatomic) UITextView *textView;
@property (strong, readwrite, nonatomic) REPlaceholderTextView *textView;
@end

View File

@@ -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 -

View File

@@ -0,0 +1,19 @@
//
// REPlaceholderTextView.h
// RETableViewManagerExample
//
// Created by Roman Efimov on 4/11/13.
// Copyright (c) 2013 Roman Efimov. All rights reserved.
//
#import <UIKit/UIKit.h>
@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

View File

@@ -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

View File

@@ -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 = "<group>"; };
30082D1917170EDD008B0DB5 /* REPlaceholderTextView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = REPlaceholderTextView.m; sourceTree = "<group>"; };
300E5A24170F486800910013 /* REFloatItem.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = REFloatItem.h; sourceTree = "<group>"; };
300E5A25170F486800910013 /* REFloatItem.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = REFloatItem.m; sourceTree = "<group>"; };
300E5A27170F48EC00910013 /* RETableViewFloatCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RETableViewFloatCell.h; sourceTree = "<group>"; };
@@ -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;
};

View File

@@ -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