ios - 将 GestureRecognizer 添加到字符串无法正常工作
问题描述
我有一个Label
,其中我有两个具体words
应该是clickable
. 这是它的外观:
Nutungsbedigungen
并且Datenschutzrichtlinien
都应该是clickable
。我为实现这一目标所做的是:
设置标签:
func setUpDocumentsLabel(){
var textArray = [String]()
var fontArray = [UIFont]()
var colorArray = [UIColor]()
textArray.append("Mit 'fortfahren' akzeptierst du die")
textArray.append("Nutzungsbedingungen")
textArray.append("und")
textArray.append("Datenschutzrichtlinien.")
fontArray.append(Fonts.regularFontWithSize(size: 13.0))
fontArray.append(Fonts.boldFontWithSize(size: 13.0))
fontArray.append(Fonts.regularFontWithSize(size: 13.0))
fontArray.append(Fonts.boldFontWithSize(size: 13.0))
colorArray.append(Colors.white)
colorArray.append(Colors.white)
colorArray.append(Colors.white)
colorArray.append(Colors.white)
self.documentsLabel.attributedText = getAttributedString(arrayText: textArray, arrayColors: colorArray, arrayFonts: fontArray)
self.documentsLabel.isUserInteractionEnabled = true
let tapgesture = UITapGestureRecognizer(target: self, action: #selector(tappedOnLabel(_ :)))
tapgesture.numberOfTapsRequired = 1
self.documentsLabel.addGestureRecognizer(tapgesture)
}
轻拍动作:
@objc func tappedOnLabel(_ gesture: UITapGestureRecognizer) {
guard let text = self.documentsLabel.text else { return }
let nutzen = (text as NSString).range(of: "Nutzungsbedingungen")
let daten = (text as NSString).range(of: "Datenschutzrichtlinien")
if gesture.didTapAttributedTextInLabel(label: self.documentsLabel, inRange: nutzen) {
let alertcontroller = UIAlertController(title: "Tapped on", message: "user tapped on Nutzungsbedingungen", preferredStyle: .alert)
let alertAction = UIAlertAction(title: "OK", style: .default) { (alert) in
}
alertcontroller.addAction(alertAction)
self.present(alertcontroller, animated: true)
} else if gesture.didTapAttributedTextInLabel(label: self.documentsLabel, inRange: daten){
let alertcontroller = UIAlertController(title: "Tapped on", message: "user tapped on Datenschutzrichtlinien", preferredStyle: .alert)
let alertAction = UIAlertAction(title: "OK", style: .default) { (alert) in
}
alertcontroller.addAction(alertAction)
self.present(alertcontroller, animated: true)
}
}
获取属性字符串:
func getAttributedString(arrayText:[String]?, arrayColors:[UIColor]?, arrayFonts:[UIFont]?) -> NSMutableAttributedString {
let finalAttributedString = NSMutableAttributedString()
for i in 0 ..< (arrayText?.count)! {
let attributes = [NSAttributedString.Key.foregroundColor: arrayColors?[i], NSAttributedString.Key.font: arrayFonts?[i]]
let attributedStr = (NSAttributedString.init(string: arrayText?[i] ?? "", attributes: attributes as [NSAttributedString.Key : Any]))
if i != 0 {
finalAttributedString.append(NSAttributedString.init(string: " "))
}
finalAttributedString.append(attributedStr)
}
return finalAttributedString
}
问题:
strings
不是每个clickable
角色都有!该区域因设备而异length
:clickable
对于Datenschutzrichtlinien
:
iPhone 11:clickable
从D - t
iPhone SE:clickable
从D 到 2nd i
对于Nutzungsbedingungen
:
在除 iPhone SE 之外的所有设备上都可以:仅从N - 2nd n
我不知道为什么会发生这种情况,所以如果有人可以在这里帮助我,我非常感激!
更新:
我将其extension
用于UITapGestureRecognizer
:
extension UITapGestureRecognizer {
func didTapAttributedTextInLabel(label: UILabel, inRange targetRange: NSRange) -> Bool {
// Create instances of NSLayoutManager, NSTextContainer and NSTextStorage
let layoutManager = NSLayoutManager()
let textContainer = NSTextContainer(size: CGSize.zero)
let textStorage = NSTextStorage(attributedString: label.attributedText!)
// Configure layoutManager and textStorage
layoutManager.addTextContainer(textContainer)
textStorage.addLayoutManager(layoutManager)
// Configure textContainer
textContainer.lineFragmentPadding = 0.0
textContainer.lineBreakMode = label.lineBreakMode
textContainer.maximumNumberOfLines = label.numberOfLines
let labelSize = label.bounds.size
textContainer.size = labelSize
// Find the tapped character location and compare it to the specified range
let locationOfTouchInLabel = self.location(in: label)
let textBoundingBox = layoutManager.usedRect(for: textContainer)
let textContainerOffset = CGPoint(x: (labelSize.width - textBoundingBox.size.width) * 0.5 - textBoundingBox.origin.x,
y: (labelSize.height - textBoundingBox.size.height) * 0.5 - textBoundingBox.origin.y);
let locationOfTouchInTextContainer = CGPoint(x: locationOfTouchInLabel.x - textContainerOffset.x,
y: locationOfTouchInLabel.y - textContainerOffset.y);
let indexOfCharacter = layoutManager.characterIndex(for: locationOfTouchInTextContainer, in: textContainer, fractionOfDistanceBetweenInsertionPoints: nil)
return NSLocationInRange(indexOfCharacter, targetRange)
}
解决方案
您在这里遇到的具体问题是因为您的标签有
.textAlignment = .center
但didTapAttributedTextInLabel()
代码不知道......它根据“ und Datenschutzrichtlinien ”计算位置。从标签的左边缘开始。
你可以试试这个来修复它 - 快速测试(仅在一台设备上) - 似乎可以完成这项工作。
在您的FirstLaunchViewController
班级中,不要将文档标签居中:
let documentsLabel: UILabel = {
let v = UILabel()
// don't do this
//v.textAlignment = .center
v.numberOfLines = 0
v.translatesAutoresizingMaskIntoConstraints = false
return v
}()
看来您只使用func getAttributedString(...)
了一次,即为 格式化属性文本documentsLabel
,因此将其更改如下:
func getAttributedString(arrayText:[String]?, arrayColors:[UIColor]?, arrayFonts:[UIFont]?) -> NSMutableAttributedString {
let finalAttributedString = NSMutableAttributedString()
for i in 0 ..< (arrayText?.count)! {
let attributes = [NSAttributedString.Key.foregroundColor: arrayColors?[i], NSAttributedString.Key.font: arrayFonts?[i]]
let attributedStr = (NSAttributedString.init(string: arrayText?[i] ?? "", attributes: attributes as [NSAttributedString.Key : Any]))
if i != 0 {
finalAttributedString.append(NSAttributedString.init(string: " "))
}
finalAttributedString.append(attributedStr)
}
// add paragraph attribute
let paragraph = NSMutableParagraphStyle()
paragraph.alignment = .center
let attributes: [NSAttributedString.Key : Any] = [NSAttributedString.Key.paragraphStyle: paragraph]
finalAttributedString.addAttributes(attributes, range: NSRange(location: 0, length: finalAttributedString.length))
return finalAttributedString
}
现在您的didTapAttributedTextInLabel(...)
func 应该检测到相对于“可点击”单词的正确点击位置。
推荐阅读
- java - 如何在Java中创建一个没有年份参数的新日期对象?
- mysql - 将字符串日期更改为日期数据类型
- performance - _CrtIsValidHeapPointer 在 Windows 10 1809 上消耗过多的 cpu,为什么?
- windows - 无法将源添加到我的新 git 存储库
- android - 如何修复 No Agent definition found for id Watson Assistant 错误?
- database - 限制相对于另一列条目的重复条目
- java - 限制对共享资源的访问的设计模式
- c++ - 如何使用opencv在不丢失像素的情况下对图像进行去失真
- c# - 通过 .Net 和 SqlBulkCopy 从 Oracle 到 SQL Server 以极高的精度传输浮点数
- javascript - 功能组件和组件类的区别?