自定义textFeild

#import <UIKit/UIKit.h>


@interface NDTextFieldPadding : UITextField

{

    BOOL isEnablePadding;

    float paddingLeft;

    float paddingRight;

    float paddingTop;

    float paddingBottom;

}


+ (NDTextFieldPadding *) createRightAngleRectTextFieldWithFont:(UIFont *)font;

+ (NDTextFieldPadding *) createRightAngleRectTextFieldWithFont:(UIFont *)font textAlignment:(NSTextAlignment)textAlignment;


+ (NDTextFieldPadding *) createRoundTextFieldWithFont:(UIFont *)font;

+ (NDTextFieldPadding *) createRoundTextFieldWithFont:(UIFont *)font textAlignment:(NSTextAlignment)textAlignment;


- (void)setPadding:(BOOL)enable top:(float)top right:(float)right bottom:(float)bottom left:(float)left;


@end

#import "NDTextFieldPadding.h"


@implementation NDTextFieldPadding

#pragma mark - 类方法


+ (NDTextFieldPadding *) createRightAngleRectTextFieldWithFont:(UIFont *)font

{

    return [NDTextFieldPadding createRightAngleRectTextFieldWithFont:font textAlignment:NSTextAlignmentNatural];

}


+ (NDTextFieldPadding *) createRightAngleRectTextFieldWithFont:(UIFont *)font textAlignment:(NSTextAlignment)textAlignment

{

    NDTextFieldPadding *tf = [[NDTextFieldPadding alloc] init];

    tf.textColor = [UIColor blackColor];

    tf.borderStyle = UITextBorderStyleNone;

    tf.clearButtonMode = UITextFieldViewModeWhileEditing;

    tf.textAlignment = textAlignment;

    tf.font = font;

    tf.layer.borderWidth = kSeparatorLineWidth;

    tf.layer.borderColor = kSubColor.CGColor;

    return tf;

}


+ (NDTextFieldPadding *) createRoundTextFieldWithFont:(UIFont *)font

{

    return [NDTextFieldPadding createRoundTextFieldWithFont:font textAlignment:NSTextAlignmentNatural];

}


+ (NDTextFieldPadding *) createRoundTextFieldWithFont:(UIFont *)font textAlignment:(NSTextAlignment)textAlignment;

{

    NDTextFieldPadding *tf = [[NDTextFieldPadding alloc] init];

    tf.backgroundColor = kTextFieldFillColor;

    tf.textColor = kLoginHeavyColor;

    tf.borderStyle = UITextBorderStyleNone;

    tf.clearButtonMode = UITextFieldViewModeWhileEditing;

    tf.textAlignment = textAlignment;

    tf.font = font;

    tf.layer.masksToBounds = YES;

    tf.layer.borderWidth = kSeparatorLineWidth;

    tf.layer.borderColor = kLoginColor.CGColor;

    return tf;

}


#pragma mark - 实例方法

- (void)setPadding:(BOOL)enable top:(float)top right:(float)right bottom:(float)bottom left:(float)left {

    isEnablePadding = enable;

    paddingTop = top;

    paddingRight = right;

    paddingBottom = bottom;

    paddingLeft = left;

}


#pragma mark - 重写

- (CGRect)textRectForBounds:(CGRect)bounds {

    if (isEnablePadding) {

        return CGRectMake(bounds.origin.x + paddingLeft,

                          bounds.origin.y + paddingTop,

                          bounds.size.width - paddingRight, bounds.size.height - paddingBottom);

    } else {

        return CGRectMake(bounds.origin.x, bounds.origin.y, bounds.size.width, bounds.size.height);

    }

}


- (CGRect)editingRectForBounds:(CGRect)bounds {

    return [self textRectForBounds:bounds];

}


@end




阅读更多
个人分类: IOS-项目汇总
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

自定义textFeild

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭