Statistics
| Branch: | Tag: | Revision:

root / Classes / RSTextFieldCell.m @ 64929bae

History | View | Annotate | Download (5 kB)

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
    if (self.detailTextLabel) {
51
        textField.textColor = self.detailTextLabel.textColor;        
52
    }
53
    [self addSubview:textField];
54
    
55
    self.detailTextLabel.textColor = [UIColor clearColor];
56
    self.detailTextLabel.backgroundColor = [UIColor clearColor];
57
    self.detailTextLabel.highlightedTextColor = [UIColor clearColor];
58
    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.";  
59
    
60
    if (fixedLabelText) {
61
        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
62
            fixedTextLabel = [[UILabel alloc] initWithFrame:CGRectMake(21.0, 14.5, 458.0, 18.0)];
63
        } else {
64
            fixedTextLabel = [[UILabel alloc] initWithFrame:CGRectMake(21.0, 14.5, 280.0, 18.0)];
65
        }    
66
        fixedTextLabel.font = [UIFont systemFontOfSize:17.0];
67
        fixedTextLabel.textColor = [UIColor value1DetailTextLabelColor];
68
        fixedTextLabel.backgroundColor = [UIColor clearColor];
69
        fixedTextLabel.textAlignment = UITextAlignmentRight;
70
        fixedTextLabel.text = fixedLabelText;
71
        [self addSubview:fixedTextLabel];
72
    }
73
}
74

    
75

    
76
- (void)drawRect:(CGRect)rect {
77
    [super drawRect:rect];
78

    
79
    CGRect aRect = self.detailTextLabel.frame;
80
    
81
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
82
        if (modalPresentationStyle == UIModalPresentationFormSheet) {
83
            textField.frame = CGRectMake(aRect.origin.x + 31.0, aRect.origin.y + 1.0, aRect.size.width - 24, aRect.size.height);
84
        } else {
85
            textField.frame = CGRectMake(aRect.origin.x + 35.0, aRect.origin.y + 1.0, aRect.size.width - 24, aRect.size.height);
86
        }
87
    } else {
88
        textField.frame = CGRectMake(aRect.origin.x + 10.0, aRect.origin.y + 1.0, aRect.size.width, aRect.size.height);
89
    }
90
    
91
    if (fixedTextLabel) {
92
        int xOffset;
93
        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
94
            xOffset = 5;
95
        else
96
            xOffset = 3;
97
        CGSize fixedLabelTextSize = [fixedTextLabel.text sizeWithFont:fixedTextLabel.font constrainedToSize:CGSizeMake(280.0, 900.0f)];
98
        textField.frame = CGRectMake(textField.frame.origin.x,
99
                                     textField.frame.origin.y,
100
                                     textField.frame.size.width - fixedLabelTextSize.width + xOffset,
101
                                     textField.frame.size.height);
102
    }
103
    
104
    // this isn't generic.  this is for text decoration on the end of the textField
105
    if (self.accessoryView) {
106
        aRect = textField.frame;
107
        aRect.origin.x += 9.0;
108
        textField.frame = aRect;
109
    }
110
    
111
    textField.contentStretch = self.detailTextLabel.contentStretch;
112
    textField.backgroundColor = [UIColor clearColor];
113
    textField.transform = self.detailTextLabel.transform;
114
    textField.clipsToBounds = self.detailTextLabel.clipsToBounds;
115
    textField.clearsContextBeforeDrawing = self.detailTextLabel.clearsContextBeforeDrawing;
116
    textField.contentMode = self.detailTextLabel.contentMode;
117
    textField.autoresizingMask = self.detailTextLabel.autoresizingMask;
118
    textField.autoresizesSubviews = YES;
119
    textField.font = self.detailTextLabel.font;
120
}
121

    
122
- (void)dealloc {
123
    [textField release];
124
    [fixedTextLabel release];
125
    [super dealloc];
126
}
127

    
128
@end