Swift 4:如何在 UITextView 中设置每行只有一个单词并为每一行创建一个数组
Swift 4: How to set only one word per row in an UITextView and create an array with each row
我创建了一个 UITextView
并且我希望每行只有一个词,这样每次我按 space 破折号时它实际上是 return 文本。我怎样才能使 space 按钮 return 而不是实际间隔文本?另外,有没有办法记录每个单词以创建自定义数组?
这是我现在的代码:
@IBAction weak var wordView : UITextView!
@IBLabel weak var label : UILabel!
var words : [String] = []
override func viewDidLoad() {
super.viewDidLoad()
let endEditingTapGesture = UITapGestureRecognizer(target: view, action: #selector(UIView.endEditing(_:)))
endEditingTapGesture.cancelsTouchesInView = false
view.addGestureRecognizer(endEditingTapGesture)
}
@IBAction func button(_ sender: Any) {
getArray()
}
func getArray() {
for _ in words {
words.append(wordView.text)
}
}
每次按下按钮时,按钮都会向数组中添加单词...我不确定这是最好的解决方案...有什么帮助吗?
试试用下面的 textView 委托方法用新行替换 space,
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
//checks if new text is white space
if (text == " ") {
if (textView.text?.characters.last == "\n") {
// this will prevent multiple new lines
return false
}
let newText = (textView.text as NSString).replacingCharacters(in: range, with: "\n")
textView.text = newText
return false
}
return true
}
获取所有单词的数组
let allWords = yourTextView.text.components(separatedBy: "\n")
如果您正在使用委托,则可以使用委托。
这里是示例代码。
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
let oldText = NSString(format: "%@", textView.text)
var newText = oldText.replacingCharacters(in: range, with: text)
newText = newText.replacingOccurrences(of: " ", with: "\n")
newText = newText.replacingOccurrences(of: "-", with: "\n")
textView.text = newText
let myWordArr = textView.text.components(separatedBy: "\n")
print(myWordArr)
return false
}
别忘了设置委托。
我创建了一个 UITextView
并且我希望每行只有一个词,这样每次我按 space 破折号时它实际上是 return 文本。我怎样才能使 space 按钮 return 而不是实际间隔文本?另外,有没有办法记录每个单词以创建自定义数组?
这是我现在的代码:
@IBAction weak var wordView : UITextView!
@IBLabel weak var label : UILabel!
var words : [String] = []
override func viewDidLoad() {
super.viewDidLoad()
let endEditingTapGesture = UITapGestureRecognizer(target: view, action: #selector(UIView.endEditing(_:)))
endEditingTapGesture.cancelsTouchesInView = false
view.addGestureRecognizer(endEditingTapGesture)
}
@IBAction func button(_ sender: Any) {
getArray()
}
func getArray() {
for _ in words {
words.append(wordView.text)
}
}
每次按下按钮时,按钮都会向数组中添加单词...我不确定这是最好的解决方案...有什么帮助吗?
试试用下面的 textView 委托方法用新行替换 space,
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
//checks if new text is white space
if (text == " ") {
if (textView.text?.characters.last == "\n") {
// this will prevent multiple new lines
return false
}
let newText = (textView.text as NSString).replacingCharacters(in: range, with: "\n")
textView.text = newText
return false
}
return true
}
获取所有单词的数组
let allWords = yourTextView.text.components(separatedBy: "\n")
如果您正在使用委托,则可以使用委托。
这里是示例代码。
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
let oldText = NSString(format: "%@", textView.text)
var newText = oldText.replacingCharacters(in: range, with: text)
newText = newText.replacingOccurrences(of: " ", with: "\n")
newText = newText.replacingOccurrences(of: "-", with: "\n")
textView.text = newText
let myWordArr = textView.text.components(separatedBy: "\n")
print(myWordArr)
return false
}
别忘了设置委托。