简体   繁体   English

在NSTextStorage上的范围崩溃

[英]Crashing on range on NSTextStorage

I have a UITextView that should receive NSAttributesString contracted from HTML. 我有一个UITextView应该接收从HTML收缩的NSAttributesString。 The end result is black text with custom underline for links. 最终结果是带有自定义下划线链接的黑色文本。 I am crashing on range. 我在射程上崩溃了。

Terminating app due to uncaught exception 'NSRangeException', reason: 'NSMutableRLEArray objectAtIndex:effectiveRange:: Out of bounds' 因未捕获的异常'NSRangeException'而终止应用程序,原因:'NSMutableRLEArray objectAtIndex:effectiveRange :: out of bounds'

Here is the code: 这是代码:

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let text = "random text <a href='http://www.google.com'>http://www.google.com </a> more random text"

        let storage = NSTextStorage()
        let layout = UnderlineLayout()
        storage.addLayoutManager(layout)
        let container = NSTextContainer()
        layout.addTextContainer(container)

        let textView = UITextView(frame: CGRect(x: 30, y: 380, width: 300, height: 200), textContainer: container)
        textView.isUserInteractionEnabled = true
        textView.isEditable = false
        textView.textContainerInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        textView.text = text
//        textView.attributedText = htmlStyleAttributeText(text: text)
        textView.backgroundColor = UIColor.white
        textView.textColor = UIColor.black

        let underLineColor: UIColor = UIColor(red: 245/255, green: 190/255, blue: 166/255, alpha: 1)


        let attributes = [NSAttributedString.Key.underlineStyle.rawValue: 0x15,
                          NSAttributedString.Key.underlineColor: underLineColor,
                          NSAttributedString.Key.font: UIFont.systemFont(ofSize: 25),
                          NSAttributedString.Key.baselineOffset:0] as! [NSAttributedString.Key : Any]

        let rg = NSRange(text.startIndex..., in: text)

        storage.addAttributes(attributes, range: rg)
        view.addSubview(textView)
    }

    public func htmlStyleAttributeText(text: String) -> NSMutableAttributedString? {

        if let htmlData = text.data(using: .utf8) {

            let options: [NSAttributedString.DocumentReadingOptionKey: Any] = [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html, NSAttributedString.DocumentReadingOptionKey.characterEncoding: String.Encoding.utf8.rawValue]
            let attributedString = try? NSMutableAttributedString(data: htmlData, options: options, documentAttributes: nil)

            return attributedString
        }
        return nil
    }
}

import UIKit

class UnderlineLayout: NSLayoutManager {
    override func drawUnderline(forGlyphRange glyphRange: NSRange, underlineType underlineVal: NSUnderlineStyle, baselineOffset: CGFloat, lineFragmentRect lineRect: CGRect, lineFragmentGlyphRange lineGlyphRange: NSRange, containerOrigin: CGPoint) {
        if let container = textContainer(forGlyphAt: glyphRange.location, effectiveRange: nil) {
            let boundingRect = self.boundingRect(forGlyphRange: glyphRange, in: container)
            let offsetRect = boundingRect.offsetBy(dx: containerOrigin.x, dy: containerOrigin.y)

            let left = offsetRect.minX
            let bottom = offsetRect.maxY
            let width = offsetRect.width
            let path = UIBezierPath()
            path.lineWidth = 4
            path.move(to: CGPoint(x: left, y: bottom))
            path.addLine(to: CGPoint(x: left + width, y: bottom))
            path.stroke()
        }
    }
}

When I comment this line: textView.text = text 当我评论这一行时: textView.text = text

And uncomment this, all crashes: textView.attributedText = htmlStyleAttributeText(text: text) 并取消注释,所有崩溃: textView.attributedText = htmlStyleAttributeText(text: text)

let rg = NSRange(text.startIndex..., in: text)

=> =>

let rg = NSRange(text.startIndex..., in: textView.attributedText!.string)

Because the first one returns {0,87} and the second one {0,50}. 因为第一个返回{0,87}而第二个返回{0,50}。 That's normal, the "html tags" have been interpreted and remove from the final string to show. 这是正常的,“html标签”已被解释并从最终字符串中删除以显示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM