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

58 篇文章 0 订阅

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
                
            })
            
            
        }

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值