2015-02-12 77 views
1

如何在swift中爲文本字段(UITextField)創建填充?我希望它是全球範圍,所以當UIView加載每個文本字段有這個填充適用於它。在xCode 6.1中創建UITextField的通用填充/插圖(Swift)

當前銀行代碼:

class MyCustomTextField: UITextField { 
// Custom Code that i can't figure out. 
} 

步驟我嘗試採取:

  • 蘋果開發者文檔,它不談論插圖或填充 覆蓋
  • 搜索Stackoverflow並且看到它們或者與objective-c相關,或者 黑客在Xc被移除時頌成爲6.1
  • 谷歌,一無所獲約斯威夫特填充(僅目標c)

謝謝你在先進。
Swift初學者:)

回答

1
class MyCustomTextField : UITextField { 
    var leftMargin : CGFloat = 10.0 

    override func textRectForBounds(bounds: CGRect) -> CGRect { 
     var newBounds = bounds 
     newBounds.origin.x += leftMargin 
     return newBounds 
    } 

    override func editingRectForBounds(bounds: CGRect) -> CGRect { 
     var newBounds = bounds 
     newBounds.origin.x += leftMargin 
     return newBounds 
    } 
}