0
我想從用戶輸入到UITextView的信息中編制一個編號列表。例如,在UITextView中創建一個編號列表Swift 3
- 列表項一個
- 列表項中包含兩個
- 列表項目三
這裏是我已經試過,但並沒有給我想要的效果的代碼。 VAR currentLine:INT = 1
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
// Add "1" when the user starts typing into the text field
if (textView.text.isEmpty && !text.isEmpty) {
textView.text = "\(currentLine). "
currentLine += 1
}
else {
if text.isEmpty {
if textView.text.characters.count >= 4 {
let str = textView.text.substring(from:textView.text.index(textView.text.endIndex, offsetBy: -4))
if str.hasPrefix("\n") {
textView.text = String(textView.text.characters.dropLast(3))
currentLine -= 1
}
}
else if text.isEmpty && textView.text.characters.count == 3 {
textView.text = String(textView.text.characters.dropLast(3))
currentLine = 1
}
}
else {
let str = textView.text.substring(from:textView.text.index(textView.text.endIndex, offsetBy: -1))
if str == "\n" {
textView.text = "\(textView.text!)\(currentLine). "
currentLine += 1
}
}
}
return true
}
如下建議:How to make auto numbering on UITextview when press return key in swift但沒有成功。
任何幫助,非常感謝。
老兄,非常感謝你,我甚至沒有想到這樣做,你搖滾! –
@JacobE歡迎你 –
我只是試圖實現這個到一個新的應用程序,以及項目符號列表和一件工作,請你檢查一下s://stackoverflow.com/q/45869927/7513942?sem = 2 –