// // 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
时间: 2024-11-05 16:25:43