iOS 富文本初探

有的时候啊,咱们须要在一行或者多行文本中显示不一样颜色,不一样字号的文字,甚至于有的须要点击,有的不须要。这统称为富文本。
git

在网页中,有不少相似的应用。除开网页,我如今遇到的这种状况也是非用不可,用户政策和用户协议在多语言的实现中,考虑到自适应,就必须显示在同一个控件中(UILabel/UITextView).github

NSMutableAttributedString/NSAttributedString用来表示富文本。app

不如咱们有一段文本,中间有两段是要求不一样颜色显示,能够点击的,由于是国际化,每一段的长度都不同,因此索性所有拆开了。url

var attrDic1 = [NSFontAttributeName: UIFont.systemFontOfSize(15)]spa

        var str1 = NSLocalizedString("register prompt first segment", comment: "register prompt first segment").net

        var attrDic2 = [NSFontAttributeName: UIFont.systemFontOfSize(15), NSForegroundColorAttributeName: UIColor(red: 59/255, green: 126/255, blue: 55/255, alpha: 1), NSLinkAttributeName:"http://www.baidu.com"]orm

        var str2 = NSLocalizedString("register prompt second segment", comment: "register prompt second segment")blog

        var attrDic3 = [NSFontAttributeName: UIFont.systemFontOfSize(15)]three

        var str3 = NSLocalizedString("register prompt three segment", comment: "register prompt three segment")get

        var attrDic4 = [NSFontAttributeName: UIFont.systemFontOfSize(15), NSForegroundColorAttributeName: UIColor(red: 59/255, green: 126/255, blue: 55/255, alpha: 1), NSLinkAttributeName:"http://www.baidu.com"]

        var str4 = NSLocalizedString("register prompt four segment", comment: "register prompt four segment")

        var attrDic5 = [NSFontAttributeName: UIFont.systemFontOfSize(15)]

        var str5 = NSLocalizedString("register prompt five segment", comment: "register prompt five segment")

        

        var allStr = str1+str2+str3+str4+str5

        var attrStr1 = NSMutableAttributedString(string: str1, attributes: attrDic1)

        var attrStr2 = NSMutableAttributedString(string: str2, attributes: attrDic2)

        var attrStr3 = NSMutableAttributedString(string: str3, attributes: attrDic3)

        var attrStr4 = NSMutableAttributedString(string: str4, attributes: attrDic4)

        var attrStr5 = NSMutableAttributedString(string: str5, attributes: attrDic5)

        

        attrStr1.appendAttributedString(attrStr2);

        attrStr1.appendAttributedString(attrStr3);

        attrStr1.appendAttributedString(attrStr4);

        attrStr1.appendAttributedString(attrStr5);

        vPromptTextView.attributedText = attrStr1

//        vPromptTextView.linkTextAttributes = attrDic2

//        vPromptTextView.userInteractionEnabled = true

//        vPromptTextView.scrollEnabled = false

//        vPromptTextView.editable = false

//        vPromptTextView.selectable = true

//        vPromptTextView.textContainer.lineFragmentPadding = 0

//        vPromptTextView.textContainerInset = UIEdgeInsetsMake(0, 0, 0, 0)

//        vPromptTextView.delegate = self;

        vPromptTextView.textAlignment = NSTextAlignment.Center

//        vPromptTextView.dataDetectorTypes = UIDataDetectorTypes.All

        var tap = UITapGestureRecognizer(target: self, action: "tappedTextView:")

        vPromptTextView.addGestureRecognizer(tap)

这里是5段内容,使用了国际化,UILabel说是不能点击,事实上,我把全部的注释都打开,同时实现了了UITextView的委托,也没办法点击,好吧,我不知道问题在哪里。据网上搜索到的内容讲,能够点击以后,也就是用safiri打开一段连接,这样的话和个人实际应用场景有差异,因此使用了tapgesture的方式,看看具体实现:

    func tappedTextView(tapGesture:UITapGestureRecognizer){

        if tapGesture.state != UIGestureRecognizerState.Ended {

            return

        }

        

        var textView = tapGesture.view as UITextView

        var tapLocation = tapGesture.locationInView(textView)

        var textPosition = textView.closestPositionToPoint(tapLocation)

        

        var attributes = textView.textStylingAtPosition(textPosition, inDirection: UITextStorageDirection.Backward)

        if let url = attributes[NSLinkAttributeName] as? String{

//            UIApplication.sharedApplication().openURL(NSURL(string: url)!)

            link = url

            self.performSegueWithIdentifier("openLink", sender: self)

        }

    }


这样就能够实现我本身想要的内容的,也能够实现内部点击。

图文混排下一步就是core text 渲染,这个内容仍是很丰富的。

iOS7 新增了TextKit,是在Core Text之上进行了封装。

iOS的内容是至关丰富的,是在不敢称iOS高手了,学的越多,发现不知道的东西越多。

最后,推荐两个第三方库


两个库:DTCoreText=> http://blog.cnbang.net/tech/2630/

TTTAttributedLabel=> https://github.com/TTTAttributedLabel/TTTAttributedLabel

相关文章
相关标签/搜索