关闭

自定义textFeild

158人阅读 评论(0) 收藏 举报
分类:

#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




0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:26501次
    • 积分:933
    • 等级:
    • 排名:千里之外
    • 原创:62篇
    • 转载:53篇
    • 译文:0篇
    • 评论:0条