(转)设置 UILabel 和 UITextField 的 Padding 或 Insets (理解UIEdgeInsets)
2012-06-15 18:52
411 查看
转自http://unmi.cc/uilable-uitextfield-padding-insets 主要是理解下UIEdgeInsets在IOS UI里的意义.
靠,这货其实就是间隔,起个名字这么让人费解!!!正值表示间隔值,负值表示超出参照物的距离。
--------------------------------------------------------分割线,下面是转载原文---------------------------------------------------
iOS 的控件,只看到 UIButton 可以设置 Padding/Insets,即按钮上文字或图片与按钮边界的间隙,对与 CSS 来说叫做 Padding,在 iOS 中叫做 Insets,UIButton 设置 Insets 相应的属性如下:
Configuring Edge Insets
contentEdgeInsets property
titleEdgeInsets property
imageEdgeInsets property
它们接受的属性类型是:UIEdgeInsets,由函数 UIEdgeInsetsMake ( CGFloat top, CGFloat left, CGFloat bottom, CGFloat right ); 构造出,分别表示其中的内容/标题/图片离各边的距离。
在 xib 中也有界面来对按钮的这三个 EdgeInsets 属性的设置,分别是按钮的 Edge 和 Inset 属性。
印像中,Swing 的许多组件都可设置 Insets 属性,可对于 iOS 的控件就没那么幸运了,比如我想设置 UILable 或 UITextField 中的文本离边界的间隙,无伦是在 xib 里还是直接代码的方式都无能为力,因为它们根据未开放相应的属性让你去控制。
办法当然还是有的,自定义相应自己的控件了,比如 InsetsLabel 或是 InsetsTextField,接着就是覆盖某些个方法来达成。
首先来看 UILabel 的子类 InsetsLabel 的实现代码:
关键就是覆盖了 -(void) drawTextInRect: (CGRect) rect; 方法,在画 Label 的文本时分别设置文本与 Label 四个边的间隙,即画在 Label 内的一个小矩形内,这个例子提供了便利的构造函数,提供自己的 UIEdgeInsets 属性。另外,函数 UIEdgeInsetsInsetRect(CGRect, UIEdgeInsets) 应该是好理解的。
再看如何设置 UITextField 中文本到四边的间距,这里也可以定义自己的 InsetsTextField:
效果如下:
![](http://pic002.cnblogs.com/images/2012/107596/2012061518495093.png)
上面更像是借鉴的 InsetsLabel 的实现,其实对于 UITextField 还有更好的实现办法,而且更简单,因为 UITextFiled 原来就支持的做法。比如它可以让你做出在文本框最前方固定一个 $ 符号,表示这个文本框是输入钱的,第一个$ 是不能被删除的。确实,你可以在 TextField 上贴个 Label,然后文本框的光标后移,稍显麻烦了。
而 UITextField 可以直接设置 leftView 或 rightView, 然后文本输入区域就在 leftView 和 rightView 之间了,看例子:
rightView 也是一样的设置方式,其中的 Mode 有四种,看到名字应该不难理解:
UITextFieldViewModeNever,
UITextFieldViewModeWhileEditing,
UITextFieldViewModeUnlessEditing,
UITextFieldViewModeAlways
它的效果呢就更酷了:
![](http://pic002.cnblogs.com/images/2012/107596/2012061518503242.png)
文本框的起始光标是从上图数字 1 位置开始的。
实际应用中,对于 UITextField 如果有类似的需求,我会毫不犹豫的使用 leftView/rightView 属性来设置。
参考:1. http://stackoverflow.com/questions/2694411/text-inset-for-uitextfield
2. http://stackoverflow.com/questions/5674655/uitextfield-align-left-margin
本文链接 http://unmi.cc/uilable-uitextfield-padding-insets, 来自 隔叶黄莺 Unmi Blog
靠,这货其实就是间隔,起个名字这么让人费解!!!正值表示间隔值,负值表示超出参照物的距离。
--------------------------------------------------------分割线,下面是转载原文---------------------------------------------------
iOS 的控件,只看到 UIButton 可以设置 Padding/Insets,即按钮上文字或图片与按钮边界的间隙,对与 CSS 来说叫做 Padding,在 iOS 中叫做 Insets,UIButton 设置 Insets 相应的属性如下:
Configuring Edge Insets
contentEdgeInsets property
titleEdgeInsets property
imageEdgeInsets property
它们接受的属性类型是:UIEdgeInsets,由函数 UIEdgeInsetsMake ( CGFloat top, CGFloat left, CGFloat bottom, CGFloat right ); 构造出,分别表示其中的内容/标题/图片离各边的距离。
在 xib 中也有界面来对按钮的这三个 EdgeInsets 属性的设置,分别是按钮的 Edge 和 Inset 属性。
印像中,Swing 的许多组件都可设置 Insets 属性,可对于 iOS 的控件就没那么幸运了,比如我想设置 UILable 或 UITextField 中的文本离边界的间隙,无伦是在 xib 里还是直接代码的方式都无能为力,因为它们根据未开放相应的属性让你去控制。
办法当然还是有的,自定义相应自己的控件了,比如 InsetsLabel 或是 InsetsTextField,接着就是覆盖某些个方法来达成。
首先来看 UILabel 的子类 InsetsLabel 的实现代码:
再看如何设置 UITextField 中文本到四边的间距,这里也可以定义自己的 InsetsTextField:
![](http://pic002.cnblogs.com/images/2012/107596/2012061518495093.png)
上面更像是借鉴的 InsetsLabel 的实现,其实对于 UITextField 还有更好的实现办法,而且更简单,因为 UITextFiled 原来就支持的做法。比如它可以让你做出在文本框最前方固定一个 $ 符号,表示这个文本框是输入钱的,第一个$ 是不能被删除的。确实,你可以在 TextField 上贴个 Label,然后文本框的光标后移,稍显麻烦了。
而 UITextField 可以直接设置 leftView 或 rightView, 然后文本输入区域就在 leftView 和 rightView 之间了,看例子:
UITextFieldViewModeNever,
UITextFieldViewModeWhileEditing,
UITextFieldViewModeUnlessEditing,
UITextFieldViewModeAlways
它的效果呢就更酷了:
![](http://pic002.cnblogs.com/images/2012/107596/2012061518503242.png)
文本框的起始光标是从上图数字 1 位置开始的。
实际应用中,对于 UITextField 如果有类似的需求,我会毫不犹豫的使用 leftView/rightView 属性来设置。
参考:1. http://stackoverflow.com/questions/2694411/text-inset-for-uitextfield
2. http://stackoverflow.com/questions/5674655/uitextfield-align-left-margin
本文链接 http://unmi.cc/uilable-uitextfield-padding-insets, 来自 隔叶黄莺 Unmi Blog
相关文章推荐
- 设置 UILabel 和 UITextField 的 Padding 或 Insets (理解UIEdgeInsets)
- 设置 UILabel 和 UITextField 的 Padding 或 Insets (理解UIEdgeInsets)
- (转)设置 UILabel 和 UITextField 的 Padding 或 Insets (理解UIEdgeInsets)
- 设置 UILabel 和 UITextField 的 Padding 或 Insets (理解UIEdgeInsets)
- 设置 UILabel 和 UITextField 的 Padding 或 Insets (理解UIEdgeInsets)
- 设置 UILabel 和 UITextField 的 Padding 或 Insets (理解UIEdgeInsets)
- 设置 UILabel 和 UITextField 的 Padding 或 Insets (理解UIEdgeInsets)
- 设置UIlabel偏移,UITextField偏移 UIEdgeInsets 说明 设置 UILabel 和 UITextField 的 Padding 或 Insets
- 设置 UILabel 和 UITextField 的 Padding 或 Insets(内容与边距)
- 设置 UILabel 和 UITextField 的 Padding 或 Insets
- 设置 UILabel 和 UITextField 的 Padding 或 Insets
- 设置 UILabel 和 UITextField 的 Padding 或 Insets
- UILabel 内容的UIEdgeInsets 和 UITextField的 leftView/rightView
- iOS - UIButton(UIEdgeInsets)/设置button上的文字和图片上下垂直居中对齐
- iOS - UIButton(UIEdgeInsets)/设置button上的文字和图片上下垂直居中对齐
- ios8 UITableView设置 setSeparatorInset:UIEdgeInsetsZero不起作用的解决办法
- 问题:iOS8设置tableview的setSeparatorInset:UIEdgeInsetsZero不起作用
- iOS - UIButton(UIEdgeInsets)/设置button上的文字和图片上下垂直居中对齐
- ios8 UITableView设置 setSeparatorInset UIEdgeInsetsZero不起作用的解决办法(去掉15px空白间距)
- ios8 UITableView设置 setSeparatorInset:UIEdgeInsetsZero不起作用的解决办法