如何将随后的文本添加到uitextfield
我有一个文本字段,其占位符为letschat
。现在,只要我开始在我的文本框中输入内容,我想将我的文本框显示为some @letschat
。当我的文本框为空时,我的占位符必须显示。我做到了。但是,只要我开始在我的文本框中输入内容,我就想设置它。无论我与打字我想这段文字也像可见:如何将随后的文本添加到uitextfield
一些@Lletschat
我怎样才能做到这一点?
顺应你的类UITextfieldDelegate
,然后分配textfield.delegate = self
现在,添加这个委托的方法,如果你想用户endtyping后要追加你@letschat。
func textFieldDidEndEditing(textField: UITextField) {
textField.text = "\(textField.text)@letschat"
}
或者如果你想在打字时间。
textField.addTarget(self, action: #selector(YourViewController.textFieldDidChange(_:)), forControlEvents: UIControlEvents.EditingChanged)
func textFieldDidChange(textField: UITextField) {
if textField.containsString("@letschat") {
textField.text = textField.text.stringByReplacingOccurrencesOfString("@letschat", withString: "")
}
textField.text = "\(textField.text)@letschat"
}
希望这会有所帮助。
这将在设置文本后调用。 –
@SalmanGhumsani查看我编辑的答案 –
输入几个字符后,您是否收到“... @ letschat @ letschat @ letschat”? – Astoria
您可以使用UITextField
textFieldDidChange
的action,并在每次更改textField文本时获得调用。
就像这样:
func textChangedAction(sender:UITextFiled) {
if sender.text.rangeOfString("@Lletschat") != nil{
sender.text = sender.text.replacingOccurrences(of: "@Lletschat", with: "")
}
sender.text = "\(sender.text!) @Lletschat"
}
如果你想改变你的具体文字的颜色,你可以检查here。
实现文本框的委托像这个 -
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
textField.text = textField.text?.replacingOccurrences(of: " @\(textField.placeholder!)", with: "", options: .literal, range: nil)
textField.text?.append(string)
textField.text?.append(" @\(textField.placeholder!)")
return false
}
简单的解决方案:
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
guard let newRange = textField.text?.range(from: range), let result = textField.text?.replacingCharacters(in: newRange, with: string) else { return true }
if result.endsWithString("@letschat") {
return true
} else {
textField.text = result + "@letschat"
let position = textField.position(from: textField.beginningOfDocument, offset: result.characters.count)!
textField.selectedTextRange = textField.textRange(from: position, to: position)
return false
}
}
有了帮手扩展:
extension String {
func range(from oldOne: NSRange) -> Range<String.Index>? {
guard
let from16 = utf16.index(utf16.startIndex, offsetBy: oldOne.location, limitedBy: utf16.endIndex),
let to16 = utf16.index(utf16.startIndex, offsetBy: oldOne.location + oldOne.length, limitedBy: utf16.endIndex),
let from = from16.samePosition(in: self),
let to = to16.samePosition(in: self)
else { return nil }
return from ..< to
}
func endsWithString(_ string: String) -> Bool {
guard characters.count >= string.characters.count else { return false }
let index = self.index(startIndex, offsetBy: characters.count - string.characters.count)
let substring = self.substring(from: index)
return substring == string
}
}
难,但明确的解决方案是创建你自己的UIControl
-subclass与UITextField
和UILabel
儿童意见:
+-----------+ +-----------+
| textfield | -(3px)- | @letschat |
+-----------+ +-----------+
使用autolayout来保持它之间的3个像素的距离。 不要忘记配置你的班级,把所有传入的动作发送到文本字段。您可以对这些控件使用不同的字体颜色,以便用户不会对改变标签价值的努力感到困惑。
我创建了一个UITextField
子类,它使用占位符(如果设置)作为后缀。据我所见,一切都按预期工作。也许有一些调整需要适应您的需求。
随意问,如果有不清楚的地方:
class SuffixTextField: UITextField {
override init(frame: CGRect) {
super.init(frame: frame)
sharedInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
sharedInit()
}
private func sharedInit() {
addTarget(self, action: #selector(textChanged), for: .editingChanged)
}
override var text: String? {
didSet {
selectedTextRange = maxTextRange
}
}
override var attributedText: NSAttributedString? {
didSet {
selectedTextRange = maxTextRange
}
}
@objc private func textChanged() {
if let currentText = text, let placeholder = placeholder {
if currentText == placeholder {
self.text = nil
} else if !currentText.hasSuffix(placeholder) {
self.text = currentText + placeholder
}
}
}
private var maxCursorPosition: UITextPosition? {
guard let placeholder = placeholder, !placeholder.isEmpty else { return nil }
guard let text = text, !text.isEmpty else { return nil }
return position(from: beginningOfDocument, offset: (text as NSString).range(of: placeholder, options: .backwards).location)
}
private var maxTextRange: UITextRange? {
guard let maxCursorPosition = maxCursorPosition else { return nil }
return textRange(from: maxCursorPosition, to: maxCursorPosition)
}
override var selectedTextRange: UITextRange? {
get { return super.selectedTextRange }
set {
guard let newRange = newValue,
let maxCursorPosition = maxCursorPosition else {
super.selectedTextRange = newValue
return
}
if compare(maxCursorPosition, to: newRange.start) == .orderedAscending {
super.selectedTextRange = textRange(from: maxCursorPosition, to: maxCursorPosition)
} else if compare(maxCursorPosition, to: newRange.end) == .orderedAscending {
super.selectedTextRange = textRange(from: newRange.start, to: maxCursorPosition)
} else {
super.selectedTextRange = newValue
}
}
}
}
在这里你可以看到一个预览: https://www.dropbox.com/s/etkbme37wuxbw1q/preview.mov?dl=0
我已经使用ur代码解决方案与持有人的价值..但ist一点也不反映...可以请你分享代码..所以,这将有助于 – doubtman
没有更多的代码。您只需以编程方式创建'SuffixTextField'或在故事板中添加文本字段并将其自定义类设置为'SuffixTextField'。 –
你想去哪里该文本将出现?在打字的下面? – Astoria
@Astoria YEAH ...我measn ...当我完成键入我的文本框,如果我的文本字段中没有文本。那时候我的占位符shoyld显示...当我开始输入我的占位符应该隐藏...并且如果我输入'a'.then自动我必须看到'a @ letschat'。同样的,如果我输入更多的词像'adasd @ letschat'必须在文本字段中可见 – doubtman