我有一个TextView,它有一个自定义类" BulletedTextView"。 Here is a screenshot of what is entered into the Storyboard File.虽然,当我进入模拟器时,它似乎根本没有连接。并且,它只允许我放置多条没有子弹的线。根据我的代码,它应该有多行,并且每个行都有子弹。这个相同的代码适用于我的其他一个应用程序,它工作正常,没有问题。
这是我的代码:
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())
}
}
}