设置 UILabel 和 UITextField 的 Padding 或 Insets (理解UIEdgeInsets)
转自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 的实现代码:
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
//1.header#import@interface InsetsLabelUILabel@property(nonatomic)UIEdgeInsets insets;-(id)UIEdgeInsets)-(id)UIEdgeInsets)@end//2.#import@implementation InsetsLabel@synthesize insets=_insets;-(id)UIEdgeInsets)insets self =super initWithFrame:frame]; if(self){ self.insets } return self;}-(id)UIEdgeInsets)insets self =super init]; if(self){ self.insets } return self;}-(void) return [super drawTextInRect:UIEdgeInsetsInsetRect(rect,self.insets)];} |
关键就是覆盖了 -(void) drawTextInRect: (CGRect) rect; 方法,在画 Label 的文本时分别设置文本与 Label 四个边的间隙,即画在 Label 内的一个小矩形内,这个例子提供了便利的构造函数,提供自己的 UIEdgeInsets 属性。另外,函数 UIEdgeInsetsInsetRect(CGRect, UIEdgeInsets) 应该是好理解的。
再看如何设置 UITextField 中文本到四边的间距,这里也可以定义自己的 InsetsTextField:
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
//// // //#import@interface InsetsTextFieldUITextField@end@implementation InsetsTextField//控制- return CGRectInset(}//- return CGRectInset(}@end//-----------------------------------------------------------------//下面是使用InsetsTextField initWithFrame:CGRectMake(10,//须手动设置它的insetsTextField.borderStyleUITextBorderStyleRoundedRect;[self.view[insetsTextField |
效果如下:

上面更像是借鉴的 InsetsLabel 的实现,其实对于 UITextField 还有更好的实现办法,而且更简单,因为 UITextFiled 原来就支持的做法。比如它可以让你做出在文本框最前方固定一个 $ 符号,表示这个文本框是输入钱的,第一个$ 是不能被删除的。确实,你可以在 TextField 上贴个 Label,然后文本框的光标后移,稍显麻烦了。
而 UITextField 可以直接设置 leftView 或 rightView, 然后文本输入区域就在 leftView 和 rightView 之间了,看例子:
|
1
2
3
4
5
6
|
UILabel *paddingViewUILabel alloc]paddingView.text"$";paddingView.textColorUIColor darkGrayColor];paddingView.backgroundColorUIColor clearColor];textfield.leftViewtextfield.leftViewModeUITextFieldViewModeAlways; |
rightView 也是一样的设置方式,其中的 Mode 有四种,看到名字应该不难理解:
UITextFieldViewModeNever,
UITextFieldViewModeWhileEditing,
UITextFieldViewModeUnlessEditing,
UITextFieldViewModeAlways
它的效果呢就更酷了:

文本框的起始光标是从上图数字 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)
转自http://unmi.cc/uilable-uitextfield-padding-insets 主要是理解下UIEdgeInsets在IOS UI里的意义.靠,这货其实就是间隔,起个名字这么让 ...
- iOS 设置UILabel 的内边距
iOS 设置UILabel 的内边距 - (void)drawTextInRect:(CGRect)rect { UIEdgeInsets insets = {, , , }; [super draw ...
- UIButton、UILabel、UITextField 初学者需要了解的基本定义和常用设置
以下是三个IOS开发中最常用的控件,作为IOS基础学习教程知识 ,初学者需要了解其基本定义和常用设置,以便在开发在熟练运用. UIButton按钮 第一.UIButton的定义 UIButton * ...
- ##DAY2 UILabel、UITextField、UIButton、UIImageView、UISlider
##DAY2 UILabel.UITextField.UIButton.UIImageView.UISlider #pragma mark ———————UILabel——————————— UILa ...
- 设置UIButton中的文字和图片,设置UILabel的文在显示不同颜色
UIButton: UIEdgeInsets 在UIButton中有三个对EdgeInsets的设置:ContentEdgeInsets.titleEdgeInsets.imageEdgeInsets ...
- iOS 设置UILabel的行间距并自适应高度
NSString *contentStr = @"总以为,在最初的地方,有一个最原来的我,就也会有一个最原来的你"; UILabel *tempLabel = [[UILabel ...
- 关于margin:-10000px;padding:10000px;的理解
原文链接: 内外补丁负值法是指通过内外补丁的设置来解决一些我们通常方法不能实现的效果.例如:可以通过改变盒模型的样式来使几列div由内容撑开高度但几列div与最高的一栏等高的问题.但是为什么会出现这样 ...
- 如何设置UILabel中的字体的间距
cell.teacherDescriptionLabel.text = content; cell.teacherDescriptionLabel.textAlignment = NSTextAlig ...
- 设置UILabel可变高度(根据文本内容自动适应高度)
@property(nonatomic)UILabel *showLabel; // 计算文本所占高度,计算出来之后设置label的高度 // 第一个参数:字体大小,字体大小/样式影响计算字体的高 ...
随机推荐
- U盘安装linix
首先制作u盘启动器. 然后在电脑上使用UltraISO将你的安装的linix系统刻录到u盘上 在放入一份linix系统在u盘更目录 boot页面u盘启动.选择 第二次没有放入系统在u盘根目录出现这个: ...
- python中实现上下文管理器的两种方法
上下文管理器: python中实现了__enter__和__exit__方法的对象就可以称之为上下文管理器 实现方法一举例: def File(object): def __init__(self, ...
- js中的reduce()函数
1. 首先看下语法如下 2 . 写了个demo如下 var fa = [1,2,3,4] function red(a, b) { console.log(arguments); return a + ...
- python 之 多线程、多进程代码
thread-多线程 multiprocessing-多进程 #!/usr/bin/python # -*- coding:utf-8 -*- import os import threading i ...
- BZOJ2186: [Sdoi2008]沙拉公主的困惑(求[1,N!]与M!互素的个数)(线性筛)
2186: [Sdoi2008]沙拉公主的困惑 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 6103 Solved: 2060[Submit][S ...
- BZOJ2124: 等差子序列(树状数组&hash -> bitset 求是否存在长度为3的等差数列)
2124: 等差子序列 Time Limit: 3 Sec Memory Limit: 259 MBSubmit: 2354 Solved: 826[Submit][Status][Discuss ...
- Java其他API介绍
有一些类虽然不像集合.多线程.网络编程中的类那样属于Java中的核心类,但是它们在开发过程中给我们带来很多便利,这里就对它们做下简要的介绍和演示. 一.System类 System类中的构造方法是私有 ...
- linux-一篇文章完成lnmp环境的编译安装
lnmp环境搭建 前置条件 操作系统安装:CentOS 6.8 64位最小化安装. 配置好IP.DNS.网关.主机名 配置防火墙,开启80.3306端口 关闭访问墙 service iptables ...
- linux系统硬件配置
转自:http://blog.163.com/yang_jianli/blog/static/1619900062010391127338/ 一:查看cpu more /proc/cpuinfo | ...
- SQL Server中的事务与其隔离级别之脏读, 未提交读,不可重复读和幻读
原本打算写有关 SSIS Package 中的事务控制过程的,但是发现很多基本的概念还是需要有 SQL Server 事务和事务的隔离级别做基础铺垫.所以花了点时间,把 SQL Server 数据库中 ...