Custom Class of UITextView Not Hooking Up

ⅰ亾dé卋堺 提交于 2019-12-11 14:54:09

问题


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())
        }
    }
}

来源:https://stackoverflow.com/questions/45869927/custom-class-of-uitextview-not-hooking-up

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!