jjzjj

ios - [NSConcreteTextStorage 属性 :atIndex:effectiveRange:]: Range or index out of bounds error in NSMutableAttributedString

coder 2024-01-29 原文

我正在尝试创建一个属性字符串,其中在字符串末尾附加了一个链接:

func addMoreAndLessFunctionality (textView:UITextView){
        if textView.text.characters.count >= 120
        {
            let lengthOfString = 255
            var abc : String =  (somelongStringInitiallyAvailable as NSString).substringWithRange(NSRange(location: 0, length: lengthOfString))
            abc += " ...More"
            textView.text = abc
            let attribs = [NSForegroundColorAttributeName: StyleKit.appDescriptionColor, NSFontAttributeName: StyleKit.appDescriptionFont]
            let attributedString: NSMutableAttributedString = NSMutableAttributedString(string: abc, attributes: attribs)


            attributedString.addAttribute(NSLinkAttributeName, value: " ...More", range: NSRange(location:255, length: 8))

          textView.attributedText = attributedString
            textView.textContainer.maximumNumberOfLines = 3;

        }
}

我想在这里实现的是,如果 TextView 文本中的字符超过 255,它应该在 TextView 中显示可点击的“...更多”链接,点击我已经能够获得委托(delegate)“shouldInteractWithUrl”调用,我在其中增加了 TextView 中的行数,并将链接文本更改为“...更少”。点击 Less 我再次调用相同的方法,以便它可以再次截断。 :

    func textView(textView: UITextView, shouldInteractWithURL URL: NSURL, inRange characterRange: NSRange) -> Bool
{
    print("textview should interact with URl")

    let textTapped = textView.text[textView.text.startIndex.advancedBy(characterRange.location)..<textView.text.endIndex]

    if textTapped == " ...More"{

        var abc : String =  (self.contentDetailItemManaged?.whatsnewDesc)!
        abc += " ...Less"
        textView.textContainer.maximumNumberOfLines = 0
        let attribs = [NSForegroundColorAttributeName: StyleKit.appDescriptionColor, NSFontAttributeName: StyleKit.appDescriptionFont]
        let attributedString: NSMutableAttributedString = NSMutableAttributedString(string: abc, attributes: attribs)
        attributedString.addAttribute(NSLinkAttributeName, value: " ...Less", range: NSRange(location:abc.characters.count-8 , length: 8))

        textView.attributedText = attributedString


    }
    else if textTapped == " ...Less"{

        textView.attributedText = nil
        textView.text = somelongStringInitiallyAvailable
        self.addMoreAndLessFunctionality(textView)
    }

    return true
}

现在的问题是,当第一次调用第一个方法时(它是在我设置了 textview 的文本之后调用的),它工作正常,但是在点击“...更多”之后,textview 正常扩展,“...更多”更改为“...更少”。当点击“...Less”时,它会崩溃并发生异常:

[NSConcreteTextStorage attribute:atIndex:effectiveRange:]: Range or index out of bounds'

任何帮助将不胜感激。 (另外,字符串“...More”的开头有一个空格,所以总共有 8 个字符,我可能在输入上面的代码时漏掉了这个空格)

谢谢:)

最佳答案

func textView(textView: UITextView, shouldInteractWithURL URL: NSURL, inRange characterRange: NSRange) -> Bool

中返回 false

关于ios - [NSConcreteTextStorage 属性 :atIndex:effectiveRange:]: Range or index out of bounds error in NSMutableAttributedString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39087445/

有关ios - [NSConcreteTextStorage 属性 :atIndex:effectiveRange:]: Range or index out of bounds error in NSMutableAttributedString的更多相关文章

随机推荐