首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在NSTextStorage上撞击靶场

在NSTextStorage上撞击靶场
EN

Stack Overflow用户
提问于 2019-03-21 11:19:26
回答 1查看 845关注 0票数 2

我有一个UITextView,它应该接收来自HTML的NSAttributesString。最终的结果是黑色文本与自定义下划线的链接。我要撞到靶场了。

由于非正常异常“NSRangeException”终止应用程序,原因:“NSMutableRLEArray objectAtIndex:effectiveRange::bounds”

以下是代码:

代码语言:javascript
运行
复制
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()
        }
    }
}

当我评论这一行时:textView.text = text

取消注释,所有崩溃:textView.attributedText = htmlStyleAttributeText(text: text)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-03-21 18:43:01

代码语言:javascript
运行
复制
let rg = NSRange(text.startIndex..., in: text)

=>

代码语言:javascript
运行
复制
let rg = NSRange(text.startIndex..., in: textView.attributedText!.string)

因为第一个返回{0,87},第二个返回{0,50}。这是正常的,"html标记“已经被解释并从最后的字符串中移除以显示。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55279279

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档