0

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 the code adapted from this post:

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())
        }
    }
}
Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
  • 5
    Please add your code as text not image. – Mo Abdul-Hameed Aug 24 '17 at 19:59
  • 1
    You are not calling `super.willMove(toSuperView: newSuperView)`. But I don't know whether it is an issue or not. – Orkhan Alikhanov Aug 24 '17 at 20:09
  • @MoeAbdul-Hameed Done! –  Aug 25 '17 at 13:10
  • @OrkhanAlikhanov I tried it. It did not work. –  Aug 25 '17 at 13:10
  • @OrkhanAlikhanov Calling super is pointless [func willMoveToSuperview:](https://developer.apple.com/documentation/uikit/uiview/1622629-willmove) **The default implementation of this method does nothing. Subclasses can override it to perform additional actions whenever the superview changes.** – Leo Dabus Apr 04 '21 at 21:31

0 Answers0