Files
real-e-party-iOS/YuMi/E-P/NewLogin/Views/EPPolicyLabel.swift
edwinQQQ 90360448a1 fix: 统一应用名称为 "E-Party" 并更新相关描述
主要变更:
1. 在 Info.plist 中将应用名称和描述中的 "E-Parti" 替换为 "E-Party"。
2. 更新多个本地化字符串和提示信息,确保一致性。
3. 修改部分代码中的错误提示信息,使用本地化字符串替代硬编码文本。

此更新旨在提升品牌一致性,确保用户在使用过程中获得统一的体验。
2025-10-15 19:11:01 +08:00

152 lines
6.1 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// EPPolicyLabel.swift
// YuMi
//
// Created by AI on 2025-01-27.
//
import UIKit
class EPPolicyLabel: UILabel {
// MARK: - Properties
var onUserAgreementTapped: (() -> Void)?
var onPrivacyPolicyTapped: (() -> Void)?
// MARK: - Initialization
override init(frame: CGRect) {
super.init(frame: frame)
setup()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
setup()
}
// MARK: - Setup
private func setup() {
numberOfLines = 0
isUserInteractionEnabled = true
// 使 YMLocalizedString
let fullText = YMLocalizedString("XPLoginViewController6")
let userAgreementText = YMLocalizedString("XPLoginViewController7")
let privacyPolicyText = YMLocalizedString("XPLoginViewController9")
let attributedString = NSMutableAttributedString(string: fullText)
attributedString.addAttribute(NSAttributedString.Key.foregroundColor,
value: UIColor.darkGray,
range: NSRange(location: 0, length: fullText.count))
attributedString.addAttribute(NSAttributedString.Key.font,
value: UIFont.systemFont(ofSize: 12),
range: NSRange(location: 0, length: fullText.count))
//
if let userRange = fullText.range(of: userAgreementText) {
let nsRange = NSRange(userRange, in: fullText)
attributedString.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.systemBlue, range: nsRange)
attributedString.addAttribute(NSAttributedString.Key.underlineStyle, value: NSUnderlineStyle.single.rawValue, range: nsRange)
}
//
if let privacyRange = fullText.range(of: privacyPolicyText) {
let nsRange = NSRange(privacyRange, in: fullText)
attributedString.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.systemBlue, range: nsRange)
attributedString.addAttribute(NSAttributedString.Key.underlineStyle, value: NSUnderlineStyle.single.rawValue, range: nsRange)
}
attributedText = attributedString
//
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTap(_:)))
addGestureRecognizer(tapGesture)
}
// MARK: - Actions
@objc private func handleTap(_ gesture: UITapGestureRecognizer) {
guard let attributedText = self.attributedText else {
print("[EPPolicyLabel] No attributed text")
return
}
let text = attributedText.string
let userAgreementText = YMLocalizedString("XPLoginViewController7")
let privacyPolicyText = YMLocalizedString("XPLoginViewController9")
print("[EPPolicyLabel] Tap detected, text: \(text)")
let layoutManager = NSLayoutManager()
let textContainer = NSTextContainer(size: bounds.size)
let textStorage = NSTextStorage(attributedString: attributedText)
layoutManager.addTextContainer(textContainer)
textStorage.addLayoutManager(layoutManager)
textContainer.lineFragmentPadding = 0
textContainer.maximumNumberOfLines = numberOfLines
textContainer.lineBreakMode = lineBreakMode
let locationOfTouchInLabel = gesture.location(in: self)
let textBoundingBox = layoutManager.usedRect(for: textContainer)
// textAlignment
var textContainerOffset = CGPoint.zero
switch textAlignment {
case .left, .natural, .justified:
textContainerOffset = CGPoint(x: 0, y: (bounds.height - textBoundingBox.height) / 2)
case .center:
textContainerOffset = CGPoint(x: (bounds.width - textBoundingBox.width) / 2,
y: (bounds.height - textBoundingBox.height) / 2)
case .right:
textContainerOffset = CGPoint(x: bounds.width - textBoundingBox.width,
y: (bounds.height - textBoundingBox.height) / 2)
@unknown default:
textContainerOffset = CGPoint(x: 0, y: (bounds.height - textBoundingBox.height) / 2)
}
let locationOfTouchInTextContainer = CGPoint(x: locationOfTouchInLabel.x - textContainerOffset.x,
y: locationOfTouchInLabel.y - textContainerOffset.y)
//
guard textBoundingBox.contains(locationOfTouchInTextContainer) else {
print("[EPPolicyLabel] Tap outside text bounds")
return
}
let indexOfCharacter = layoutManager.characterIndex(for: locationOfTouchInTextContainer,
in: textContainer,
fractionOfDistanceBetweenInsertionPoints: nil)
print("[EPPolicyLabel] Character index: \(indexOfCharacter)")
//
if let userRange = text.range(of: userAgreementText) {
let nsRange = NSRange(userRange, in: text)
print("[EPPolicyLabel] User agreement range: \(nsRange)")
if NSLocationInRange(indexOfCharacter, nsRange) {
print("[EPPolicyLabel] User agreement tapped!")
onUserAgreementTapped?()
return
}
}
if let privacyRange = text.range(of: privacyPolicyText) {
let nsRange = NSRange(privacyRange, in: text)
print("[EPPolicyLabel] Privacy policy range: \(nsRange)")
if NSLocationInRange(indexOfCharacter, nsRange) {
print("[EPPolicyLabel] Privacy policy tapped!")
onPrivacyPolicyTapped?()
return
}
}
print("[EPPolicyLabel] No link tapped")
}
}