我正在尝试创建一个属性字符串,其中在字符串的末尾附加了一个链接:
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 I再次调用相同的方法,以便它可以再次截断。 :
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文本之后调用它),它工作正常,但点击&#34; ...更多& #34 ;, textview正常扩展,&#34; ...更多&#34;改变为&#34; ...少&#34 ;.当&#34; ...较少&#34;被点击,它崩溃并发生异常:
[NSConcreteTextStorage attribute:atIndex:effectiveRange:]: Range or index out of bounds'
任何帮助将不胜感激。 (也是字符串&#34; ...更多&#34;开头有一个空格,所以共有8个字符,我可能在输入代码时错过了这个空格)
谢谢:)
答案 0 :(得分:0)
.then(course => {/* check if the course is null and then throws an error */} )
在return false