8.9 C
London
Friday, April 19, 2024

ios – textList of NSParagraphStyle not working for customized UITextVIew


I attempted with textList of NSParagrphStyle, it really works nicely

    let textView = UITextView()
    textView.backgroundColor = UIColor.grey
    textView.isEditable = false
    //Create NSTextList
    let checklist = NSTextList(markerFormat: .decimal, choices: 0)
    checklist.startingItemNumber = 1
    
    //Create NSParagraphStyle with textual content checklist
    let paragraphStyle = NSMutableParagraphStyle()
    paragraphStyle.textLists = [list]
    
    let attributes = [NSAttributedString.Key.paragraphStyle : paragraphStyle]
    
    //Add attributes to checklist block
    let nsmutableAttributedString = NSMutableAttributedString(string: "Test1nTest2nTest3", attributes: attributes)
    textView.attributedText = nsmutableAttributedString
    

enter image description here

whereas if I attempt to use customized layoutManager, then the textList is now not working,

func getTextView() -> UITextView {
    
    let textView = CustomTextView()
    textView.backgroundColor = UIColor.grey
    textView.isEditable = false
    //Create NSTextList
    let checklist = NSTextList(markerFormat: .decimal, choices: 0)
    checklist.startingItemNumber = 1
    
    //Create NSParagraphStyle with textual content checklist
    let paragraphStyle = NSMutableParagraphStyle()
    paragraphStyle.textLists = [list]
    
    let attributes = [NSAttributedString.Key.paragraphStyle : paragraphStyle]
    
    //Add attributes to checklist block
    let nsmutableAttributedString = NSMutableAttributedString(string: "Test1nTest2nTest3", attributes: attributes)
    textView.textStorage.setAttributedString(nsmutableAttributedString)
    
    return textView
}

closing class CustomTextView: UITextView {

    // MARK: - Initializer

    public override init(body: CGRect, textContainer: NSTextContainer?) {
        let layoutManager = NSLayoutManager()
        let textStorage = NSTextStorage()
        textStorage.addLayoutManager(layoutManager)
        let newTextContainer = NSTextContainer(measurement: body.measurement)
        layoutManager.addTextContainer(newTextContainer)
        tremendous.init(body: body, textContainer: newTextContainer)
    }

    required init?(coder aDecoder: NSCoder) {
        tremendous.init(coder: aDecoder)
    }
}

enter image description here

does it imply I must manually implement the textList in my layoutManager, or is there a simple method I can get the default implementation of textList of paragraphStyle for UITextStyle

Latest news
Related news

LEAVE A REPLY

Please enter your comment!
Please enter your name here