在120个字符之后附加readmore标签,并使其在ios中可点击

数据来自parsing器,如果文本超过120个字符,那么它应该追加“… ReadMore”,就像Facebook一样。 我已经得到了附加文本的代码,但不知道如何使可点击的链接。 我正在使用Swift语言。

if cell!.bhikmangaTextlbl!.text!.utf16Count >= 120 { var abc : String = (cell!.bhikmangaTextlbl!.text! as NSString).substringWithRange(NSRange(location: 0, length: 120)) abc += " ...ReadMore" cell!.bhikmangaTextlbl!.text = abc } 

 @Mihir Mehta, This is code i have implemented. var bhikmangaTextlbl:UITextView? if cell!.bhikmangaTextlbl!.text!.utf16Count >= 120 { var abc : String = (cell!.bhikmangaTextlbl!.text! as NSString).substringWithRange(NSRange(location: 0, length: 120)) abc += "...ReadMore" cell!.bhikmangaTextlbl!.text = abc var attribs = [NSForegroundColorAttributeName: UIColor.blackColor(), NSFontAttributeName: UIFont.systemFontOfSize(14.0)] var attributedString: NSMutableAttributedString = NSMutableAttributedString(string: abc, attributes: attribs) attributedString.addAttribute(NSLinkAttributeName, value: "...ReadMore", range: NSRange(location: 120, length: 11)) attributedString.addAttribute(NSUnderlineStyleAttributeName, value: NSUnderlineStyle.StyleSingle.rawValue, range: NSRange(location: 120, length: 11)) cell!.bhikmangaTextlbl!.attributedText = attributedString } //The function you said i have written here func textView(textView: UITextView, shouldInteractWithURL URL: NSURL, inRange characterRange: NSRange) -> Bool { return true } 

你可以在这里使用属性string…并使文本的某些部分可点击

这里是Objective C代码。 你可以为Swift写同样的东西

 NSDictionary *attribs = @{ NSForegroundColorAttributeName: BODY_FONT_COLOR, NSFontAttributeName: [UIFont fontWithName:FontHelvetica size:AppFont16] }; NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:ACCIDENT_DETECTION_AUTOMATIC_TEXT attributes:attribs]; [attributedString addAttribute:NSLinkAttributeName value:@"Accident detection" range:[[attributedString string] rangeOfString:@"Read More"]]; NSDictionary *linkAttributes = @{NSForegroundColorAttributeName: LINKS_FONT_COLOR, NSUnderlineColorAttributeName: [UIColor clearColor], NSUnderlineStyleAttributeName: @(NSUnderlineStyleNone)}; // assume that textView is a UITextView previously created (either by code or Interface Builder) self.accidentDetectionText.linkTextAttributes = linkAttributes; // customizes the appearance of links self.accidentDetectionText.attributedText = attributedString; 

那么我宁愿添加一个额外的标题“阅读更多”,并添加点击手势扩展第一个标签。