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
|
#import
<UIKit/UIKit.h>
@interface
InsetsLabel : UILabel
@property ( nonatomic )
UIEdgeInsets
insets;
-( id )
initWithFrame:(CGRect)frame andInsets: ( UIEdgeInsets )
insets;
-( id )
initWithInsets: ( UIEdgeInsets )
insets;
@end
#import
"InsetsLabel.h"
@implementation
InsetsLabel
@synthesize
insets=_insets;
-( id )
initWithFrame:(CGRect)frame andInsets:( UIEdgeInsets )insets
{
self
= [ super
initWithFrame:frame];
if ( self ){
self .insets
= insets;
}
return
self ;
}
-( id )
initWithInsets:( UIEdgeInsets )insets
{
self
= [ super
init];
if ( self ){
self .insets
= insets;
}
return
self ;
}
-( void )
drawTextInRect:(CGRect)rect {
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
<UIKit/UIKit.h>
@interface
InsetsTextField : UITextField
@end
@implementation
InsetsTextField
-
(CGRect)textRectForBounds:(CGRect)bounds {
return
CGRectInset( bounds , 20 , 0 );
}
-
(CGRect)editingRectForBounds:(CGRect)bounds {
return
CGRectInset( bounds , 20 , 0 );
}
@end
InsetsTextField
*insetTextField = [[InsetsTextField alloc]
initWithFrame:CGRectMake(10,
10, 180, 25)];
insetsTextField.borderStyle
= UITextBorderStyleRoundedRect ;
[ self .view
addSubview:insetsTextField];
[insetsTextField
release];
|
效果如下:
上面更像是借鉴的InsetsLabel的实现,其实对于 UITextField还有更好的实现办法,而且更简单,因为UITextFiled原来就支持的做法。比如它可以让你做出在文本框最前方固定一个 $ 符号,表示这个文本框是输入钱的,第一个$ 是不能被删除的。确实,你可以在 TextField上贴个Label,然后文本框的光标后移,稍显麻烦了。
而 UITextField可以直接设置 leftView或 rightView,然后文本输入区域就在 leftView和 rightView之间了,看例子:
1
2
3
4
5
6
|
UILabel
*paddingView = [[ UILabel
alloc] initWithFrame:CGRectMake(0, 0, 10, 25)];
paddingView.text
= @ "$" ;
paddingView.textColor
= [ UIColor
darkGrayColor];
paddingView.backgroundColor
= [ UIColor
clearColor];
textfield.leftView
= paddingView;
textfield.leftViewMode
= UITextFieldViewModeAlways ;
|
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
分享到:
相关推荐
UILabel 和UITextField 的常用api 介绍和使用
该文档详细列出UITextField常用以及不常用的相关属性及如何进行设置,对大家掌握UITextField的用法很有帮助。
注册或登录界⾯ UILabel .UITextField
UILabel的text设置不同颜色、字体
UIButton,UILabel,UITextField做的一个仿造QQ登陆界面的demo
代码中是JSPatch学习笔记的UI基础部分,其中包括以下内容: 1. 项目环境配置 2. 变量 * 局部变量 * 全局变量 ... * UITextField * 文本框内容改变事件监听 * UIAlertView * 弹框 * 监听按钮点击事件
UILabel、UILabel基本用法、UILabel详解,UILabel常用方法
1) 实用型应用模板 2) UIView、UILabel、UITextField、UIButton和UITextView 等基本控件使用 3) iOS位置服务 4) Web Google地图API 5) 本地Google地图API
设置UILabel局部点击 根据range来设置label上的字体点击位置 得到相应点击事件
UILabel 继承于 UIView 里的各种属性和方法使用
在IOS开发中,由于服务器返回的字符串的长度是不固定的,所以不能给UILabel设给固定的大小,要动态的设置UILabel的大小。
下面小编就为大家带来一篇iOS 设置UILabel的行间距并自适应高度的方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
TextKit 是iOS7新推出的类库,在CoreText 的基础上封装的,主要用来做图文...TextKit 没有新增加类,他就是在原来有的文本显示控件上加以封装,所以我们经常使用的UILabel、UITextField、UITextView在这里都可以使用
UIlabel根据文本、字体获得label宽度;自适应宽高
ios uilabel的代码 ios uilabel的代码 ios uilabel的代码 ios uilabel的代码 ios uilabel的代码 ios uilabel的代码
UILabel-手写使用手写动画设置 UILabel 文本- ( void )clearAllWritingText;- ( void )setHandwritingText:( NSString *)text color:(UIColor*)textColor animatedWithDuration:( NSTimeInterval )duration ...
UIButton,UILabel文字旋转(倾斜),UIButton,UILabel文字旋转(倾斜)
在UILabel左边或右边添加一个图标(Objective-C).zip,uilabel,可以在左侧或右侧放置小图标。