UILabel 内容的UIEdgeInsets 和 UITextField的 leftView/rightView

自定义UILabel和UITextField的内边距
本文介绍了如何在iOS中为UILabel和UITextField设置类似UIButton的Padding/Insets效果。由于系统默认不提供此功能,作者通过自定义InsetsLabel和InsetsTextField子类,并覆盖特定方法来实现内容与边界的间距调整。同时提到了UITextField的leftView/rightView属性及UITextFieldViewMode的四种模式。

深度参考了 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
};





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值