UILabel and UITextField 's Insets
in IOS ,We can set Insets for UIButton ,it's called Padding in css but called Insets in IOS. So UIButton 's Insets property is:
Configuring Edge Insets
contentEdgeInsets property
titleEdgeInsets property
imageEdgeInsets property
Then U can use UIEdgeInsetsMake( CGFloat top, CGFloat left, CGFloat bottom, CGFloat right ); to set the Insets for UIButton.
But,there is not Insets for UILabel or UITextField ? if we want use Insets,we should define it bySelf.
step 1: Define InsetLabel class and it's superClass is UILabel ,the code like this:
#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)]; }
The importent method is reWrite -(void)drawTextInRect:(CGRect)rect.when you draw label,setting the padding text to bound dispace.
step 2:Define InsetLabel class and it's superClass is UITextField ,the code like this:
#import <UIKit/UIKit.h> @interface InsetsTextField : UITextField @end @implementation InsetsTextField //控制 placeHolder 的位置,左右缩 20 - (CGRect)textRectForBounds:(CGRect)bounds { return CGRectInset( bounds , 20 , 0 ); } // 控制文本的位置,左右缩 20 - (CGRect)editingRectForBounds:(CGRect)bounds { return CGRectInset( bounds , 20 , 0 ); } @end //----------------------------------------------------------------- //下面是使用 InsetsTextField 的代码,可放在 viewDidLoad 等代理方法中 InsetsTextField *insetTextField = [[InsetsTextField alloc] initWithFrame:CGRectMake(10, 10, 180, 25)]; //须手动设置它的 borderStyle, 不然看不到边框的 insetsTextField.borderStyle = UITextBorderStyleRoundedRect; [self.view addSubview:insetsTextField]; [insetsTextField release];
This is UILabel and UITextField 's Insets.you can use it if you need.
in the other,UITextField's leftView and RightView you can use if you want draw image in UITextField's left or right.

浙公网安备 33010602011771号