【问题标题】:Add lefthand margin to UITextField将左侧边距添加到 UITextField
【发布时间】:2011-08-06 04:32:28
【问题描述】:

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

【问题讨论】:

标签: ios cocoa-touch uitextfield alignment leftalign


【解决方案1】:

您可以通过扩展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 并覆盖相同的方法。所以请改用@clopez 解决方案。
  • 首先我投了赞成票,但后来投了反对票。如果为原点添加 10 px,则需要从宽度中减去 20 px 以保持左右边距相同。另外,正如上面已经提到的那样,通过类别覆盖方法确实是个坏主意。
  • 这个问题是关于左边距的。
  • 重置的原始边界值是多少?
【解决方案2】:

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

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

【讨论】:

  • 一个问题。如何使用 Storyboard 或 Interface Builder 中的这个新扩展类?非常感谢。
  • 嗨,Ricardo,您需要在 Storyboard 或 Interface Builder 中选择元素并在 Identity Inspector 中编辑自定义类。 grrrab.it/5rr3xy
  • 您正在覆盖的方法正在做一些您没有考虑到的事情,例如不包括覆盖视图占用的边框和空间......所以我会先打电话给 super,看起来像这样:return CGRectInset([super textRectForBounds:bounds], 10.f, 0.f);
【解决方案3】:
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;

试试这个代码

【讨论】:

  • 太棒了!谢谢!如果我们需要更大的边距,只需将 CGRectMake 5 修改为更大的值。
【解决方案4】:

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

【讨论】:

  • 这比子类化要好得多... :)
【解决方案5】:

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

您不会认为选择左对齐和无边框会有什么大不了的,但它会使单词基本上重叠或直接到框的边缘并且看起来不正确。

不好:

好:

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

【讨论】:

  • +1 注意到 UITextBorderStyleRoundedRect 为 TextField 添加了一个很好的左侧填充,而 UITextBorderStyleNone 加上您自己的(直角)边框使文本正好靠在边缘。
  • 这在本质上是有效的,但是如果您制作自己的图层边框(如圆形或椭圆形边缘),则会产生围绕控件制作浅色边框的副作用;所以你仍然需要使用 UITextBorderStyleNone 和 padding 方法来获得正确的效果。
【解决方案6】:

继承 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)
    }
}

【讨论】:

    【解决方案7】:

    我几乎已经通过覆盖- (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;
    
    
    }
    

    【讨论】:

    • 为什么不 bounds.origin.y - 20 ?
    【解决方案8】:

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

    【讨论】:

      【解决方案9】:

      你可以试试这个

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

      【讨论】:

      • 这不是回答问题
      猜你喜欢
      • 2015-04-12
      • 1970-01-01
      • 2011-10-24
      • 1970-01-01
      • 2013-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-25
      相关资源
      最近更新 更多