您的位置:首页 > 产品设计 > UI/UE

iOS UITextField 设置内边距

2015-06-19 16:39 513 查看
http://blog.csdn.net/erica_sadun/article/details/9088181
1.inherit UITextField Class.

 

[cpp] view plaincopy

.h  

//  

//  TextField.h  

//  TTShow  

//  

//  Created by twb on 13-9-10.  

//  Copyright (c) 2013年 twb. All rights reserved.  

//  

  

#import <UIKit/UIKit.h>  

  

@interface TextField : UITextField  

  

@property (nonatomic, assign) CGFloat dx;  

@property (nonatomic, assign) CGFloat dy;  

  

@end  

 

 

[cpp] view plaincopy

.m  

//  

//  TextField.m  

//  TTShow  

//  

//  Created by twb on 13-9-10.  

//  Copyright (c) 2013年 twb. All rights reserved.  

//  

  

#import "TextField.h"  

  

#define kTextFieldPaddingWidth  (10.0f)  

#define kTextFieldPaddingHeight (10.0f)  

  

@implementation TextField  

  

- (id)initWithFrame:(CGRect)frame  

{  

    self = [super initWithFrame:frame];  

    if (self) {  

        // Initialization code  

    }  

    return self;  

}  

  

- (CGRect)textRectForBounds:(CGRect)bounds  

{  

    return CGRectInset(bounds,  

                       self.dx == 0.0f ? kTextFieldPaddingWidth : self.dx,  

                       self.dy == 0.0f ? kTextFieldPaddingHeight : self.dy);  

}  

  

- (CGRect)editingRectForBounds:(CGRect)bounds  

{  

    return CGRectInset(bounds,  

                       self.dx == 0.0f ? kTextFieldPaddingWidth : self.dx,  

                       self.dy == 0.0f ? kTextFieldPaddingHeight : self.dy);  

}  

  

- (void)setDx:(CGFloat)dx  

{  

    _dx = dx;  

    [self setNeedsDisplay];  

}  

  

- (void)setDy:(CGFloat)dy  

{  

    _dy = dy;  

    [self setNeedsDisplay];  

}  

  

/* 

// Only override drawRect: if you perform custom drawing. 

// An empty implementation adversely affects performance during animation. 

- (void)drawRect:(CGRect)rect 



    // Drawing code 



*/  

  

@end  

 

2.APPLE private method.

[self.yourTextField setValue:[NSNumber numberWithInt:5] forKey:@"_paddingTop"];

[self.yourTextField setValue:[NSNumber numberWithInt:5] forKey:@"_paddingLeft"];

[self.yourTextField setValue:[NSNumber numberWithInt:5] forKey:@"_paddingBottom"];

[self.yourTextField setValue:[NSNumber numberWithInt:5] forKey:@"_paddingRight"];

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: