【问题标题】:Add padding to uitextfield using appearance使用外观向 uitextfield 添加填充
【发布时间】:2014-11-25 03:02:38
【问题描述】:

我不想为了获得文本填充而继承 UITextField 类。

我想使用外观代理。

我试过这个:

UIView *padding = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 5, 2)];
[[UITextField appearance] setLeftView:padding];
[[UITextField appearance] setLeftViewMode:UITextFieldViewModeAlways];

不幸的是,如果我将此代码放在我的应用程序委托中,任何包含 uitextfield 的视图控制器都无法加载视图

如何在不对文本字段进行子类化的情况下添加填充?

【问题讨论】:

标签: ios uitextfield padding


【解决方案1】:
Can you try these..

- (CGRect)textRectForBounds:(CGRect)bounds {
    int margin = 10;
    CGRect inset = CGRectMake(bounds.origin.x + bounds.size.width/2, bounds.origin.y, bounds.size.width/2 - margin, bounds.size.height);
    return inset;
}

- (CGRect)editingRectForBounds:(CGRect)bounds {
    int margin = 10;
    CGRect inset = CGRectMake(bounds.origin.x + bounds.size.width/2, bounds.origin.y, bounds.size.width/2 - margin, bounds.size.height);
    return inset;
}

(或)

这样做

// Do any additional setup after loading the view from its nib.

 UIView *paddingTxtfieldView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 42)];// what ever you want 
 txtfield.leftView = paddingTxtfieldView;
 txtfield.leftViewMode = UITextFieldViewModeAlways;
 txtfield.returnKeyType = UIReturnKeyNext;// if you want to go next textfiled write this otherwise "UIReturnKeyDone"

【讨论】:

  • 我不想继承 uitextfield... 为了编写上述方法,我需要继承它
  • 因为它只是一个简单的填充功能不需要的类
  • 在此处查看更多信息stackoverflow.com/questions/19371018/…
  • 如果以上答案帮助您投票并标记为正确答案
猜你喜欢
  • 2014-04-15
  • 2021-03-12
  • 1970-01-01
  • 2013-01-29
  • 2011-12-15
  • 2019-07-31
  • 1970-01-01
  • 1970-01-01
  • 2018-05-13
相关资源
最近更新 更多