深度参考了 http://unmi.cc/uilable-uitextfield-padding-insets/#more-4060 后 然后自己测试UITextField的时候 又发现了一些东西 纪录下来
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 的实现代码:
#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)];
}
同理 UITextField
@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
最后来说 textField 的 rightView 直接 粘代码了
#import "CustomTextField.h"
@interface CustomTextField ()
@property (nonatomic) CGRect rightCBounds;
@end
@implementation CustomTextField
//需要重写该方法
- (CGRect)rightViewRectForBounds:(CGRect)bounds{
return _rightCBounds;
}
@end
//使用的时候
UILabel *paddingView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 10, 210)];
paddingView.textAlignment = NSTextAlignmentCenter;
paddingView.text = @"元";
paddingView.textColor = [UIColor darkGrayColor];
paddingView.backgroundColor = [UIColor clearColor];
textField = [[CustomTextField alloc]initWithFrame:CGRectMake(20, 50, 300, 60)];
textField.placeholder = @"请输入金额";
textField.textAlignment = NSTextAlignmentRight;
textField.rightView = paddingView;
textField.rightViewMode = UITextFieldViewModeUnlessEditing;
textField.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:textField];
textField.rightCBounds =CGRectMake(270, 0, 30, 60);
UITextFieldViewMode 这个属性 有4种 大家可以尝试一下
typedef NS_ENUM(NSInteger, UITextFieldViewMode) {
UITextFieldViewModeNever,
UITextFieldViewModeWhileEditing,
UITextFieldViewModeUnlessEditing,
UITextFieldViewModeAlways
};
自定义UILabel和UITextField的内边距
本文介绍了如何在iOS中为UILabel和UITextField设置类似UIButton的Padding/Insets效果。由于系统默认不提供此功能,作者通过自定义InsetsLabel和InsetsTextField子类,并覆盖特定方法来实现内容与边界的间距调整。同时提到了UITextField的leftView/rightView属性及UITextFieldViewMode的四种模式。
2131

被折叠的 条评论
为什么被折叠?



