//
// EATextField.h
// EATextField
//
// Created by Eiwodetianna on 2017/8/9.
// Copyright © 2017年 lanou. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface EATextField : UITextField
@property (nonatomic, retain) UIImage *leftImage;
@property (nonatomic, retain) UIImage *leftHighlightedImage;
@property (nonatomic, assign) CGSize leftViewSize;
@property (nonatomic, retain) UIColor *placeholderTextColor;
@end
//
// EATextField.m
// EATextField
//
// Created by Eiwodetianna on 2017/8/9.
// Copyright © 2017年 lanou. All rights reserved.
//
#import "EATextField.h"
@interface EATextField ()
@property (nonatomic, retain) UIImageView *leftImageView;
@property (nonatomic, retain) NSMutableDictionary *placeholderAttributes;
@end
@implementation EATextField
- (UIImageView *)leftImageView {
if (_leftImageView == nil) {
CGRect leftFrame = self.bounds;
leftFrame.size.width = self.bounds.size.height;
self.leftView = [[UIImageView alloc] initWithFrame:leftFrame];
self.leftImageView = (UIImageView *)self.leftView;
self.placeholderAttributes = [NSMutableDictionary dictionary];
}
return _leftImageView;
}
- (void)setLeftImage:(UIImage *)leftImage {
if (_leftImage != leftImage) {
_leftImage = leftImage;
self.leftImageView.image = leftImage;
self.leftViewMode = UITextFieldViewModeAlways;
}
}
- (void)setLeftHighlightedImage:(UIImage *)leftHighlightedImage {
if (_leftHighlightedImage != leftHighlightedImage) {
_leftHighlightedImage = leftHighlightedImage;
self.leftImageView.highlightedImage = leftHighlightedImage;
self.leftViewMode = UITextFieldViewModeAlways;
}
}
- (BOOL)isEditing {
BOOL isEditing = [super isEditing];
self.leftImageView.highlighted = isEditing;
return isEditing;
}
- (void)setLeftView:(UIView *)leftView {
[super setLeftView:leftView];
self.leftImageView = nil;
}
- (void)setLeftViewSize:(CGSize)leftViewSize {
CGRect leftImageViewFrame = self.leftImageView.frame;
leftImageViewFrame.size = leftViewSize;
_leftViewSize = leftViewSize;
self.leftImageView.frame = leftImageViewFrame;
}
- (void)setPlaceholderTextColor:(UIColor *)placeholderTextColor {
if (_placeholderTextColor != placeholderTextColor) {
_placeholderTextColor = placeholderTextColor;
if (placeholderTextColor != nil) {
_placeholderAttributes[NSForegroundColorAttributeName] = placeholderTextColor;
if (self.placeholder) {
NSAttributedString *attributePlaceholder = [[NSAttributedString alloc] initWithString:self.placeholder attributes:_placeholderAttributes];
self.attributedPlaceholder = attributePlaceholder;
}
}
}
}
- (void)setPlaceholder:(NSString *)placeholder {
[super setPlaceholder:placeholder];
if (_placeholderTextColor != nil && placeholder != nil) {
NSAttributedString *attributePlaceholder = [[NSAttributedString alloc] initWithString:self.placeholder attributes:_placeholderAttributes];
self.attributedPlaceholder = attributePlaceholder;
}
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
@end
//
// ViewController.m
// EATextField
//
// Created by Eiwodetianna on 2017/8/9.
// Copyright © 2017年 lanou. All rights reserved.
//
#import "ViewController.h"
#import "EATextField.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
EATextField *text = [[EATextField alloc] initWithFrame:CGRectMake(100, 100, 200, 40)];
text.leftImage = [UIImage imageNamed:@"icon_username.png"];
text.leftHighlightedImage = [UIImage imageNamed:@"icon_username_select.png"];
text.leftViewSize = CGSizeMake(30, 30);
text.placeholderTextColor = [UIColor greenColor];
text.placeholder = @"placeholder";
text.backgroundColor = [UIColor redColor];
[self.view addSubview:text];
UITextField *text1 = [[EATextField alloc] initWithFrame:CGRectMake(100, 300, 200, 40)];
text1.backgroundColor = [UIColor redColor];
[self.view addSubview:text1];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[self.view endEditing:YES];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end