我有一個帶有超鏈接文本的 UILabel。這是代碼:
let termsText = NSLocalizedString("To continue using this feature, please accept the Terms & Conditions", bundle: Bundle.terms, comment: "Continue")
let linkText = NSLocalizedString("Terms & Conditions", bundle: Bundle.terms, comment: "Terms")
termsLabel.setAttributedTextWithLinks(stringForAttributedText: termsText,
linksText: [linkText], boldFont: false,
underlined: false,
fontSize: 14,
textColor: UIColor(named: "Charcoal",
in: Bundle(for: ViewController.self),
compatibleWith: nil) ?? .black,
linkColor: UIColor(named: "Blue",
in: Bundle(for: ViewController.self),
compatibleWith: nil) ?? .blue,
paragraphStyle: nil) { [weak self] _ in
self?.navigateToTermsAndConditionsVC()
}
這是屬性字串代碼:
func setAttributedTextWithLinks(stringForAttributedText: String, linksText: [String], boldFont: Bool = false, underlined: Bool = false, fontSize: CGFloat = 14.0, textColor: UIColor = UIColor(named: "Carbon", in: Bundle(for: UILabelWithLink.self), compatibleWith: nil) ?? .black, linkColor: UIColor = UIColor(named: "Blue", in: Bundle(for: AlertViewController.self), compatibleWith: nil) ?? .blue, paragraphStyle: NSParagraphStyle? = nil, linkTapped: @escaping (UInt) -> Void) {
font = UIFont.systemFont(ofSize: fontSize, weight: boldFont ? .semibold : .regular)
let textAttributes = [NSAttributedString.Key.foregroundColor: textColor, NSAttributedString.Key.font: font] as [NSAttributedString.Key: Any]
var linkAttributes = [NSAttributedString.Key.foregroundColor: linkColor, NSAttributedString.Key.font: font] as [NSAttributedString.Key: Any]
if underlined {
linkAttributes[NSAttributedString.Key.underlineStyle] = 1
}
setAttributedTextWithLinks(stringForAttributedText: stringForAttributedText, textAttributes: textAttributes, isTextBold: boldFont, linksText: linksText, linkAttributes: linkAttributes, isLinkBold: boldFont, paragraphStyle: paragraphStyle, linkTapped: linkTapped)
}
這是 setAttributedTextWithLinks() 的代碼:
func setAttributedTextWithLinks(stringForAttributedText: String, textAttributes: [NSAttributedString.Key: Any], isTextBold: Bool, linksText: [String], linkAttributes: [NSAttributedString.Key: Any], isLinkBold: Bool, paragraphStyle: NSParagraphStyle?, linkTapped: @escaping (UInt) -> Void) {
self.linkTapped = linkTapped
cachedLinksText = linksText
let nsstringForAttributedText = stringForAttributedText as NSString
var textAttributesVar = textAttributes
var linkAttributesVar = linkAttributes
updateAttributesForAsianCharacters(attributes: &textAttributesVar, isBold: isTextBold)
updateAttributesForAsianCharacters(attributes: &linkAttributesVar, isBold: isLinkBold)
let mutableAttributedText = NSMutableAttributedString(string: stringForAttributedText)
let nonLinkComponents = stringForAttributedText.components(separatedBy: linksText)
for regularText in nonLinkComponents {
let range = nsstringForAttributedText.range(of: regularText)
mutableAttributedText.addAttributes(textAttributesVar, range: range)
}
for linkText in linksText {
let linkRange = nsstringForAttributedText.range(of: linkText)
mutableAttributedText.addAttributes(linkAttributesVar, range: linkRange)
}
if paragraphStyle != nil {
mutableAttributedText.addAttribute(NSAttributedString.Key.paragraphStyle, value: paragraphStyle!, range: nsstringForAttributedText.range(of: stringForAttributedText))
}
attributedText = mutableAttributedText
configureLinkTextForTouch()
configureLayoutManagerTextStorageTextContainer()
}
我在應用程式中更改了語言。問題是這種翻譯適用于使用上述西班牙語和中文代碼的超鏈接,但不適用于法語和德語。有什么我應該專門針對這兩種語言做的嗎?
uj5u.com熱心網友回復:
問題在于:
for linkText in linksText {
let linkRange = nsstringForAttributedText.range(of: linkText)
mutableAttributedText.addAttributes(linkAttributesVar, range: linkRange)
}
linkRange
錯誤時的值是{9223372036854775807, 0}
,即NSNotFound
(對于第一個)。你應該檢查一下。
在您的法語翻譯中,您有"... modalités et conditions ..."
,并且您搜索"Modalités et conditions"
,以大寫開頭,所以找不到它是正常的......
您正在使用range(of:options:)
和省略options
引數,使用默認值。而是使用.caseInsensitive
for options
。范圍。
現在,這種做法有一個流程,如果您有多個鏈接具有相同的單詞,range(of:options:)
將只回傳第一次出現。你需要回圈。
我覺得這項作業相當復雜。如果你有翻譯的手,我會選擇:
"UseThisFeature" = "To continue using this feature, please accept the <link>%@</link>"
"TermAndConditions" = "Terms & Conditions"
之后替換并應用效果會更簡單。&可以是您想要的任何“標記/標簽” <link>
。</link>
最后,我建議UITextView
用于處理標簽。這要容易得多。UILabel
不是為此而生的。請參閱WWDC 2018:TextKit 最佳實踐,幻燈片 2。對于最終用戶來說,男性UITextView
看起來很容易。UILabel
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/528929.html