因此,基本上,我想向UITextView添加一个无序列表。在另一个UITextView中,我想添加一个有序列表。
我尝试使用这段代码,但在用户第一次按enter (仅此而已)之后,它只给了我一个弹着点,而且我甚至无法备份它。
- (void)textViewDidChange:(UITextView *)textView
{
if ([myTextField.text isEqualToString:@"\n"]) {
NSString *bullet = @"\u2022";
myTextField.text = [myTextField.text stringByAppendingString:bullet];
}
}如果您只找到了一种使用Swift来执行它的方法,那么可以随意发布Swift版本的代码。
发布于 2021-05-12 12:46:05
Swift 5.x版本。
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
//
// If the replacement text is "\n" and the
// text view is the one you want bullet points
// for
if (text == "\n") {
// If the replacement text is being added to the end of the
// text view, i.e. the new index is the length of the old
// text view's text...
if range.location == textView.text.count {
// Simply add the newline and bullet point to the end
let updatedText: String = textView.text! + "\n \u{2022} "
textView.text = updatedText
}
else {
// Get the replacement range of the UITextView
let beginning: UITextPosition = textView.beginningOfDocument
let start: UITextPosition = textView.position(from: beginning, offset: range.location)!
let end: UITextPosition = textView.position(from: start, offset: range.length)!
let textRange: UITextRange = textView.textRange(from: start, to: end)!
// Insert that newline character *and* a bullet point
// at the point at which the user inputted just the
// newline character
textView.replace(textRange, withText: "\n \u{2022} ")
// Update the cursor position accordingly
let cursor: NSRange = NSMakeRange(range.location + "\n \u{2022} ".count, 0)
textView.selectedRange = cursor
}
return false
}
// Else return yes
return true
}https://stackoverflow.com/questions/27304655
复制相似问题