我想为我使用自定义背景UITextFields
.这工作正常,除了我必须使用UITextBorderStyleNone
它使它看起来漂亮的事实.这会强制文本粘在左边而不进行任何填充.
我可以手动设置填充,使其看起来类似于UITextBorderStyleRoundedRect
使用我的自定义背景图像吗?
我找到了一个整洁的小黑客为这种确切的情况设置左边填充.
基本上,您将leftView属性设置为UITextField
您想要的填充大小的空视图:
UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 20)]; textField.leftView = paddingView; textField.leftViewMode = UITextFieldViewModeAlways;
对我来说就像一个魅力!
在Swift 3/Swift 4中,可以通过这样做来完成
let paddingView: UIView = UIView(frame: CGRect(x: 0, y: 0, width: 5, height: 20)) textField.leftView = paddingView textField.leftViewMode = .always
我创建了这个类别实现并将其添加到.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
基于Piotr Blasiak提供的链接.它似乎比创建一个全新的子类更简单,也更简单,然后添加额外的UIView
.但是,似乎缺少一些东西,因为无法控制文本字段中的填充.
Swift 2解决方案:
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)
}
}
适用于Xcode> 6的Swift 3版本,您可以在其中编辑Interface Builder/Storyboard中的插入值.
import UIKit @IBDesignable class FormTextField: UITextField { @IBInspectable var inset: CGFloat = 0 override func textRect(forBounds bounds: CGRect) -> CGRect { return bounds.insetBy(dx: inset, dy: inset) } override func editingRect(forBounds bounds: CGRect) -> CGRect { return textRect(forBounds: bounds) } }
编辑:仍适用于iOS 11.3.1
在iOS 6 myTextField.leftView = paddingView;
中导致问题
这解决了这个问题
myTextField.layer.sublayerTransform = CATransform3DMakeTranslation(5, 0, 0)
对于右对齐文本字段CATransform3DMakeTranslation(-5, 0, 0)
,请在评论中使用latenitecoder
向UITextField添加填充的一种好方法是子类化并添加edgeInsets属性.然后设置edgeInsets,并相应地绘制UITextField.这也可以使用自定义leftView或rightView集正常运行.
OSTextField.h
#import@interface OSTextField : UITextField @property (nonatomic, assign) UIEdgeInsets edgeInsets; @end
OSTextField.m
#import "OSTextField.h" @implementation OSTextField - (id)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self) { self.edgeInsets = UIEdgeInsetsZero; } return self; } -(id)initWithCoder:(NSCoder *)aDecoder{ self = [super initWithCoder:aDecoder]; if(self){ self.edgeInsets = UIEdgeInsetsZero; } return self; } - (CGRect)textRectForBounds:(CGRect)bounds { return [super textRectForBounds:UIEdgeInsetsInsetRect(bounds, self.edgeInsets)]; } - (CGRect)editingRectForBounds:(CGRect)bounds { return [super editingRectForBounds:UIEdgeInsetsInsetRect(bounds, self.edgeInsets)]; } @end
只需将UITextField子类化为:
@implementation DFTextField - (CGRect)textRectForBounds:(CGRect)bounds { return CGRectInset(bounds, 10.0f, 0); } - (CGRect)editingRectForBounds:(CGRect)bounds { return [self textRectForBounds:bounds]; } @end
这会在两侧增加10个点的水平填充.
目标C代码
MyTextField.h
#import@interface MyTextField : UITextField @property (nonatomic) IBInspectable CGFloat padding; @end
MyTextField.m
#import "MyTextField.h" IB_DESIGNABLE @implementation MyTextField @synthesize padding; -(CGRect)textRectForBounds:(CGRect)bounds{ return CGRectInset(bounds, padding, padding); } -(CGRect)editingRectForBounds:(CGRect)bounds{ return [self textRectForBounds:bounds]; } @end
根据Evil Trout的回答,您可能想要创建一个类别,以便在多个应用程序中更容易使用.
头文件:
@interface UITextField (PaddingText) -(void) setLeftPadding:(int) paddingValue; -(void) setRightPadding:(int) paddingValue; @end
实施文件:
#import "UITextField+PaddingText.h" @implementation UITextField (PaddingText) -(void) setLeftPadding:(int) paddingValue { UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, paddingValue, self.frame.size.height)]; self.leftView = paddingView; self.leftViewMode = UITextFieldViewModeAlways; } -(void) setRightPadding:(int) paddingValue { UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, paddingValue, self.frame.size.height)]; self.rightView = paddingView; self.rightViewMode = UITextFieldViewModeAlways; } @end
用法示例
#import "UITextField+PaddingText.h" [self.YourTextField setLeftPadding:20.0f];
希望它可以帮助你们
干杯
创建文本字段自定义
PaddingTextField.swift
import UIKit class PaddingTextField: UITextField { @IBInspectable var paddingLeft: CGFloat = 0 @IBInspectable var paddingRight: CGFloat = 0 override func textRectForBounds(bounds: CGRect) -> CGRect { return CGRectMake(bounds.origin.x + paddingLeft, bounds.origin.y, bounds.size.width - paddingLeft - paddingRight, bounds.size.height); } override func editingRectForBounds(bounds: CGRect) -> CGRect { return textRectForBounds(bounds) }}
将textfield类设置为PaddingTextField并根据需要自定义填充
好好享受
Swift版本:
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中分配价值了
您无法设置填充.而是有一个UIView
具有你的背景图像和UITextField
它的内部.同样设置UITextField
宽度UIViewWidth-(paddingSize x 2)
和高度,然后将其设置为点paddingSize,paddingSize
.
我基于Nate的解决方案,但后来我发现当你使用leftView/rightView属性时这会导致问题,所以它更好地调整了super的实现,因为它会考虑左/右视图.
- (CGRect)textRectForBounds:(CGRect)bounds { CGRect ret = [super textRectForBounds:bounds]; ret.origin.x = ret.origin.x + 5; ret.size.width = ret.size.width - 10; return ret; } - (CGRect)editingRectForBounds:(CGRect)bounds { return [self textRectForBounds:bounds]; }
只是像这样继承UITextField(Swift版本):
import UIKit class CustomTextField: UITextField { override func textRectForBounds(bounds: CGRect) -> CGRect { return CGRectInset(bounds, 25.0, 0) } override func editingRectForBounds(bounds: CGRect) -> CGRect { return self.textRectForBounds(bounds) } }
这增加了两侧25.0点的水平填充.
Swift 3的更新版本
@IBDesignable class FormTextField: UITextField { @IBInspectable var paddingLeft: CGFloat = 0 @IBInspectable var paddingRight: CGFloat = 0 override func textRect(forBounds bounds: CGRect) -> CGRect { return CGRect(x: bounds.origin.x + paddingLeft, y: bounds.origin.y, width: bounds.size.width - paddingLeft - paddingRight, height: bounds.size.height) } override func editingRect(forBounds bounds: CGRect) -> CGRect { return textRect(forBounds: bounds) } }
基于@Evil Trout投票最多的答案,我在ViewController类中创建了一个自定义方法,如下所示:
- (void) modifyTextField:(UITextField *)textField { UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 20)]; textField.leftView = paddingView; textField.leftViewMode = UITextFieldViewModeAlways; textField.rightView = paddingView; textField.rightViewMode = UITextFieldViewModeAlways; [textField setBackgroundColor:[UIColor whiteColor]]; [textField setTextColor:[UIColor blackColor]]; }
现在,我可以在内部调用该方法(viewDidLoad方法),并将我的任何TextField发送到该方法,并为左右添加填充,并通过仅编写一行代码来为文本和背景色赋值,如下所示:
[self modifyTextField:self.firstNameTxtFld];
这在iOS 7上完美运行!我知道添加过多的View可能会使该类加载起来更重。但是,当担心其他解决方案中的困难时,我发现自己更偏向于这种方法,并且使用这种方式更加灵活。;)
感谢您的骇客“邪恶鳟鱼”!(弓)
我以为应该用Swift更新此答案的代码片段:
由于Swift允许我们为现有类编写扩展,因此我们以这种方式编写它。
extension UITextField { func addPaddingToTextField() { let paddingView: UIView = UIView.init(frame: CGRectMake(0, 0, 8, 20)) self.leftView = paddingView; self.leftViewMode = .Always; self.rightView = paddingView; self.rightViewMode = .Always; self.backgroundColor = UIColor.whiteColor() self.textColor = UIColor.blackColor() } }
用法:
self.firstNameTxtFld.addPaddingToTextField()
希望这对其他人有所帮助!
干杯!
以下是如何在SWIFT中实现这一目标
@IBOutlet weak var yourTextField: UITextField! override func viewDidLoad() { super.viewDidLoad() let paddingView = UIView(frame: CGRectMake(0, 0, 10, self.yourTextField.frame.height)) yourTextField.leftView = paddingView yourTextField.leftViewMode = UITextFieldViewMode.Always } }
资源
Swift 2.0版本:
let paddingView: UIView = UIView(frame: CGRectMake(0, 0, 5, 20)) textField.leftView = paddingView textField.leftViewMode = UITextFieldViewMode.Always;