Create application/directory for subdir, if metadata or permissions are applied.
[pithos-ios] / Classes / RSTextFieldCell.m
1 //
2 //  RSTextFieldCell.m
3 //  RSCustomViews
4 //
5 //  Created by Mike Mayo on 1/19/11.
6 //  Copyright 2011 __MyCompanyName__. All rights reserved.
7 //
8
9 #import "RSTextFieldCell.h"
10 #import "UIColor+MoreColors.h"
11
12
13 @implementation RSTextFieldCell
14
15 @synthesize textField, modalPresentationStyle, fixedTextLabel;
16
17 - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
18     self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
19     if (self) {
20         [self initializeSubviews:style withFixedTextLabel:nil];
21     }
22     return self;
23 }
24
25 - (id)initCellWithFixedLabel:(NSString *)fixedLabelText withStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
26     self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
27     if (self) {
28         [self initializeSubviews:style withFixedTextLabel:fixedLabelText];
29     }
30     return self;
31 }
32     
33 - (void)initializeSubviews:(UITableViewCellStyle)style withFixedTextLabel:(NSString *)fixedLabelText {
34     self.selectionStyle = UITableViewCellSelectionStyleNone;
35     
36     textField = [[UITextField alloc] initWithFrame:self.detailTextLabel.frame];
37     textField.textAlignment = self.detailTextLabel.textAlignment;
38     textField.returnKeyType = UIReturnKeyDone;
39     textField.backgroundColor = [UIColor clearColor];
40     textField.adjustsFontSizeToFitWidth = NO;
41     textField.autocorrectionType = UITextAutocorrectionTypeNo;
42     textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
43     
44     if (style == UITableViewCellStyleValue1) {
45         textField.font = [UIFont systemFontOfSize:17.0];
46     } else if (style == UITableViewCellStyleValue2) {
47         textField.font = [UIFont boldSystemFontOfSize:15.0];
48     }
49     
50     textField.textColor = self.detailTextLabel.textColor;        
51     [self addSubview:textField];
52     
53     self.detailTextLabel.textColor = [UIColor clearColor];
54     self.detailTextLabel.backgroundColor = [UIColor clearColor];
55     self.detailTextLabel.highlightedTextColor = [UIColor clearColor];
56     self.detailTextLabel.text = @"Using a very long string here to make sure that the UILabel is rendered at the maximum width so we can copy it for the UITextField.";  
57     
58     if (fixedLabelText) {
59         if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
60             fixedTextLabel = [[UILabel alloc] initWithFrame:CGRectMake(21.0, 14.5, 458.0, 18.0)];
61         } else {
62             fixedTextLabel = [[UILabel alloc] initWithFrame:CGRectMake(21.0, 14.5, 280.0, 18.0)];
63         }    
64         fixedTextLabel.font = [UIFont systemFontOfSize:17.0];
65         fixedTextLabel.textColor = [UIColor value1DetailTextLabelColor];
66         fixedTextLabel.backgroundColor = [UIColor clearColor];
67         fixedTextLabel.textAlignment = UITextAlignmentRight;
68         fixedTextLabel.text = fixedLabelText;
69         [self addSubview:fixedTextLabel];
70     }
71 }
72
73
74 - (void)drawRect:(CGRect)rect {
75     [super drawRect:rect];
76
77     CGRect aRect = self.detailTextLabel.frame;
78     
79     if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
80         if (modalPresentationStyle == UIModalPresentationFormSheet) {
81             textField.frame = CGRectMake(aRect.origin.x + 31.0, aRect.origin.y + 1.0, aRect.size.width - 24, aRect.size.height);
82         } else {
83             textField.frame = CGRectMake(aRect.origin.x + 35.0, aRect.origin.y + 1.0, aRect.size.width - 24, aRect.size.height);
84         }
85     } else {
86         textField.frame = CGRectMake(aRect.origin.x + 10.0, aRect.origin.y + 1.0, aRect.size.width, aRect.size.height);
87     }
88     
89     if (fixedTextLabel) {
90         int xOffset;
91         if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
92             xOffset = 5;
93         else
94             xOffset = 3;
95         CGSize fixedLabelTextSize = [fixedTextLabel.text sizeWithFont:fixedTextLabel.font constrainedToSize:CGSizeMake(280.0, 900.0f)];
96         textField.frame = CGRectMake(textField.frame.origin.x,
97                                      textField.frame.origin.y,
98                                      textField.frame.size.width - fixedLabelTextSize.width + xOffset,
99                                      textField.frame.size.height);
100     }
101     
102     // this isn't generic.  this is for text decoration on the end of the textField
103     if (self.accessoryView) {
104         aRect = textField.frame;
105         aRect.origin.x += 9.0;
106         textField.frame = aRect;
107     }
108     
109     textField.contentStretch = self.detailTextLabel.contentStretch;
110     textField.backgroundColor = [UIColor clearColor];
111     textField.transform = self.detailTextLabel.transform;
112     textField.clipsToBounds = self.detailTextLabel.clipsToBounds;
113     textField.clearsContextBeforeDrawing = self.detailTextLabel.clearsContextBeforeDrawing;
114     textField.contentMode = self.detailTextLabel.contentMode;
115     textField.autoresizingMask = self.detailTextLabel.autoresizingMask;
116     textField.autoresizesSubviews = YES;
117     textField.font = self.detailTextLabel.font;
118 }
119
120 - (void)dealloc {
121     [textField release];
122     [fixedTextLabel release];
123     [super dealloc];
124 }
125
126 @end