標籤:協議 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簡單實現一個常規條款、免責聲明文字+帶有連結的展示形式