swift_ActiveLabel文字中tag和Url动态可点击使用

platform :ios,'9.0'
target ‘SwiftDemo’ do
use_frameworks!
inhibit_all_warnings!
pod 'ActiveLabel'

end

文件中引入import ActiveLabel

  let label = ActiveLabel.init(frame: CGRect.init(x: 0, y: 0, width: 300, height: 300))
        label.numberOfLines = 0
        //设置动态标签交互属性,包含主题标签、提及和网址等类型,这些嫩通可被点击
        label.enabledTypes = [.mention,.url,.hashtag]
        label.text = "但是点击烧烤 收看见发件方 几十块的解释道 #hase and @hhdsd. https://www.baidu.com"
        label.textColor = UIColor.red
        //交互事件,当用户点击内容,在控制台输出日志信息
        label.handleURLTap { (hashUrl) in
            print(hashUrl)
        }
        
        label.handleHashtagTap { (hashTag) in
            print("# + \(hashTag)")
        }
        label.handleMentionTap { (mention) in
              print("@" + mention)
        }
        self.view.addSubview(label)
       

 

 

第二种是自己可定义标签类型

    
        let label = ActiveLabel.init(frame: CGRect.init(x: 20, y: 40, width: view.frame.width - 40, height: 300))
        view.addSubview(label)
        //通过正则表达式创建自定义动作类型
        let customType = ActiveType.custom(pattern: "\\sare\\b")
        let customType2 =  ActiveType.custom(pattern: "\\ssupports\\b")
        //d自定义动作类型添加到动态标签所支持的类型表中
        label.enabledTypes.append(customType)
        label.enabledTypes.append(customType2)
        //设置网址最大长度,超出长度将截取网址并在尾部添加省略号
        label.urlMaximumLength = 31
        //对动态标签对象外观属性进行自定义设置
        label.customize { (label) in
            label.text = "This is apost whit #muleiple #hashtags and a @userhandle. links are also sypported link" + "this one : http:ww.coolketang.com . NOw it also supports custom patterns -> are\n\n" + "let's trim a long link :\nhttps://twitter.com/twicket_app/status/63434342352355235224242"
            label.numberOfLines = 0
            //行间距
            label.lineSpacing = 4
            //文字颜色
            label.textColor = UIColor.red
            //主题文字
            label.hashtagColor = UIColor.green
            //提及标签颜色
            label.mentionColor = UIColor.yellow
            //网址文字颜色
            label.URLColor = UIColor.blue
            //网址文字被选中时候颜色
            label.URLSelectedColor = UIColor.red
            
            label.handleMentionTap({ (mention) in
                
            })
            label.handleHashtagTap({ (hashTag) in
                
            })
            label.handleURLTap({ (urlTap) in
               let url = URL.init(string: urlTap.absoluteString)
                UIApplication.shared.open(url!, options: [:], completionHandler: nil)
            })
            //自定义类型设置颜色和选中之后的颜色
            label.customColor[customType] = UIColor.purple
            label.customSelectedColor[customType] = UIColor.green
            //自定义类型设置颜色和选中之后的颜色
            label.customColor[customType2] = UIColor.purple
            label.customSelectedColor[customType2] = UIColor.green
            
            //自定义类型点击方法
            
            label.handleCustomTap(for: customType, handler: { (customType1) in
                
            })
            label.handleCustomTap(for: customType2, handler: { (customType2) in
                
            })
            
            
        }

你可能感兴趣的:(swift,iOS)