uitextfield 设置左图标以及图标文字间距,输入框背景图切换

参考网上资料,直接使用网上的代码,没有实现,重新修改
YLSTextField文件

#import <UIKit/UIKit.h>
@interface YLSTextField : UITextField
@end

#import "YLSTextField.h"
@implementation YLSTextField
//改变文字位置
-(CGRect) textRectForBounds:(CGRect)bounds{
    CGRect iconRect=[super textRectForBounds:bounds];
    iconRect.origin.x+=10;
    return iconRect;
}
//改变编辑时文字位置
-(CGRect) editingRectForBounds:(CGRect)bounds{
    CGRect iconRect=[super editingRectForBounds:bounds];
    iconRect.origin.x+=10;
    return iconRect;
}
@end

页面中代码

self.loginuser.delegate=self;
    self.loginpass.delegate=self;
    //设置输入框左边图标
    UIImageView *image1=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"user.png"]];
    image1.frame=CGRectMake(0, 0, 27, 27);
    self.loginuser.leftView=image1;
    self.loginuser.leftViewMode=UITextFieldViewModeAlways;
    UIImageView *image2=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"mima.png"]];
    image2.frame=CGRectMake(0, 0, 27, 27);
    self.loginpass.leftView=image2;
    self.loginpass.leftViewMode=UITextFieldViewModeAlways;

输入框点击时背景图改变,不知道为啥在storyboard中设置的没有起作用。

//改变输入框背景图片
-(void)textFieldDidBeginEditing:(YLSTextField *)textField{
    if (textField.tag==3) {
        [self.loginuser setBackground:[UIImage imageNamed:@"inputlan.png"]];
    }else if(textField.tag==4){
        [self.loginpass setBackground:[UIImage imageNamed:@"inputlan.png"]];
    }
}

-(void)textFieldDidEndEditing:(YLSTextField *)textField{
    if (textField.tag==3) {
        [self.loginuser setBackground:[UIImage imageNamed:@"inputhui.png"]];
    }else if(textField.tag==4){
        [self.loginpass setBackground:[UIImage imageNamed:@"inputhui.png"]];
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值