ios 向 UITextField 添加左边距

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5674655/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 19:34:57  来源:igfitidea点击:

Add lefthand margin to UITextField

ioscocoa-touchuitextfieldalignmentleftalign

提问by iOSPawan

I want to put the left margin of a UITextField's text at 10 px. What is the best way to do that?

我想将 aUITextField文本的左边距放在 10 像素处。最好的方法是什么?

采纳答案by clopez

As I have explained in a previous comment, the best solution in this case is to extend the UITextFieldclass instead of using a category, so you can use it explicitly on the desired text fields.

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

A category is intended to add new functions to an existing class, not to override an existing method.

一个类别的目的是新的功能添加到现有的类,而不是重写现有的方法。

回答by Riad Krim

You can do it by extending UITextFieldclass and overriding two methods:

您可以通过扩展UITextField类并覆盖两个方法来实现:

- (CGRect)textRectForBounds:(CGRect)bounds;
- (CGRect)editingRectForBounds:(CGRect)bounds;

Here is the code:

这是代码:

The interface in MYTextField.h

界面在 MYTextField.h

@interface MYTextField : UITextField

@end

Its implementation in MYTextField.m

它的实施在 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

回答by btmanikandan

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;

Try this code

试试这个代码

回答by Mamta

For Swift 3 :

对于 Swift 3 :

Make an outlet of the UITextField, say usernameTextField. Then write the following code in viewDidLoad()

做一个出口UITextField,比如 usernameTextField。然后在里面写入以下代码viewDidLoad()

let paddingView : UIView = UIView(frame: CGRect(x: 0, y: 0, width: 5, height: 20))
usernameTextField.leftView = paddingView
usernameTextField.leftViewMode = .always

Change the width: 5to a greater value if more space is required.

width: 5如果需要更多空间,请将更改为更大的值。

回答by Ethan Parker

I was hoping to see a setting in IB in the property inspector to adjust this value. Turns out I had set alignment and border style inadvertently to values that messed with the padding on the left.

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

You wouldn't think it would be a big deal to choose left justify and no border but it makes the words basically overlap or come right to the edge of the box and it doesn't look right.

你不会认为选择左对齐和无边框会是什么大不了的事,但它使单词基本上重叠或出现在框的边缘并且看起来不正确。

Bad:

坏的:

enter image description here

在此处输入图片说明

Good:

好的:

enter image description here

在此处输入图片说明

Fixed it right up for me. Hope this helps someone else as well.

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

回答by Claytog

Subclass the UITextField and add an extension:

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

usage

用法

class MyUITextFieldClass: UITextField {
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        self.paddingLeft(inset: 10)
    }
}

回答by iOSPawan

i have reached almost by overriding - (CGRect)textRectForBounds:(CGRect)bounds. now issue is when TextField goes in to edit mode their left margin reset to Zero .......

我几乎通过覆盖达到了- (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;


}

回答by Payne Miller

For Swift 4:

对于 Swift 4

I prefer to use IBDesignableclass and IBInspectableproperties to allow me to set the padding via Xcode storyboards and keep it reusable. I've also updated the code to work in 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))
    }
}

回答by Anish

You can try this

你可以试试这个

TextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
TextField.textAlignment = UITextAlignmentCenter;