-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathTECustomTextField.m
71 lines (59 loc) · 2.66 KB
/
TECustomTextField.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//
// TECustomTextField.m
// CelebrityApp
//
// Created by Asif Ali on 12/9/15.
// Copyright © 2015 MobiWhiz. All rights reserved.
//
#define kTitleOffset 15.0
#define kImageOffset 5.0
#define kIconWidth 20.0
#import "TECustomTextField.h"
@implementation TECustomTextField
-(void) awakeFromNib {
[super awakeFromNib];
_iconOffset = _iconOffset == 0 ? kImageOffset : _iconOffset;
_titleOffset = _titleOffset == 0 ? kTitleOffset : _titleOffset;
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kIconWidth + _titleOffset + _iconOffset, 22)];
if (_leftIcon) {
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(_iconOffset, 0, kIconWidth, 20)];
[imageView setContentMode:UIViewContentModeScaleAspectFit];
[imageView setImage:_leftIcon];
[view addSubview:imageView];
self.leftView = view;
self.leftViewMode = UITextFieldViewModeAlways;
}
if (_rightIcon) {
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(_iconOffset, 0, kIconWidth, 20)];
[imageView setContentMode:UIViewContentModeScaleAspectFit];
[imageView setImage:_rightIcon];
[view addSubview:imageView];
self.rightView = view;
self.rightViewMode = UITextFieldViewModeAlways;
}
}
-(void) setLeftIcon:(UIImage *)leftIcon {
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kIconWidth + _titleOffset + _iconOffset, 22)];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(_iconOffset, 0, kIconWidth, 20)];
[imageView setContentMode:UIViewContentModeScaleAspectFit];
[imageView setImage:leftIcon];
[view addSubview:imageView];
self.leftView = view;
self.leftViewMode = UITextFieldViewModeAlways;
}
-(void) setRightIcon:(UIImage *)rightIcon {
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kIconWidth + _titleOffset + _iconOffset, 22)];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(_iconOffset, 0, kIconWidth, 20)];
[imageView setContentMode:UIViewContentModeScaleAspectFit];
[imageView setImage:rightIcon];
[view addSubview:imageView];
self.rightView = view;
self.rightViewMode = UITextFieldViewModeAlways;
}
-(void) drawPlaceholderInRect:(CGRect)rect {
NSAttributedString *string = [[NSAttributedString alloc] initWithString:self.placeholder
attributes:@{ NSForegroundColorAttributeName : self.placeholderColor != nil ? self.placeholderColor : [UIColor lightGrayColor] }];
self.attributedPlaceholder = string;
[super drawPlaceholderInRect:rect];
}
@end