![](http://static.blog.csdn.net/images/category_icon.jpg)
版权声明:本CSDN博客所有文章不会即时更新,请关注个人博客:http://www.huangyibiao.com/
- //
- // HYBTextField.h
- // CloudShopping
- //
- // Created by sixiaobo on 14-7-10.
- // Copyright (c) 2014年 com.Uni2uni. All rights reserved.
- //
- #import <UIKit/UIKit.h>
- /*!
- * @brief 自定义TextField,用于修改默认textfield的属性为我们工程中需要的属性
- * @author huangyibiao
- */
- @interface HYBTextField : UITextField
- @property (nonatomic, strong) UIColor *placeholderColor;
- @property (nonatomic, strong) UIFont *placeholderFont;
- @property (nonatomic, assign) CGFloat leftPadding;
- // 默认leftPadding = 8.0
- - (id)initWithFrame:(CGRect)frame placeholderColor:(UIColor *)color font:(UIFont *)font;
- - (id)initWithFrame:(CGRect)frame placeholderColor:(UIColor *)color font:(UIFont *)font leftPadding:(CGFloat)leftPadding;
- @end
- //
- // HYBTextField.m
- // CloudShopping
- //
- // Created by sixiaobo on 14-7-10.
- // Copyright (c) 2014年 com.Uni2uni. All rights reserved.
- //
- #import "HYBTextField.h"
- @implementation HYBTextField
- - (id)initWithFrame:(CGRect)frame placeholderColor:(UIColor *)color font:(UIFont *)font {
- return [self initWithFrame:frame placeholderColor:color font:font leftPadding:8];
- }
- - (id)initWithFrame:(CGRect)frame
- placeholderColor:(UIColor *)color
- font:(UIFont *)font
- leftPadding:(CGFloat)leftPadding {
- if (self = [super initWithFrame:frame]) {
- self.placeholderColor = color;
- self.placeholderFont = font;
- self.leftPadding = leftPadding;
- self.autocapitalizationType = UITextAutocapitalizationTypeNone;
- self.autocorrectionType = UITextAutocorrectionTypeNo;
- self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
- self.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
- self.borderStyle = UITextBorderStyleNone;
- self.backgroundColor = [UIColor whiteColor];
- }
- return self;
- }
- - (void)drawPlaceholderInRect:(CGRect)rect {
- [kColorWith16RGB(0xa8a8a8) setFill];
- [[self placeholder] drawInRect:CGRectMake(self.leftPadding, rect.origin.y, rect.size.width, rect.size.height)
- withFont:self.placeholderFont];
- return;
- }
- // 控制编辑文本的位置
- - (CGRect)editingRectForBounds:(CGRect)bounds {
- CGFloat padding = self.leftPadding;
- if (self.textAlignment == NSTextAlignmentRight) {
- padding = 0;
- }
- CGRect inset = CGRectMake(bounds.origin.x + padding, bounds.origin.y,
- bounds.size.width, bounds.size.height);
- return inset;
- }
- - (CGRect)placeholderRectForBounds:(CGRect)bounds {
- NSString *obtainSizeString = self.text;
- CGSize size = [obtainSizeString sizeWithFont:self.placeholderFont];
- return CGRectMake(bounds.origin.x, (bounds.size.height - size.height) / 2,
- bounds.size.width, bounds.size.height);
- }
- // 控制显示文本的位置
- - (CGRect)textRectForBounds:(CGRect)bounds {
- CGFloat padding = self.leftPadding;
- if (self.textAlignment == NSTextAlignmentRight) {
- padding = 0;
- }
- CGRect inset = CGRectMake(bounds.origin.x + padding, bounds.origin.y,
- bounds.size.width, bounds.size.height);
- return inset;
- }
- @end