UITextField设置leftView的Insets

Insets就是css中的padding

我们给UITextField设置了leftView,目的是在文本输入框左侧显示一个图标。但是在ios7里,这个图标会紧紧地挨着TextField的左边框,很不美观,所以就希望设置一个Insets。但是直接设置ImageView的bounds不行,需要用下面这个方法:

@interface YLSTextField : UITextField

-(id)initWithFrame:(CGRect)frame Icon:(UIImageView*)icon;

@end
@implementation YLSTextField

-(id)initWithFrame:(CGRect)frame Icon:(UIImageView*)icon
{
    self = [super initWithFrame:frame];
    if (self) {
        self.leftView = icon;
        self.leftViewMode = UITextFieldViewModeAlways;
    }
    return self;
}

-(CGRect) leftViewRectForBounds:(CGRect)bounds {
    CGRect iconRect = [super leftViewRectForBounds:bounds];
    iconRect.origin.x += 10;// 右偏10
    return iconRect;
}

@end
UIImage *usernameImage = [UIImage imageNamed:@"user"];
UIImageView *usernameIcon = [[UIImageView alloc] initWithImage:usernameImage];
usernameIcon.frame = CGRectMake(0, 0, 20, 20);

self.username = [[YLSTextField alloc] initWithFrame:CGRectMake(0, 0, 240, 30) Icon:usernameIcon];
self.username.placeholder = @"用户名";
self.username.borderStyle = UITextBorderStyleRoundedRect;
self.username.clearButtonMode = UITextFieldViewModeWhileEditing;
[self.username setKeyboardType:UIKeyboardTypeNumberPad];

关键就是定义UITextField的子类,并覆盖其leftViewRectForBounds方法

UITextField设置leftView的Insets

时间: 2024-10-26 13:13:49

UITextField设置leftView的Insets的相关文章

给UITextField设置头或尾空白

有时候,我们需要在UITextField的头尾加入一些空白,如下图所示: 其中,黄色和红色部分代表空白. 实现起来,比较简单,只需要设置UITextField的leftView.leftViewMode和rightView.rightViewMode即可,代码如下: 假设现有UItextField * txtField: UIView * rView =[[UIView alloc] initWithFrame:CGRectMake(txtField.frame.size.width - 10,

【代码笔记】UITextField设置placeholder颜色

一,效果图. 二,工程图. 三,代码. RootViewController.h #import <UIKit/UIKit.h> @interface RootViewController : UIViewController <UITextFieldDelegate> @end RootViewController.m #import "RootViewController.h" @interface RootViewController () @end @i

iOS UITextField设置placeholder颜色

设置UITextField的placeholder颜色 UIColor *color = [UIColor blackColor]; textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"密码" attributes:@{NSForegroundColorAttributeName: color}]; iOS UITextField设置placeholder颜色

(转)设置 UILabel 和 UITextField 的 Padding 或 Insets (理解UIEdgeInsets)

转自http://unmi.cc/uilable-uitextfield-padding-insets 主要是理解下UIEdgeInsets在IOS UI里的意义.靠,这货其实就是间隔,起个名字这么让人费解!!!正值表示间隔值,负值表示超出参照物的距离.--------------------------------------------------------分割线,下面是转载原文--------------------------------------------------- iOS 

iOS UITextField 设置下底边

@interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(30, 100, 160, 30)]; textField.text = @"设置textField下边框"; CALayer *b

iOS UITextField 设置内边距

http://blog.csdn.net/erica_sadun/article/details/9088181 1.inherit UITextField Class. ? [cpp]?view plaincopy .h?? //?? //??TextField.h?? //??TTShow?? //?? //??Created?by?twb?on?13-9-10.?? //??Copyright?(c)?2013年?twb.?All?rights?reserved.?? //?? ?? #i

UITextView模拟UITextField 设置Placeholder属性 --董鑫

由于最近有用到输入框,刚开始考虑的是UITextField,因为它在没有输入的时候可以有提示的Placeholder更能,很人性化,但UITextField只能单行输入,不能跳行,对于一些强迫症的亲来说,很别捏!所以我就想用UITextView,并找出Placeholder的类似方法.我的思路是使用2个UITextView来模拟出UITextField的PlaceHolder效果,一个背景为透明的TextView放在最上面,另一个责作为PlaceHolder的TextView放在最底层.它们之间

UITextField的leftView左侧留空隙

UIImageView *loginNameFieldLeftView = [[UIImageView alloc]initWithFrame:CGRectMake(5, 0, 15, 15)]; loginNameFieldLeftView.image = [UIImage imageNamed:@"username.png"]; UIView *leftPaddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 25, 

iOS学习-UITextField设置placeholder的颜色

UITextField *text = [[UITextField alloc] initWithFrame:CGRectMake(50, 200, 200, 40)]; text.borderStyle = UITextBorderStyleRoundedRect; NSMutableAttributedString * attributedStr = [[NSMutableAttributedString alloc]initWithString:@"密码"]; [attribut