繁体   English   中英

通过子类在文本字段前面添加空格

[英]Adding spaces in front of text field via subclassing

因此,我目前正在尝试在文本字段中的文本之前添加一些空间。 因此,当我键入内容时,它以四个空格而不是一个空格开头。 因此,我进行了一些研究,发现了一些看起来确实不错的东西(如所有投票所表明的那样),即在UITextField的开头创建空间 一个问题是,我不知道如何在其他班级中实际实现这一点(假设这就是帖子打算让读者做的事情)。

这就是我应该做的。 我想应该实例化该类的对象,并使用该类中的方法在我的文本字段前面添加空格。 但是我实际上不知道代码中的样子。 谁能给我一个例子,说明如何实际实现本文中的代码? 在UITextField的开头创建空间

这是我到目前为止的代码:

import UIKit

class SignUpViewController: UIViewController {

    @IBOutlet weak var facebookButton: UIButton!
    @IBOutlet weak var googleplusButton: UIButton!
    @IBOutlet weak var fullNameTextField: UITextField!
    @IBOutlet weak var emailAddressTextField: UITextField!
    @IBOutlet weak var passwordTextField: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()
        facebookButton.layer.cornerRadius = 5
        googleplusButton.layer.cornerRadius = 5

        fullNameTextField.layer.borderColor = UIColor.lightGrayColor().CGColor
        fullNameTextField.layer.borderWidth = 1.0
        emailAddressTextField.layer.borderColor = UIColor.lightGrayColor().CGColor
        emailAddressTextField.layer.borderWidth = 1.0
        passwordTextField.layer.borderColor = UIColor.lightGrayColor().CGColor
        passwordTextField.layer.borderWidth = 1.0

    }
}

您需要创建UITextField子类。 然后将此代码添加到其实现中

static CGFloat leftMargin = 10;
- (CGRect)textRectForBounds:(CGRect)bounds
{
    bounds.origin.x += leftMargin;
    bounds.size.width -= (leftMargin + 20);
    return bounds;
}
- (CGRect)editingRectForBounds:(CGRect)bounds
{
    bounds.origin.x += leftMargin;
    bounds.size.width -= (leftMargin + 20);
    return bounds;
}

之后,将自定义类设置为UITextField

您应该从@ScareCrow answer创建一个类。 之后,转到情节提要,并更改UITextField的类。 如: 图片

之后,创建文本字段的IBOutlet。 该出口将是TextField类的实例。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM