定制textField

//
//  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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值