Ios 如何将后续文本添加到uitextfield
我有一个带有占位符的文本字段,名为Ios 如何将后续文本添加到uitextfield,ios,swift,uitextfield,Ios,Swift,Uitextfield,我有一个带有占位符的文本字段,名为letschat。现在,每当我开始在文本字段中键入内容时,我都希望将文本字段显示为some@letschat。当我的文本字段为空时,必须显示占位符。我做到了。但我想在我开始输入文本字段时设置。无论我用什么输入,我都希望此文本也可见,如: 一些@Lletschat 我如何才能做到这一点?使您的类符合UITextfieldDelegate,然后分配textfield.delegate=self 现在,如果希望在用户结束键入后追加@letschat,请添加此委托方法
letschat
。现在,每当我开始在文本字段中键入内容时,我都希望将文本字段显示为some@letschat
。当我的文本字段为空时,必须显示占位符。我做到了。但我想在我开始输入文本字段时设置。无论我用什么输入,我都希望此文本也可见,如:
一些@Lletschat
我如何才能做到这一点?使您的类符合
UITextfieldDelegate
,然后分配textfield.delegate=self
现在,如果希望在用户结束键入后追加@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"
}
希望这对您有所帮助。您可以使用UITextField
textfielddichange
的函数,并在每次文本字段文本更改时获得调用
就这样:
func textChangedAction(sender:UITextFiled) {
if sender.text.rangeOfString("@Lletschat") != nil{
sender.text = sender.text.replacingOccurrences(of: "@Lletschat", with: "")
}
sender.text = "\(sender.text!) @Lletschat"
}
如果要更改特定文本的颜色,可以进行检查。像这样实现textfield委托-
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
}
}
使用“自动布局”可使其之间保持3个像素的距离。
别忘了将类配置为将所有传入操作发送到textfield。您可以为这些控件使用不同的字体颜色,这样用户就不会对更改标签值的操作感到困惑。我创建了一个
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
}
}
}
}
您可以在此处看到预览:
您希望此文本显示在何处?在键入的字段下?@Astoria是的……我是说……当我完成键入文本字段时,如果文本字段中没有文本。此时,我的占位符将显示……当我开始键入占位符时,我应该隐藏……如果我键入
a
。那么我将自动看到类似a@letschat
。如果我键入更多类似adasd@letschat
必须在文本字段中可见此设置文本后将调用。您没有收到“@letschat@letschat@letschat“在输入了几个字符之后?它不是那样工作的didBeginEditing
在字段成为第一响应者时调用。@Astoria.Ya这是它的工作。但是一旦我开始编辑它的显示,这很好。但是当我取消所有文本并且没有文本时,我的意思是文本字段中甚至没有一个字母。然后我的占位符必须显示。但是现在仍然显示了“@letschat”。我怎样才能达到目标that@doubtman如果结果==“@letschat”{//set nil value并返回false}和“@letschat”shoyld不可编辑,请尝试使用。我再次将标记放在“@letschat”的后面,还有一个“@letschat”也在添加,而且我也尝试了这个…如果结果==“@letschat”{textField.text=”“return false}
当textField中没有值时。仍然是“@letschat”当你肯定需要实施困难的解决方案时,因为有太多的if
案例。我使用了带有占位符值的代码解决方案。但它根本没有反映…你能分享代码吗?这样,就不会有更多的代码了。您只需以编程方式创建一个subfixtfield
,或者在故事板中添加一个textfield,并将其自定义类设置为subfixtfield
。
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
}
}
}
}