【发布时间】:2020-04-29 13:15:15
【问题描述】:
我有一个Label,其中有两个特定的words,应该是clickable。看起来是这样的:
Nutungsbedigungen 和 Datenschutzrichtlinien 都应该是 clickable。我为实现这一目标所做的是:
setUpLabel:
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)
}
tappedAction:
@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)
}
}
getAttributedString:
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! clickable 区域的length 因设备而异:
对于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)
}
【问题讨论】:
-
什么是
gesture.didTapAttributedTextInLabel-- 是不是来自这个:stackoverflow.com/questions/1256887/… -- 你应该向我们展示你的确切功能 -
@LouFranco 抱歉。是的,就是这样
-
关于这个问题有几种实现方式——您使用的是哪一种。另外,请注意,评论显示有许多边缘情况不起作用。
-
我强烈建议改用
UITextView。这会容易得多(Apple 推荐):developer.apple.com/videos/play/wwdc2018/221/?time=152 -
@Chris - 您在这里遇到的特定 问题是因为您的标签有
.textAlignment = .center...但didTapAttributedTextInLabel()代码不知道...它根据 "und Datenschutzrichtlinien." 从标签的左边缘开始计算位置。
标签: ios swift string label nsattributedstring