我想为我的UITextFields使用一个自定义背景。这很好,除了我必须使用UITextBorderStyleNone来使它看起来很漂亮。这将迫使文本保持在左边,没有任何填充。
我可以手动设置填充,使它看起来类似于UITextBorderStyleRoundedRect除了使用我的自定义背景图像?
我想为我的UITextFields使用一个自定义背景。这很好,除了我必须使用UITextBorderStyleNone来使它看起来很漂亮。这将迫使文本保持在左边,没有任何填充。
我可以手动设置填充,使它看起来类似于UITextBorderStyleRoundedRect除了使用我的自定义背景图像?
当前回答
斯威夫特版本:
extension UITextField {
@IBInspectable var padding_left: CGFloat {
get {
LF.log("WARNING no getter for UITextField.padding_left")
return 0
}
set (f) {
layer.sublayerTransform = CATransform3DMakeTranslation(f, 0, 0)
}
}
}
这样你就可以在IB中赋值
其他回答
只是UITextField的子类,像这样:
@implementation DFTextField
- (CGRect)textRectForBounds:(CGRect)bounds
{
return CGRectInset(bounds, 10.0f, 0);
}
- (CGRect)editingRectForBounds:(CGRect)bounds
{
return [self textRectForBounds:bounds];
}
@end
这增加了10点的水平填充两边。
编辑:在iOS 11.3.1中仍然有效
在iOS 6 myTextField。leftView = paddingView;引起了问题
这就解决了问题
myTextField.layer.sublayerTransform = CATransform3DMakeTranslation(5, 0, 0)
对于右对齐的文本字段使用CATransform3DMakeTranslation(- 5,0,0),正如latenitecoder在注释中提到的那样
我创建了这个类别实现,并将其添加到.m文件的顶部。
@implementation UITextField (custom)
- (CGRect)textRectForBounds:(CGRect)bounds {
return CGRectMake(bounds.origin.x + 10, bounds.origin.y + 8,
bounds.size.width - 20, bounds.size.height - 16);
}
- (CGRect)editingRectForBounds:(CGRect)bounds {
return [self textRectForBounds:bounds];
}
@end
根据彼得·布拉斯亚克提供的链接。这似乎比创建一个全新的子类更简单,也比添加额外的UIView更简单。不过,似乎缺少了一些东西,无法控制文本字段内的填充。
Swift 4解决方案:
class CustomTextField: UITextField {
struct Constants {
static let sidePadding: CGFloat = 10
static let topPadding: CGFloat = 8
}
override func textRect(forBounds bounds: CGRect) -> CGRect {
return CGRect(
x: bounds.origin.x + Constants.sidePadding,
y: bounds.origin.y + Constants.topPadding,
width: bounds.size.width - Constants.sidePadding * 2,
height: bounds.size.height - Constants.topPadding * 2
)
}
override func editingRect(forBounds bounds: CGRect) -> CGRect {
return self.textRect(forBounds: bounds)
}
}
如果有人正在寻找Swift 4.0版本,那么下面的扩展是工作。它有左填充和右填充UITextField。实际上它是IBInspectable用于storyboard配置。您可以直接从接口构建器/故事板设置该值。这是在Swift 4.0版本和Xcode 9.0的测试代码
请记住,如果你想在同一个UITextField上启用清除按钮,那么你必须保持右填充空白。
import UIKit
extension UITextField {
@IBInspectable var paddingLeft: CGFloat {
get {
return leftView!.frame.size.width
}
set {
let paddingView = UIView(frame: CGRect(x: 0, y: 0, width: newValue, height: frame.size.height))
leftView = paddingView
leftViewMode = .always
}
}
@IBInspectable var paddingRight: CGFloat {
get {
return rightView!.frame.size.width
}
set {
let paddingView = UIView(frame: CGRect(x: 0, y: 0, width: newValue, height: frame.size.height))
rightView = paddingView
rightViewMode = .always
}
}
}
最好的方法是使用UITextField的子类并在.m文件中创建一个类
#import "CustomTextField.h"
#import <QuartzCore/QuartzCore.h>
@implementation CustomTextField
- (id)initWithCoder:(NSCoder*)coder
{
self = [super initWithCoder:coder];
if (self) {
//self.clipsToBounds = YES;
//[self setRightViewMode:UITextFieldViewModeUnlessEditing];
self.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0,15,46)];
self.leftViewMode=UITextFieldViewModeAlways;
}
return self;
}
通过这样做,去到你的故事板或xib,点击身份检查器,用你自己的“CustomTextField”在类选项替换UITextfield。
注意:如果你只是给文本框自动布局填充,那么你的应用程序将不会运行,只显示空白屏幕。