如何在swift 4中的UILabel内找到子字符串的框架? [英] How to find the frame of substring inside a UILabel in swift 4?
本文介绍了如何在swift 4中的UILabel内找到子字符串的框架?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个 UILabel,其中包含一些字符串
I have a UILabel which contains some string as
我同意以下条款和条件"
"I agree to below Terms & Condistions"
.
现在点击条款和条件",我想获得它的框架,以便我可以在运行时在该位置添加一个按钮来检测对特定单词的触摸.我不知道如何检测?
Now on click of "Terms & conditions" i want to get it's frame so that i can add a button on that position at run time to detect the touch on particular word. By i am not sure how can i detect ?
推荐答案
我们无法根据您的需要制作 uilabel 可访问的属性,我们可以在此处使用 TextView 来处理此类属性
We can't make a uilabel accessible properties as you want in your case we can make use of TextView here for such property
我的班级:
import UIKit
class TextViewVC: UIViewController {
@IBOutlet weak var textView: UITextView!
let termsAndConditionsURL = "termsandconditions"
override func viewDidLoad() {
super.viewDidLoad()
textView.delegate = self
// Do any additional setup after loading the view.
let str = "I agree to below Terms & Condistions"
let attributedString = NSMutableAttributedString(string: str)
let foundRange = attributedString.mutableString.range(of: "Terms & Condistions")
attributedString.addAttribute(.foregroundColor, value: UIColor.blue, range: foundRange)
attributedString.addAttribute(.underlineStyle , value: NSUnderlineStyle.styleSingle.rawValue, range: foundRange)
attributedString.addAttribute(.link, value: termsAndConditionsURL, range: foundRange)
textView.attributedText = attributedString
}
}
extension TextViewVC : UITextViewDelegate {
func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange) -> Bool
{
if (URL.absoluteString == termsAndConditionsURL)
{
print("Need an action here")
}
else {
print("No")
}
return false
}
}
用于创建 textView 的我的故事板:
My storyBoard for creating a textView :
模拟器输出
控制台输出
这篇关于如何在swift 4中的UILabel内找到子字符串的框架?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文