定制textField

时间:2023-03-09 14:32:43
定制textField

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

定制textField 分类:
IOS开发笔记(248) 定制textField

版权声明:本****博客所有文章不会即时更新,请关注个人博客:http://www.huangyibiao.com/

  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. #import <UIKit/UIKit.h>
  9. /*!
  10. * @brief 自定义TextField,用于修改默认textfield的属性为我们工程中需要的属性
  11. * @author huangyibiao
  12. */
  13. @interface HYBTextField : UITextField
  14. @property (nonatomic, strong) UIColor *placeholderColor;
  15. @property (nonatomic, strong) UIFont  *placeholderFont;
  16. @property (nonatomic, assign) CGFloat leftPadding;
  17. // 默认leftPadding = 8.0
  18. - (id)initWithFrame:(CGRect)frame placeholderColor:(UIColor *)color font:(UIFont *)font;
  19. - (id)initWithFrame:(CGRect)frame placeholderColor:(UIColor *)color font:(UIFont *)font leftPadding:(CGFloat)leftPadding;
  20. @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. #import "HYBTextField.h"
  9. @implementation HYBTextField
  10. - (id)initWithFrame:(CGRect)frame placeholderColor:(UIColor *)color font:(UIFont *)font {
  11. return [self initWithFrame:frame placeholderColor:color font:font leftPadding:8];
  12. }
  13. - (id)initWithFrame:(CGRect)frame
  14. placeholderColor:(UIColor *)color
  15. font:(UIFont *)font
  16. leftPadding:(CGFloat)leftPadding {
  17. if (self = [super initWithFrame:frame]) {
  18. self.placeholderColor = color;
  19. self.placeholderFont = font;
  20. self.leftPadding = leftPadding;
  21. self.autocapitalizationType = UITextAutocapitalizationTypeNone;
  22. self.autocorrectionType = UITextAutocorrectionTypeNo;
  23. self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
  24. self.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
  25. self.borderStyle = UITextBorderStyleNone;
  26. self.backgroundColor = [UIColor whiteColor];
  27. }
  28. return self;
  29. }
  30. - (void)drawPlaceholderInRect:(CGRect)rect {
  31. [kColorWith16RGB(0xa8a8a8) setFill];
  32. [[self placeholder] drawInRect:CGRectMake(self.leftPadding, rect.origin.y, rect.size.width, rect.size.height)
  33. withFont:self.placeholderFont];
  34. return;
  35. }
  36. // 控制编辑文本的位置
  37. - (CGRect)editingRectForBounds:(CGRect)bounds {
  38. CGFloat padding = self.leftPadding;
  39. if (self.textAlignment == NSTextAlignmentRight) {
  40. padding = 0;
  41. }
  42. CGRect inset = CGRectMake(bounds.origin.x + padding, bounds.origin.y,
  43. bounds.size.width, bounds.size.height);
  44. return inset;
  45. }
  46. - (CGRect)placeholderRectForBounds:(CGRect)bounds {
  47. NSString *obtainSizeString = self.text;
  48. CGSize size = [obtainSizeString sizeWithFont:self.placeholderFont];
  49. return CGRectMake(bounds.origin.x, (bounds.size.height - size.height) / 2,
  50. bounds.size.width, bounds.size.height);
  51. }
  52. // 控制显示文本的位置
  53. - (CGRect)textRectForBounds:(CGRect)bounds {
  54. CGFloat padding = self.leftPadding;
  55. if (self.textAlignment == NSTextAlignmentRight) {
  56. padding = 0;
  57. }
  58. CGRect inset = CGRectMake(bounds.origin.x + padding, bounds.origin.y,
  59. bounds.size.width, bounds.size.height);
  60. return inset;
  61. }
  62. @end