Manos Chorianopoulos

couponVC detailsLabel fix

......@@ -98,8 +98,6 @@ import UIKit
infoLabel.textColor = UIColor(rgb: 0x020E1C)
infoLabel.text = "Περισσότερα"
setupExpandableDetails()
couponCodeContainerView.backgroundColor = UIColor(rgb: 0xFFFFFF)
couponCodeContainerView.layer.cornerRadius = 8.0
couponCodeContainerView.clipsToBounds = true
......@@ -157,6 +155,8 @@ import UIKit
// expirationLabel.text = ("Η προσφορά ισχύει " + coupon.expirationDate)
expirationLabel.text = "Η προσφορά ισχύει έως 30-09-2025"
setupExpandableDetails()
couponCodeTitleLabel.font = UIFont(name: "PingLCG-Regular", size: 16)
couponCodeTitleLabel.textColor = UIColor(rgb: 0x000F1E)
couponCodeTitleLabel.text = "Κωδικός Κουπονιού"
......@@ -174,25 +174,103 @@ import UIKit
termsLabel.text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas sed ex euismod, feugiat justo eu, faucibus urna. Nulla sodales euismod arcu volutpat finibus. Etiam id urna at justo facilisis tempor. Morbi dignissim erat vitae magna sodales dignissim ac in mauris. Mauris tempor convallis tortor, interdum hendrerit turpis eleifend at. Praesent."
}
private var fullDetailsText = ""
private var shouldTruncaitDetails = false
private func setupExpandableDetails() {
let fullText = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas sed ex euismod, feugiat justo eu, faucibus urna. Nulla sodales euismod arcu volutpat finibus. Etiam id urna at justo facilisis tempor. Morbi dignissim erat vitae magna sodales dignissim ac in mauris. Mauris tempor convallis tortor, interdum hendrerit turpis eleifend at. Praesent."
fullDetailsText = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas sed ex euismod, feugiat justo eu, faucibus urna. Nulla sodales euismod arcu volutpat finibus. Etiam id urna at justo facilisis tempor. Morbi dignissim erat vitae magna sodales dignissim ac in mauris. Mauris tempor convallis tortor, interdum hendrerit turpis eleifend at. Praesent."
detailsLabel.text = fullText
detailsLabel.numberOfLines = 3
detailsLabel.font = UIFont(name: "PingLCG-Regular", size: 18)
detailsLabel.textColor = UIColor(rgb: 0x020E1C)
// Add tap gesture
updateDetailsText()
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(toggleDetails))
detailsLabel.isUserInteractionEnabled = true
detailsLabel.addGestureRecognizer(tapGesture)
}
private func updateDetailsText() {
if isDetailsExpanded {
// Show full text with "Λιγότερα"
if (shouldTruncaitDetails) {
let fullTextWithLess = fullDetailsText + " Λιγότερα"
let attributedString = createAttributedString(text: fullTextWithLess, linkText: "Λιγότερα")
detailsLabel.attributedText = attributedString
} else {
detailsLabel.text = fullDetailsText
}
detailsLabel.numberOfLines = 0
} else {
// Calculate approximate characters for 4 lines and truncate if needed
let truncatedText = getTruncatedTextForFourLines()
let attributedString = createAttributedString(text: truncatedText, linkText: "Περισσότερα")
detailsLabel.attributedText = attributedString
detailsLabel.numberOfLines = 4
}
}
private func getTruncatedTextForFourLines() -> String {
// Calculate approximate characters that fit in 4 lines
let labelWidth = detailsLabel.frame.width
let fontSize: CGFloat = 18
let averageCharWidth: CGFloat = fontSize * 0.5 // Rough estimate for character width
let charactersPerLine = Int(labelWidth / averageCharWidth)
let maxCharactersFor4Lines = charactersPerLine * 4
let moreText = " Περισσότερα"
let ellipsis = "..."
let reservedCharacters = ellipsis.count + moreText.count + 15
// If text is short enough, don't truncate
if fullDetailsText.count <= maxCharactersFor4Lines {
shouldTruncaitDetails = false
return fullDetailsText
} else {
shouldTruncaitDetails = true
}
// Calculate how many characters we can use from original text
let availableCharacters = maxCharactersFor4Lines - reservedCharacters
// Truncate text and add ellipsis + more
let truncatedText = String(fullDetailsText.prefix(availableCharacters))
return truncatedText + ellipsis + moreText
}
private func createAttributedString(text: String, linkText: String) -> NSAttributedString {
let attributedString = NSMutableAttributedString(string: text)
// Regular text attributes
let regularAttributes: [NSAttributedString.Key: Any] = [
.font: UIFont(name: "PingLCG-Regular", size: 18) ?? UIFont.systemFont(ofSize: 18),
.foregroundColor: UIColor(rgb: 0x020E1C)
]
// Link text attributes (blue color)
let linkAttributes: [NSAttributedString.Key: Any] = [
.font: UIFont(name: "PingLCG-Regular", size: 18) ?? UIFont.systemFont(ofSize: 18),
.foregroundColor: UIColor(rgb: 0x00A8E8) // Blue color
]
// Apply regular attributes to entire text
attributedString.addAttributes(regularAttributes, range: NSRange(location: 0, length: text.count))
// Find and style the link text
if let range = text.range(of: linkText) {
let nsRange = NSRange(range, in: text)
attributedString.addAttributes(linkAttributes, range: nsRange)
}
return attributedString
}
@objc private func toggleDetails() {
isDetailsExpanded.toggle()
UIView.animate(withDuration: 0.3) {
self.detailsLabel.numberOfLines = self.isDetailsExpanded ? 0 : 3
self.updateDetailsText()
self.view.layoutIfNeeded()
}
}
......