设置 UITextField 输入内容向右偏移 ,UITextField 的leftView 和rightView

@property (nonatomic, strong)UITextField * textField;
@property (nonatomic, strong)UIView * tfLeftView;
@property (nonatomic, strong)UIView * tfRightView;

效果如图

在这里插入图片描述

自定义输入框左侧是视图

-(UIView *)tfLeftView{
if (!_tfLeftView) {

    _tfLeftView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 60, 44)];
    UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 50, 44)];
    label.text = @"+86 >";
    label.font = [UIFont systemFontOfSize:15];
    label.textColor = [UIColor colorWithRed:156.0f/255.0f green:159.0f/255.0f blue:163.0f/255.0f alpha:1];
    [_tfLeftView addSubview:label];
                                                                
}
return _tfLeftView;

};

自定义输入框右侧视图

-(UIView *)tfRightView{

if (!_tfRightView) {
    
    _tfRightView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 60, 44)];
    UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 60, 44)];
    label.textAlignment = NSTextAlignmentCenter;
    label.backgroundColor = [UIColor yellowColor];
    label.text = @"test";
    label.font = [UIFont systemFontOfSize:15];
    label.textColor = [UIColor blueColor];
    [_tfRightView addSubview:label];
    
}
return _tfRightView;

};

  • (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];

    self.textField = [[UITextField alloc]initWithFrame:CGRectMake(10, 200, 300, 44)];
    self.textField.placeholder = @“请输入手机号”;
    self.textField.font = [UIFont systemFontOfSize:15];
    self.textField.textColor = [UIColor colorWithRed:156.0f/255.0f green:159.0f/255.0f blue:163.0f/255.0f alpha:1];
    self.textField.backgroundColor = [UIColor whiteColor];
    self.textField.layer.cornerRadius = __kNewSize(4.f);
    self.textField.layer.borderWidth = 0.5f;
    self.textField.layer.borderColor =[UIColor colorWithRed:156.0f/255.0f green:159.0f/255.0f blue:163.0f/255.0f alpha:1].CGColor;

    self.textField.leftView = self.tfLeftView;
    self.textField.leftViewMode = UITextFieldViewModeAlways;

    self.textField.rightView = self.tfRightView;
    self.textField.rightViewMode = UITextFieldViewModeAlways;

    [self.view addSubview:self.textField];

    }

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值