Swift簡單實現一個常規條款、免責聲明文字+帶有連結的展示形式

來源:互聯網
上載者:User

標籤:協議   AC   tab   scroll   ges   http   tableview   isp   animate   

效果: IMG_F08DABE063A6-1.jpeg
class DisclamerView: UIView {     //@objc weak var vc:UIViewController?     //自訂協議    @IBInspectable var diy_protocol:String = "diyprotocol"    //超連結位址    @IBInspectable var disclamerURLStr:String = " "    //條款、免責聲明標題文字    @IBInspectable var infoStr = str_disclaimer    //連結地址描述    @IBInspectable var linkStr = ""    //展示文字的大小(用於判斷展示地區大小)    @IBInspectable var font = UIFont.systemFont(ofSize: 13)        lazy var infoTextView:UITextView = {        let tv = UITextView()        tv.delegate = self        tv.isEditable = false        tv.backgroundColor = UIColor.clear        tv.isScrollEnabled = false        //設定頁面邊界上邊距10,左右邊距各10,底邊距0  上,右,下,左        tv.textContainerInset = UIEdgeInsetsMake(10, 0, 0, -5);        self.addSubview(tv)        return tv    }()        override func awakeFromNib() {        super.awakeFromNib()        self.backgroundColor = UIColor.groupTableViewBackground    }        override func draw(_ rect: CGRect) {        // Drawing code        let attri = NSMutableAttributedString(attributedString: NSAttributedString(string: infoStr + linkStr,                                                                                   attributes:[NSAttributedStringKey.foregroundColor : UIColor.darkGray,                                                                                               NSAttributedStringKey.font : font]))        if linkStr != ""{            attri.addAttributes([NSAttributedStringKey.link:(diy_protocol+"://")],                                range: ((attri.string) as NSString).range(of: linkStr))        }        infoTextView.attributedText = attri        //左右兩天預留5個像素        infoTextView.frame = CGRect(x: 5, y: 0, width: rect.width - 5*2, height: rect.height)    }}extension DisclamerView:UITextViewDelegate{    //textView裡帶有超連結的監聽代理    func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange) -> Bool {        //判斷超連結協議        if let sch = URL.scheme,sch == diy_protocol {            //let ndvc = SFNewsDetailVC(nibName: "SFNewsDetailVC", bundle: nil)           // ndvc.requstURL = disclamerURLStr;            //if let vc = vc as? UINavigationController{            //    vc.pushViewController(ndvc, animated: true)           // }else{            //    ndvc.isPresent = true            //    vc?.present(ndvc, animated: true, completion: nil)           /// }            return true        }        return false    }}
XIB使用:
self.disclamerView.linkStr = @"點擊查看詳情"; // CGFloat dh = [STools getDisclaimerStrContentHeightWithDSize:CGSizeMake(SWIDTH - 5*2, 10000) dFont:self.disclamerView.font] + 18;    self.h_disclamerView.constant = dh;
 image.png image.png純程式碼使用
    lazy var disclamerView:DisclamerView = {        let dv = DisclamerView()        dv.backgroundColor = color_background        let dh = STools.getDisclaimerStrContentHeight(dFont: dv.font) + 18        dv.frame = CGRect(x: 0, y: 0,width: swidth, height: dh)        dv.vc = self        return dv    }()   self.tableView.tableFooterView = disclamerView

Swift簡單實現一個常規條款、免責聲明文字+帶有連結的展示形式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.