通过Interface Builder创建填充

时间:2016-03-14 11:39:49

标签: ios xcode swift uitextfield

我使用这篇文章作为参考:Create space at the beginning of a UITextField。在这篇文章中,有一个非常有用的类在文本字段中添加填充。但是,我知道如何使用这个类的唯一方法是我以编程方式创建一个文本字段。但相反,我想将此类与IBOutlet一起使用。这是TextField类:

print table
      Year Month  Value
0     1880   Jan    -29
1     1880   Feb    -20
2     1880   Mar    -18
3     1880   Apr    -27
4     1880   May    -14
5     1880   Jun    -28
6     1880   Jul    -23
7     1880   Aug     -7
8     1880   Sep    -16
9     1880   Oct    -16
10    1880   Nov    -18
11    1880   Dec    -21
12    1881   Jan     -9
13    1881   Feb    -13
14    1881   Mar      1
15    1881   Apr     -3
16    1881   May     -4
17    1881   Jun    -28
18    1881   Jul     -6
19    1881   Aug     -2
20    1881   Sep     -8
21    1881   Oct    -19
22    1881   Nov    -26
23    1881   Dec    -15
24    1882   Jan     10
25    1882   Feb      9
26    1882   Mar      2
27    1882   Apr    -20
28    1882   May    -17
29    1882   Jun    -25
...    ...   ...    ...
1604  2013   Sep     76
1605  2013   Oct     69
1606  2013   Nov     80
1607  2013   Dec     66
1608  2014   Jan     73
1609  2014   Feb     50
1610  2014   Mar     77
1611  2014   Apr     78
1612  2014   May     86
1613  2014   Jun     66
1614  2014   Jul     58
1615  2014   Aug     81
1616  2014   Sep     90
1617  2014   Oct     85
1618  2014   Nov     68
1619  2014   Dec     79
1620  2015   Jan     81
1621  2015   Feb     87
1622  2015   Mar     90
1623  2015   Apr     73
1624  2015   May     78
1625  2015   Jun     78
1626  2015   Jul     73
1627  2015   Aug     78
1628  2015   Sep     82
1629  2015   Oct    106
1630  2015   Nov    103
1631  2015   Dec    110
1632  2016   Jan    114
1633  2016   Feb    135

[1634 rows x 3 columns]

这是我尝试将它与我的IBOutlet一起使用:

class TextField: UITextField {

    let padding = UIEdgeInsets(top: 0, left: 35, bottom: 0, right: 5);

    override func textRectForBounds(bounds: CGRect) -> CGRect {
        return self.newBounds(bounds)
    }

    override func placeholderRectForBounds(bounds: CGRect) -> CGRect {
        return self.newBounds(bounds)
    }

    override func editingRectForBounds(bounds: CGRect) -> CGRect {
        return self.newBounds(bounds)
    }

    private func newBounds(bounds: CGRect) -> CGRect {
        print("paisjdpfij")
        var newBounds = bounds
        newBounds.origin.x += padding.left
        newBounds.origin.y += padding.top
        newBounds.size.height -= padding.top + padding.bottom
        newBounds.size.width -= padding.left + padding.right
        return newBounds
    }
}

但是,文本字段中仍然没有填充。任何人都有解决这个问题的方法吗?

现在,我使用了这段代码:

@IBOutlet weak var firstNameTextField: TextField!

override func viewDidLoad() {
    super.viewDidLoad()

    firstNameTextField = TextField()

}

在文本字段的左侧添加填充。不过,我也想在底部加一些填充物。并且似乎并不是添加底部填充的简单解决方案。

1 个答案:

答案 0 :(得分:0)

您可以像创建一样创建类别/扩展名或创建自定义文本字段。并实现这两种方法,并通过改变不同的界限来玩它。

import UIKit

class CustomTextField: UITextField 
{

    required init?(coder aDecoder: NSCoder){
        super.init(coder: aDecoder)
    }

    override func textRectForBounds(bounds: CGRect) -> CGRect 
    {
        return CGRectMake(bounds.origin.x + 10, bounds.origin.y + 8, bounds.size.width - 20, bounds.size.height - 16);
    }

    override func editingRectForBounds(bounds: CGRect) -> CGRect 
    {
        return self.textRectForBounds(bounds);
    }
}

有关详细信息,请参阅此Nate Flink的答案:Set padding for UITextField with UITextBorderStyleNone