swift 如何在标签内嵌入自定义按钮?
How to embed a custom button within a label in swift?
我创建了一个自定义信息按钮,我想将其放入常规 UILabel 中。
这个想法是让屏幕显示 "Tap the (BUTTON HERE) for more information"。有没有办法在不创建两个 UILabel 的情况下做到这一点?如果创建 2 个标签是唯一的方法,我怎样才能将所有内容放在一行中?
我尝试在 label.text 中执行 (button) 操作,但这显示了按钮的属性而不是放置按钮。我也试过 label.addSubview(button) 可以工作但是在错误的地方添加了按钮。
最好的方法是使用带有 NSAttributedString 的 UITextView,其中一个属性是您的 link。
let textView = UITextView()
textView.delegate = self
// These allow the link to be tapped
textView.isEditable = false
textView.isScrollEnabled = false
// Removes padding that UITextView uses, making it look more like a UILabel
textView.textContainer.lineFragmentPadding = 0.0
textView.textContainerInset = .zero
然后对于 NSAttributedString
let text = "Tap HERE for more information"
let linkText = "HERE"
// Get range for tappable section
let linkRange = (text as NSString).range(of: linkText)
// Styling
let attributes: [NSAttributedString.Key: Any] = [
.foregroundColor: UIColor.black
]
// Actual Link!
let linkTextAttributes: [NSAttributedString.Key: Any] = [
.underlineStyle: NSUnderlineStyle.single.rawValue,
.link: "https://www.example.com" //The link you want to link to
]
let attributedString = NSMutableAttributedString(string: text, attributes: attributes)
attributedString.addAttributes(linkTextAttributes, range: linkRange)
然后使用这些 UITextView 委托函数
// Removes a lot of the actions when user selects text
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
return false
}
// Handle the user tapping the link however you like here
func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
viewModel.urlTapped(URL)
return false
}
我创建了一个自定义信息按钮,我想将其放入常规 UILabel 中。 这个想法是让屏幕显示 "Tap the (BUTTON HERE) for more information"。有没有办法在不创建两个 UILabel 的情况下做到这一点?如果创建 2 个标签是唯一的方法,我怎样才能将所有内容放在一行中?
我尝试在 label.text 中执行 (button) 操作,但这显示了按钮的属性而不是放置按钮。我也试过 label.addSubview(button) 可以工作但是在错误的地方添加了按钮。
最好的方法是使用带有 NSAttributedString 的 UITextView,其中一个属性是您的 link。
let textView = UITextView()
textView.delegate = self
// These allow the link to be tapped
textView.isEditable = false
textView.isScrollEnabled = false
// Removes padding that UITextView uses, making it look more like a UILabel
textView.textContainer.lineFragmentPadding = 0.0
textView.textContainerInset = .zero
然后对于 NSAttributedString
let text = "Tap HERE for more information"
let linkText = "HERE"
// Get range for tappable section
let linkRange = (text as NSString).range(of: linkText)
// Styling
let attributes: [NSAttributedString.Key: Any] = [
.foregroundColor: UIColor.black
]
// Actual Link!
let linkTextAttributes: [NSAttributedString.Key: Any] = [
.underlineStyle: NSUnderlineStyle.single.rawValue,
.link: "https://www.example.com" //The link you want to link to
]
let attributedString = NSMutableAttributedString(string: text, attributes: attributes)
attributedString.addAttributes(linkTextAttributes, range: linkRange)
然后使用这些 UITextView 委托函数
// Removes a lot of the actions when user selects text
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
return false
}
// Handle the user tapping the link however you like here
func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
viewModel.urlTapped(URL)
return false
}