您可以使用NSAttributedString
和NSTextAttachment
將圖像添加到文本。
我沒有使用過他們一段時間,但我相信你可以做這樣的事情......
let magnifyingGlassAttachment = NSTextAttachment(data: nil, ofType: nil)
magnifyingGlassAttachment.image = UIImage(named: "MagnifyingGlass")
let magnifyingGlassString = NSAttributedString(attachment: magnifyingGlassAttachment)
現在你可以使用magnifyingGlassString
屬性串,並將其添加爲歸因文本的一部分UITextField
。
我相信你也可以精確指定放大鏡如何呈現旁邊的字符串(它是如何包裝等)
事情是這樣的......
var attributedText = NSMutableAttributedString(attributedString: magnifyingGlassString)
let searchString = NSAttributedString(string: "Search for stuff")
attributedText.appendAttributedString(searchString)
textField.attributedPlaceholder = attributedText
嘗試重寫'placeholderRectForBounds: '。 – Justlike
佔位符位置完美,那麼如何幫助我? – Quantaliinuxite
覆蓋UITextField並將圖像視圖添加爲子視圖。 – Justlike