在 UITextView Swift 3 中创建编号列表 [英] Creating A Numbered List In UITextView Swift 3

查看:27
本文介绍了在 UITextView Swift 3 中创建编号列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据用户输入到 UITextView 的信息制作一个编号列表.例如,

I am trying to make a numbered list out of the information the user inputs into a UITextView. For example,

  1. 列出第一项
  2. 列出第二项
  3. 列出项目三

这是我尝试过的代码,但没有给我想要的效果.var currentLine: Int = 1

Here is the code that I have tried but does not give me the desired effect. 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
}

如此处建议:如何在快速按下返回键时在 UITextview 上进行自动编号但没有成功.

as suggested here: How to make auto numbering on UITextview when press return key in swift but had no success.

非常感谢任何帮助.

推荐答案

您可以子类化 UITextView,覆盖方法 willMove(toSuperview,并使用选择器为 UITextViewTextDidChange 添加一个观察者,该选择器将您的文本分成几行并相应地对其进行编号.尝试像这样:

You can subclass UITextView, override method willMove(toSuperview and add an observer for UITextViewTextDidChange with a selector that break up your text into lines enumerating and numbering it accordingly. Try like this:

class NumberedTextView: UITextView {
    override func willMove(toSuperview newSuperview: UIView?) {
        frame = newSuperview?.frame.insetBy(dx: 50, dy: 80) ?? frame
        backgroundColor = .lightGray
        NotificationCenter.default.addObserver(self, selector: #selector(textViewDidChange), name: .UITextViewTextDidChange, object: nil)
    }
    func textViewDidChange(notification: Notification) {
        var lines: [String] = []
        for (index, line) in text.components(separatedBy: .newlines).enumerated() {
            if !line.hasPrefix("\(index.advanced(by: 1))") &&
                !line.trimmingCharacters(in: .whitespaces).isEmpty {
                lines.append("\(index.advanced(by: 1)). " + line)
            } else {
                lines.append(line)
            }
        }
        text = lines.joined(separator: "\n")
        // this prevents two empty lines at the bottom
        if text.hasSuffix("\n\n") {   
            text = String(text.characters.dropLast())
        }
    }
}

<小时>

import UIKit

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        let textView = NumberedTextView()
        view.addSubview(textView)
    }
}

这篇关于在 UITextView Swift 3 中创建编号列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆