【发布时间】:2011-08-06 04:32:28
【问题描述】:
我想将UITextField 文本的左边距设置为 10 像素。最好的方法是什么?
【问题讨论】:
-
有关此问题的更详细答案,请参阅此答案stackoverflow.com/questions/3727068/…
标签: ios cocoa-touch uitextfield alignment leftalign
我想将UITextField 文本的左边距设置为 10 像素。最好的方法是什么?
【问题讨论】:
标签: ios cocoa-touch uitextfield alignment leftalign
您可以通过扩展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
【讨论】:
正如我在之前的评论中所解释的,在这种情况下,最好的解决方案是扩展 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
类别旨在向现有类添加新功能,而不是覆盖现有方法。
【讨论】:
return CGRectInset([super textRectForBounds:bounds], 10.f, 0.f);
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;
试试这个代码
【讨论】:
对于 Swift 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 更改为更大的值。
【讨论】:
【讨论】:
UITextBorderStyleRoundedRect 为 TextField 添加了一个很好的左侧填充,而 UITextBorderStyleNone 加上您自己的(直角)边框使文本正好靠在边缘。
继承 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)
}
}
【讨论】:
我几乎已经通过覆盖- (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;
}
【讨论】:
对于 Swift 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))
}
}
【讨论】:
你可以试试这个
TextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
TextField.textAlignment = UITextAlignmentCenter;
【讨论】: