【问题标题】:[NSConcreteTextStorage attribute:atIndex:effectiveRange:]: Range or index out of bounds error in NSMutableAttributedString[NSConcreteTextStorage 属性:atIndex:effectiveRange:]:NSMutableAttributedString 中的范围或索引越界错误
【发布时间】:2016-08-22 19:39:04
【问题描述】:

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

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;

        }
}

我在这里想要实现的是,如果文本视图的文本中的字符超过 255 个,它应该在文本视图中显示可点击的“...更多”链接,点击时我已经能够获得委托“调用 shouldInteractWithUrl”,我正在增加文本视图中的行数,并将链接文本更改为“...Less”。在点击 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个字符,我可能在输入上面的代码时错过了这个空格)

谢谢:)

【问题讨论】:

  • 您应该停止使用text 属性并仅使用UITextView 对象的attributedText 属性。阿苏,textTapped,你为什么不只检查网址?
  • @Larme 如果我不使用 text 属性会有什么不同,你能详细说明一下吗?关于网址,我错过了明显的,谢谢提醒。
  • 没用,因为之后你设置了attributedText。看看那里 (stackoverflow.com/questions/36064762/…),例如与混合文本和属性文本相关的一些问题。

标签: ios swift ios8 nsattributedstring


【解决方案1】:

return false in func textView(textView: UITextView, shouldInteractWithURL URL: NSURL, inRange characterRange: NSRange) -&gt; Bool

【讨论】:

  • 欢迎来到 StackOverflow。虽然此代码可能会回答问题,但提供有关此代码为何和/或如何回答问题的额外上下文可提高其长期价值。 How to Answer
猜你喜欢
  • 2014-06-06
  • 2015-03-22
  • 2022-08-08
  • 2013-10-13
  • 2015-01-10
  • 2014-01-27
  • 1970-01-01
相关资源
最近更新 更多