日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

[转]设置 UILabel 和 UITextField 的 Padding 或 Insets

發(fā)布時(shí)間:2025/3/19 编程问答 60 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [转]设置 UILabel 和 UITextField 的 Padding 或 Insets 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>

iOS?的控件,只看到?UIButton?可以設(shè)置 Padding/Insets,即按鈕上文字或圖片與按鈕邊界的間隙,對(duì)與?CSS?來說叫做 Padding,在?iOS?中叫做?Insets,UIButton?設(shè)置?Insets?相應(yīng)的屬性如下:

Configuring Edge Insets

????? contentEdgeInsets? property
????? titleEdgeInsets? property
????? imageEdgeInsets? property?

它們接受的屬性類型是:UIEdgeInsets,由函數(shù) UIEdgeInsetsMake ( CGFloat top, CGFloat left, CGFloat bottom, CGFloat right );???? 構(gòu)造出,分別表示其中的內(nèi)容/標(biāo)題/圖片離各邊的距離。

在?xib?中也有界面來對(duì)按鈕的這三個(gè)?EdgeInsets?屬性的設(shè)置,分別是按鈕的?Edge?和?Inset?屬性。

印像中,Swing?的許多組件都可設(shè)置?Insets?屬性,可對(duì)于?iOS?的控件就沒那么幸運(yùn)了,比如我想設(shè)置?UILable?或?UITextField?中的文本離邊界的間隙,無倫是在?xib?里還是直接代碼的方式都無能為力,因?yàn)樗鼈兏鶕?jù)未開放相應(yīng)的屬性讓你去控制。

辦法當(dāng)然還是有的,自定義相應(yīng)自己的控件了,比如 InsetsLabel?或是? InsetsTextField,接著就是覆蓋某些個(gè)方法來達(dá)成。

首先來看?UILabel?的子類 InsetsLabel?的實(shí)現(xiàn)代碼:

//1.header file #import <UIKit/UIKit.h>@interface InsetsLabel : UILabel @property(nonatomic) UIEdgeInsets insets; -(id) initWithFrame:(CGRect)frame andInsets: (UIEdgeInsets) insets; -(id) initWithInsets: (UIEdgeInsets) insets; @end//2. implementation file #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)]; }


關(guān)鍵就是覆蓋了 -(void) drawTextInRect: (CGRect) rect;?方法,在畫??Label?的文本時(shí)分別設(shè)置文本與??Label?四個(gè)邊的間隙,即畫在?Label?內(nèi)的一個(gè)小矩形內(nèi),這個(gè)例子提供了便利的構(gòu)造函數(shù),提供自己的 UIEdgeInsets?屬性。另外,函數(shù)?UIEdgeInsetsInsetRect(CGRect, UIEdgeInsets)?應(yīng)該是好理解的。

再看如何設(shè)置?UITextField?中文本到四邊的間距,這里也可以定義自己的?InsetsTextField:

// // Created by Unmi on 11/2/11. // Copyright (c) 2011 http://unmi.cc. All rights reserved. //#import <UIKit/UIKit.h>@interface InsetsTextField : UITextField @end@implementation InsetsTextField //控制 placeHolder 的位置,左右縮 20 - (CGRect)textRectForBounds:(CGRect)bounds {return CGRectInset( bounds , 20 , 0 ); // CGRect inset = CGRectMake(bounds.origin.x + 10, bounds.origin.y, bounds.size.width - 10, bounds.size.height); //更好理解些 // return inset; }// 控制文本的位置,左右縮 20 - (CGRect)editingRectForBounds:(CGRect)bounds {return CGRectInset( bounds , 20 , 0 ); //CGRect inset = CGRectMake(bounds.origin.x + 10, bounds.origin.y, bounds.size.width - 10, bounds.size.height); // return inset; } @end//----------------------------------------------------------------- //下面是使用 InsetsTextField 的代碼,可放在 viewDidLoad 等代理方法中 InsetsTextField *insetTextField = [[InsetsTextField alloc]initWithFrame:CGRectMake(10, 10, 180, 25)];//須手動(dòng)設(shè)置它的 borderStyle, 不然看不到邊框的 insetsTextField.borderStyle = UITextBorderStyleRoundedRect; [self.view addSubview:insetsTextField]; [insetsTextField release];


效果如下:

上面更像是借鑒的?InsetsLabel?的實(shí)現(xiàn),其實(shí)對(duì)于 UITextField?還有更好的實(shí)現(xiàn)辦法,而且更簡(jiǎn)單,因?yàn)?UITextFiled?原來就支持的做法。比如它可以讓你做出在文本框最前方固定一個(gè) $ 符號(hào),表示這個(gè)文本框是輸入錢的,第一個(gè)$ 是不能被刪除的。確實(shí),你可以在 TextField?上貼個(gè)?Label,然后文本框的光標(biāo)后移,稍顯麻煩了。

而 UITextField?可以直接設(shè)置 leftView?或 rightView,?然后文本輸入?yún)^(qū)域就在 leftView?和 rightView?之間了,看例子:

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?也是一樣的設(shè)置方式,其中的? Mode?有四種,看到名字應(yīng)該不難理解:

??? UITextFieldViewModeNever,
??? UITextFieldViewModeWhileEditing,
??? UITextFieldViewModeUnlessEditing,
??? UITextFieldViewModeAlways

它的效果呢就更酷了:

文本框的起始光標(biāo)是從上圖數(shù)字 1?位置開始的。

實(shí)際應(yīng)用中,對(duì)于?UITextField?如果有類似的需求,我會(huì)毫不猶豫的使用 leftView/rightView?屬性來設(shè)置。

參考:1.?http://stackoverflow.com/questions/2694411/text-inset-for-uitextfield
??????????? 2.?http://stackoverflow.com/questions/5674655/uitextfield-align-left-margin

轉(zhuǎn)載于:https://my.oschina.net/leeming/blog/51818

總結(jié)

以上是生活随笔為你收集整理的[转]设置 UILabel 和 UITextField 的 Padding 或 Insets的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。