我想将 aUITextField
文本的左边距设置为 10 px。最好的方法是什么?
问问题
32739 次
9 回答
44
您可以通过扩展UITextField
类并覆盖两个方法来做到这一点:
- (CGRect)textRectForBounds:(CGRect)bounds;
- (CGRect)editingRectForBounds:(CGRect)bounds;
这是代码:
界面在MYTextField.h
@interface MYTextField : UITextField
@end
其实施在MYTextField.m
@implementation MYTextField
static CGFloat leftMargin = 28;
- (CGRect)textRectForBounds:(CGRect)bounds
{
bounds.origin.x += leftMargin;
return bounds;
}
- (CGRect)editingRectForBounds:(CGRect)bounds
{
bounds.origin.x += leftMargin;
return bounds;
}
@end
于 2011-05-11T12:45:14.470 回答
21
正如我在之前的评论中解释的那样,在这种情况下,最好的解决方案是扩展UITextField
类而不是使用类别,因此您可以在所需的文本字段上显式使用它。
#import <UIKit/UIKit.h>
@interface MYTextField : UITextField
@end
@implementation MYTextField
- (CGRect)textRectForBounds:(CGRect)bounds {
int margin = 10;
CGRect inset = CGRectMake(bounds.origin.x + margin, bounds.origin.y, bounds.size.width - margin, bounds.size.height);
return inset;
}
- (CGRect)editingRectForBounds:(CGRect)bounds {
int margin = 10;
CGRect inset = CGRectMake(bounds.origin.x + margin, bounds.origin.y, bounds.size.width - margin, bounds.size.height);
return inset;
}
@end
类别旨在向现有类添加新功能,而不是覆盖现有方法。
于 2012-03-28T05:03:37.033 回答
13
UITextField * textField = [[UITextField alloc]init];
[textField setDelegate:self];
[textField setFrame:CGRectMake(170,112,140,25)];
[textField setBorderStyle:UITextBorderStyleNone];
[textField setBackgroundColor:[UIColor clearColor]];
[self.View addSubview:noofChildTField];
UIView *paddingView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 20)] autorelease];
textField.leftView = paddingView;
textField.leftViewMode = UITextFieldViewModeAlways;
试试这个代码
于 2013-03-23T06:11:19.540 回答
11
对于斯威夫特 3:
做一个出口UITextField
,比如 usernameTextField。然后将以下代码写入viewDidLoad()
let paddingView : UIView = UIView(frame: CGRect(x: 0, y: 0, width: 5, height: 20))
usernameTextField.leftView = paddingView
usernameTextField.leftViewMode = .always
如果需要更多空间,请将更改width: 5
为更大的值。
于 2017-01-09T12:00:56.037 回答
5
子类化 UITextField 并添加扩展:
extension UITextField {
func paddingLeft(inset: CGFloat){
self.leftView = UIView(frame: CGRect(x: 0, y: 0, width: inset, height: self.frame.height))
self.leftViewMode = UITextField.ViewMode.always
}
}
用法
class MyUITextFieldClass: UITextField {
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
self.paddingLeft(inset: 10)
}
}
于 2019-08-28T01:23:38.553 回答
1
我几乎通过压倒一切达到了- (CGRect)textRectForBounds:(CGRect)bounds
。现在的问题是当 TextField 进入编辑模式时,他们的左边距重置为零......
@implementation UITextField(UITextFieldCatagory)
- (CGRect)textRectForBounds:(CGRect)bounds {
CGRect theRect=CGRectMake(bounds.origin.x+10, bounds.origin.y, bounds.size.width-10, bounds.size.height);
return theRect;
}
于 2011-04-15T09:17:00.330 回答
1
对于斯威夫特 4:
我更喜欢使用IBDesignable
类和IBInspectable
属性来允许我通过 Xcode 故事板设置填充并保持可重用。我还更新了代码以在 Swift 4 中工作。
import Foundation
import UIKit
@IBDesignable
class PaddableTextField: UITextField {
var padding = UIEdgeInsets(top: 0.0, left: 0.0, bottom: 0.0, right: 0.0)
@IBInspectable var left: CGFloat = 0 {
didSet {
adjustPadding()
}
}
@IBInspectable var right: CGFloat = 0 {
didSet {
adjustPadding()
}
}
@IBInspectable var top: CGFloat = 0 {
didSet {
adjustPadding()
}
}
@IBInspectable var bottom: CGFloat = 0 {
didSet {
adjustPadding()
}
}
func adjustPadding() {
padding = UIEdgeInsets(top: top, left: left, bottom: bottom, right: right)
}
override func prepareForInterfaceBuilder() {
super.prepareForInterfaceBuilder()
}
override func textRect(forBounds bounds: CGRect) -> CGRect {
return bounds.inset(by: UIEdgeInsets(top: top, left: left, bottom: bottom, right: right))
}
override func placeholderRect(forBounds bounds: CGRect) -> CGRect {
return bounds.inset(by: UIEdgeInsets(top: top, left: left, bottom: bottom, right: right))
}
override func editingRect(forBounds bounds: CGRect) -> CGRect {
return bounds.inset(by: UIEdgeInsets(top: top, left: left, bottom: bottom, right: right))
}
}
于 2018-12-02T03:46:55.767 回答
-3
你可以试试这个
TextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
TextField.textAlignment = UITextAlignmentCenter;
于 2011-04-15T09:17:26.863 回答