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

 分类:
IOS开发笔记(248) 

版权声明:本CSDN博客所有文章不会即时更新,请关注个人博客: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

定制textField的更多相关文章

  1. iOS TextField用法大全

    //初始化textfield并设置位置及大小 UITextField *text = [[UITextField alloc]initWithFrame:CGRectMake(20, 20, 130, ...

  2. IOS开发 - TextField 控件详细

    //初始化textfield并设置位置及大小 UITextField *text = [[UITextField alloc]initWithFrame:CGRectMake(20, 20, 130, ...

  3. Inspector a ProgressBar(定制属性面板)

    一.定制进度条 这篇文章主要学习如何在Unity的Inspector中使用ProgressBar 普通属性面板预览 通常我们的属性面板如下 定制属性面板预览 而通过扩展成ProcessBar后 二.内 ...

  4. Django admin的一些有用定制

    Model实例,myapp/models.py: from django.db import models class Blog(models.Model): name = models.CharFi ...

  5. IOS TextField设置大全

    //初始化textfield并设置位置及大小  //设置边框样式,只有设置了才会显示边框样式     text.borderStyle = UITextBorderStyleRoundedRect; ...

  6. 定制ckeditor的菜单

    修改配置文件config.js来定制cheditor的菜单,需要以下步骤: 1.找到ckeditor安装目录的config.js文件 2.记下要使用的功能名,以下的"-"代表分隔符 ...

  7. TextField控件详解2

    //初始化textfield并设置位置及大小 UITextField *text = [[UITextField alloc]initWithFrame:CGRectMake(20, 20, 130, ...

  8. 转发:iOS之textfield用法大全

    转发至:http://m.blog.csdn.net/article/details?id=8121915 //初始化textfield并设置位置及大小 UITextField *text = [[U ...

  9. UIAlertController中TextField的用法

    这时候可以添加如下代码: [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) { // 可以 ...

随机推荐

  1. 调试postgresql9.5.2最新源码

    最近在考量数据库的选型,考虑后期把数据切换到postgresql ,postgresql源码用c实现,代码很精炼完美,值得学习下 首先去pgsql官网下载最新的源码 ,然后还需要perl,bison ...

  2. XMind十大最有用的功能

    XMind十大最有用的功能 XMind是一款顶级商业品质的思维导图软件和头脑风暴软件,在企业和教育领域都有很广泛的应用,XMind功能全面,易上手,在此小编给大家整理出了XMind十大最有用的功能以供 ...

  3. Jupyter Notebook PDF输出的中文支持

    Jupyter Notebook是什么 Jupyter Notebook是ipython Notebook 的升级.Jupyter能够将实时代码,公式,可视化图表以Cell的方式组织在一起,形成一个对 ...

  4. JS获取和设置光标的位置

    <html> <head> <script language="javascript"> function getCursortPosition ...

  5. 2014-08-05 pig

    Pig的数据类型能够分为两种:一种是scalar类型,包含单一的value,一种是complex类型,包含有其他的类型. 对于scalar类型: int,long,float,double,chara ...

  6. Windows下Memcached在.Net程序中的实际运用(从Memcached客户端Enyim的库的编译到实际项目运用)

    1.一点基础概念 2.获取EnyimMemcached客户端的源代码并编译出动态库 3.Memcached的服务器安装(windows server) 4.在web项目中实战 一.基础概念 memca ...

  7. j2ee学习笔记 Filter过滤器

    作用:过滤response和request对象的内容 使用: Filter是J2EE的一个接口,自定义Filter需要实现该接口,并重写所有的方法. Filter提供的方法: init() doFil ...

  8. my_pnd_start

    终于有个雏形了,再给自己一些时间做做看吧.

  9. 程序员带你十天快速入门Python,玩转电脑软件开发(四)

    本系列文章立志于从一个已经习得一门编程语言的基础之上,全面介绍Python的相关开发过程和相关经验总结.本篇文章主要是基于上一篇的程序员带你十天快速入门Python,玩转电脑软件开发(三)的基础之上, ...

  10. CSS画三角形引发的一些思考

      今天刷知乎时看到了一个问题,有谁能详细讲一下css如何画出一个三角形?怎么想都想不懂? - 知乎.很巧,刚入前端坑的我前不久也遇到过这个问题,今天再来谈一谈这个问题则是因为知乎的一些答案引发了我的 ...