将左边距添加到UITextField

时间:2011-04-15 09:13:21

标签: ios cocoa-touch uitextfield alignment leftalign

我想将UITextField文本的左边距设为10像素。最好的方法是什么?

9 个答案:

答案 0 :(得分: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

答案 1 :(得分:18)

正如我在之前的评论中所解释的那样,在这种情况下,最好的解决方案是扩展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

类别旨在向现有类添加新函数,而不是覆盖现有方法。

答案 2 :(得分:11)

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;

试试此代码

答案 3 :(得分:8)

对于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更改为更大的值。

答案 4 :(得分:3)

我希望在物业检查员的IB中看到一个设置来调整这个值。结果我无意中将对齐和边框样式设置为与左边的填充相混淆的值。

你不会认为选择左对齐和没有边框会是一个大问题,但它会使单词基本上重叠或正确到达框的边缘并且它看起来并不正确。

为:

enter image description here

好:

enter image description here

为我修好了。希望这也有助于其他人。

答案 5 :(得分:2)

子类化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)
    }
}

答案 6 :(得分: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;


}

答案 7 :(得分:0)

对于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))
    }
}

答案 8 :(得分:-3)

你可以试试这个

TextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
TextField.textAlignment = UITextAlignmentCenter;
相关问题