在120个字符后附加readmore标签,并使其在ios中可点击 [英] Append readmore label after 120 char, and make it clickable in ios

查看:90
本文介绍了在120个字符后附加readmore标签,并使其在ios中可点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

数据来自解析器,如果文本超过120个字符,那么它应该像facebook一样追加... ReadMore。我有附加文本的代码,但不知道如何制作可点击链接。我正在使用Swift Languange。

The data is coming from parser, if the text is more than 120 char then it should append "...ReadMore" just like facebook. I have got the code for appending text, but dont know how to make clickable link. I'm using Swift Languange.

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
            }

这篇关于在120个字符后附加readmore标签,并使其在ios中可点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆