iOS UITextField 设置内边距

http://blog.csdn.net/erica_sadun/article/details/9088181
1.inherit UITextField Class.

 

  1. .h  
  2. //  
  3. //  TextField.h  
  4. //  TTShow  
  5. //  
  6. //  Created by twb on 13-9-10.  
  7. //  Copyright (c) 2013年 twb. All rights reserved.  
  8. //  
  9.   
  10. #import <UIKit/UIKit.h>  
  11.   
  12. @interface TextField : UITextField  
  13.   
  14. @property (nonatomic, assign) CGFloat dx;  
  15. @property (nonatomic, assign) CGFloat dy;  
  16.   
  17. @end  


 


 

  1. .m  
  2. //  
  3. //  TextField.m  
  4. //  TTShow  
  5. //  
  6. //  Created by twb on 13-9-10.  
  7. //  Copyright (c) 2013年 twb. All rights reserved.  
  8. //  
  9.   
  10. #import "TextField.h"  
  11.   
  12. #define kTextFieldPaddingWidth  (10.0f)  
  13. #define kTextFieldPaddingHeight (10.0f)  
  14.   
  15. @implementation TextField  
  16.   
  17. - (id)initWithFrame:(CGRect)frame  
  18. {  
  19.     self = [super initWithFrame:frame];  
  20.     if (self) {  
  21.         // Initialization code  
  22.     }  
  23.     return self;  
  24. }  
  25.   
  26. - (CGRect)textRectForBounds:(CGRect)bounds  
  27. {  
  28.     return CGRectInset(bounds,  
  29.                        self.dx == 0.0f ? kTextFieldPaddingWidth : self.dx,  
  30.                        self.dy == 0.0f ? kTextFieldPaddingHeight : self.dy);  
  31. }  
  32.   
  33. - (CGRect)editingRectForBounds:(CGRect)bounds  
  34. {  
  35.     return CGRectInset(bounds,  
  36.                        self.dx == 0.0f ? kTextFieldPaddingWidth : self.dx,  
  37.                        self.dy == 0.0f ? kTextFieldPaddingHeight : self.dy);  
  38. }  
  39.   
  40. - (void)setDx:(CGFloat)dx  
  41. {  
  42.     _dx = dx;  
  43.     [self setNeedsDisplay];  
  44. }  
  45.   
  46. - (void)setDy:(CGFloat)dy  
  47. {  
  48.     _dy = dy;  
  49.     [self setNeedsDisplay];  
  50. }  
  51.   
  52. /* 
  53. // Only override drawRect: if you perform custom drawing. 
  54. // An empty implementation adversely affects performance during animation. 
  55. - (void)drawRect:(CGRect)rect 
  56. { 
  57.     // Drawing code 
  58. } 
  59. */  
  60.   
  61. @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"];

 
posted @ 2015-06-19 16:39  Allen.Ma  阅读(5765)  评论(0编辑  收藏  举报