首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何添加无序列表或UITextView中的每一行新行的项目点

如何添加无序列表或UITextView中的每一行新行的项目点
EN

Stack Overflow用户
提问于 2014-12-04 21:41:11
回答 3查看 7.6K关注 0票数 4

因此,基本上,我想向UITextView添加一个无序列表。在另一个UITextView中,我想添加一个有序列表。

我尝试使用这段代码,但在用户第一次按enter (仅此而已)之后,它只给了我一个弹着点,而且我甚至无法备份它。

代码语言:javascript
复制
- (void)textViewDidChange:(UITextView *)textView
{
    if ([myTextField.text isEqualToString:@"\n"]) {
        NSString *bullet = @"\u2022";
        myTextField.text = [myTextField.text stringByAppendingString:bullet];
    }
}

如果您只找到了一种使用Swift来执行它的方法,那么可以随意发布Swift版本的代码。

EN

Stack Overflow用户

发布于 2021-05-12 12:46:05

Swift 5.x版本。

代码语言:javascript
复制
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
    }
票数 1
EN
查看全部 3 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27304655

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档