定制textField

 

2014-08-05 11:00 447人阅读 评论(0) 收藏 举报

 分类:
  1. //  
  2. //  HYBTextField.h  
  3. //  CloudShopping  
  4. //  
  5. //  Created by sixiaobo on 14-7-10.  
  6. //  Copyright (c) 2014年 com.Uni2uni. All rights reserved.  
  7. //  
  8.   
  9. #import <UIKit/UIKit.h>  
  10.   
  11. /*! 
  12.  * @brief 自定义TextField,用于修改默认textfield的属性为我们工程中需要的属性 
  13.  * @author huangyibiao 
  14.  */  
  15. @interface HYBTextField : UITextField  
  16.   
  17. @property (nonatomic, strong) UIColor *placeholderColor;  
  18. @property (nonatomic, strong) UIFont  *placeholderFont;  
  19. @property (nonatomic, assign) CGFloat leftPadding;  
  20.   
  21. // 默认leftPadding = 8.0  
  22. - (id)initWithFrame:(CGRect)frame placeholderColor:(UIColor *)color font:(UIFont *)font;  
  23. - (id)initWithFrame:(CGRect)frame placeholderColor:(UIColor *)color font:(UIFont *)font leftPadding:(CGFloat)leftPadding;  
  24.   
  25. @end  
  1. //  
  2. //  HYBTextField.m  
  3. //  CloudShopping  
  4. //  
  5. //  Created by sixiaobo on 14-7-10.  
  6. //  Copyright (c) 2014年 com.Uni2uni. All rights reserved.  
  7. //  
  8.   
  9. #import "HYBTextField.h"  
  10.   
  11. @implementation HYBTextField  
  12.   
  13. - (id)initWithFrame:(CGRect)frame placeholderColor:(UIColor *)color font:(UIFont *)font {  
  14.     return [self initWithFrame:frame placeholderColor:color font:font leftPadding:8];  
  15. }  
  16.   
  17. - (id)initWithFrame:(CGRect)frame  
  18.    placeholderColor:(UIColor *)color  
  19.                font:(UIFont *)font  
  20.         leftPadding:(CGFloat)leftPadding {  
  21.     if (self = [super initWithFrame:frame]) {  
  22.         self.placeholderColor = color;  
  23.         self.placeholderFont = font;  
  24.         self.leftPadding = leftPadding;  
  25.           
  26.         self.autocapitalizationType = UITextAutocapitalizationTypeNone;  
  27.         self.autocorrectionType = UITextAutocorrectionTypeNo;  
  28.         self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;  
  29.         self.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;  
  30.         self.borderStyle = UITextBorderStyleNone;  
  31.         self.backgroundColor = [UIColor whiteColor];  
  32.     }  
  33.     return self;  
  34. }  
  35.   
  36. - (void)drawPlaceholderInRect:(CGRect)rect {  
  37.     [kColorWith16RGB(0xa8a8a8) setFill];  
  38.     [[self placeholder] drawInRect:CGRectMake(self.leftPadding, rect.origin.y, rect.size.width, rect.size.height)  
  39.                           withFont:self.placeholderFont];  
  40.     return;  
  41. }  
  42.   
  43. // 控制编辑文本的位置  
  44. - (CGRect)editingRectForBounds:(CGRect)bounds {  
  45.     CGFloat padding = self.leftPadding;  
  46.     if (self.textAlignment == NSTextAlignmentRight) {  
  47.         padding = 0;  
  48.     }  
  49.     CGRect inset = CGRectMake(bounds.origin.x + padding, bounds.origin.y,  
  50.                               bounds.size.width, bounds.size.height);  
  51.     return inset;  
  52. }  
  53.   
  54. - (CGRect)placeholderRectForBounds:(CGRect)bounds {  
  55.     NSString *obtainSizeString = self.text;  
  56.     CGSize size = [obtainSizeString sizeWithFont:self.placeholderFont];  
  57.     return CGRectMake(bounds.origin.x, (bounds.size.height - size.height) / 2,  
  58.                       bounds.size.width, bounds.size.height);  
  59. }  
  60.   
  61. // 控制显示文本的位置  
  62. - (CGRect)textRectForBounds:(CGRect)bounds {  
  63.     CGFloat padding = self.leftPadding;  
  64.     if (self.textAlignment == NSTextAlignmentRight) {  
  65.         padding = 0;  
  66.     }  
  67.     CGRect inset = CGRectMake(bounds.origin.x + padding, bounds.origin.y,  
  68.                               bounds.size.width, bounds.size.height);  
  69.       
  70.     return inset;  
  71. }  
  72.   
  73. @end  
原文地址:https://www.cnblogs.com/iOS-mt/p/5824937.html