Custom Class of UITextView Not Hooking Up

2019-08-20 17:01发布

I have a TextView that has a custom class of "BulletedTextView". Here is a screenshot of what is entered into the Storyboard File. Although, when I go into the simulator, it does not appear that it is connected at all. And, it just allows me to put multiple lines with no bullet. It should have multiple lines, and bullets on each of them, according to my code. This same code works in one of my other apps and it works just fine, no issues.

Here is my code:

import UIKit

class BulletedTextView: UITextView {

    override func willMove(toSuperview newSuperview: UIView?) {
        super.willMove(toSuperview: newSuperView)
        frame = newSuperview?.frame.insetBy(dx: 26, dy: 355) ?? frame
        backgroundColor = UIColor(red: 0x58/255, green: 0xCB/255, blue: 0xFB/255, alpha: 0.5)
        NotificationCenter.default.addObserver(self, selector: #selector(textViewDidChange), name: .UITextViewTextDidChange, object: nil)
    }
    func textViewDidChange(notification: Notification) {
        var lines: [String] = []
        let bullet = "\u{2022}"
        for (_, line) in text.components(separatedBy: .newlines).enumerated() {
            if !line.hasPrefix("\(bullet)") &&
                !line.trimmingCharacters(in: .whitespaces).isEmpty {
                lines.append("\(bullet)  " + 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())
        }
    }
}

0条回答
登录 后发表回答