Showing
2 changed files
with
86 additions
and
8 deletions
No preview for this file type
... | @@ -98,8 +98,6 @@ import UIKit | ... | @@ -98,8 +98,6 @@ import UIKit |
98 | infoLabel.textColor = UIColor(rgb: 0x020E1C) | 98 | infoLabel.textColor = UIColor(rgb: 0x020E1C) |
99 | infoLabel.text = "Περισσότερα" | 99 | infoLabel.text = "Περισσότερα" |
100 | 100 | ||
101 | - setupExpandableDetails() | ||
102 | - | ||
103 | couponCodeContainerView.backgroundColor = UIColor(rgb: 0xFFFFFF) | 101 | couponCodeContainerView.backgroundColor = UIColor(rgb: 0xFFFFFF) |
104 | couponCodeContainerView.layer.cornerRadius = 8.0 | 102 | couponCodeContainerView.layer.cornerRadius = 8.0 |
105 | couponCodeContainerView.clipsToBounds = true | 103 | couponCodeContainerView.clipsToBounds = true |
... | @@ -156,6 +154,8 @@ import UIKit | ... | @@ -156,6 +154,8 @@ import UIKit |
156 | expirationLabel.textColor = UIColor(rgb: 0x020E1C) | 154 | expirationLabel.textColor = UIColor(rgb: 0x020E1C) |
157 | // expirationLabel.text = ("Η προσφορά ισχύει " + coupon.expirationDate) | 155 | // expirationLabel.text = ("Η προσφορά ισχύει " + coupon.expirationDate) |
158 | expirationLabel.text = "Η προσφορά ισχύει έως 30-09-2025" | 156 | expirationLabel.text = "Η προσφορά ισχύει έως 30-09-2025" |
157 | + | ||
158 | + setupExpandableDetails() | ||
159 | 159 | ||
160 | couponCodeTitleLabel.font = UIFont(name: "PingLCG-Regular", size: 16) | 160 | couponCodeTitleLabel.font = UIFont(name: "PingLCG-Regular", size: 16) |
161 | couponCodeTitleLabel.textColor = UIColor(rgb: 0x000F1E) | 161 | couponCodeTitleLabel.textColor = UIColor(rgb: 0x000F1E) |
... | @@ -174,29 +174,107 @@ import UIKit | ... | @@ -174,29 +174,107 @@ import UIKit |
174 | 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." | 174 | 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." |
175 | } | 175 | } |
176 | 176 | ||
177 | + private var fullDetailsText = "" | ||
178 | + private var shouldTruncaitDetails = false | ||
179 | + | ||
177 | private func setupExpandableDetails() { | 180 | private func setupExpandableDetails() { |
178 | - 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." | 181 | + 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." |
179 | 182 | ||
180 | - detailsLabel.text = fullText | ||
181 | - detailsLabel.numberOfLines = 3 | ||
182 | detailsLabel.font = UIFont(name: "PingLCG-Regular", size: 18) | 183 | detailsLabel.font = UIFont(name: "PingLCG-Regular", size: 18) |
183 | detailsLabel.textColor = UIColor(rgb: 0x020E1C) | 184 | detailsLabel.textColor = UIColor(rgb: 0x020E1C) |
184 | 185 | ||
185 | - // Add tap gesture | 186 | + updateDetailsText() |
187 | + | ||
186 | let tapGesture = UITapGestureRecognizer(target: self, action: #selector(toggleDetails)) | 188 | let tapGesture = UITapGestureRecognizer(target: self, action: #selector(toggleDetails)) |
187 | detailsLabel.isUserInteractionEnabled = true | 189 | detailsLabel.isUserInteractionEnabled = true |
188 | detailsLabel.addGestureRecognizer(tapGesture) | 190 | detailsLabel.addGestureRecognizer(tapGesture) |
189 | } | 191 | } |
190 | 192 | ||
193 | + private func updateDetailsText() { | ||
194 | + if isDetailsExpanded { | ||
195 | + // Show full text with "Λιγότερα" | ||
196 | + if (shouldTruncaitDetails) { | ||
197 | + let fullTextWithLess = fullDetailsText + " Λιγότερα" | ||
198 | + let attributedString = createAttributedString(text: fullTextWithLess, linkText: "Λιγότερα") | ||
199 | + detailsLabel.attributedText = attributedString | ||
200 | + } else { | ||
201 | + detailsLabel.text = fullDetailsText | ||
202 | + } | ||
203 | + detailsLabel.numberOfLines = 0 | ||
204 | + } else { | ||
205 | + // Calculate approximate characters for 4 lines and truncate if needed | ||
206 | + let truncatedText = getTruncatedTextForFourLines() | ||
207 | + let attributedString = createAttributedString(text: truncatedText, linkText: "Περισσότερα") | ||
208 | + detailsLabel.attributedText = attributedString | ||
209 | + detailsLabel.numberOfLines = 4 | ||
210 | + } | ||
211 | + } | ||
212 | + | ||
213 | + private func getTruncatedTextForFourLines() -> String { | ||
214 | + // Calculate approximate characters that fit in 4 lines | ||
215 | + let labelWidth = detailsLabel.frame.width | ||
216 | + let fontSize: CGFloat = 18 | ||
217 | + let averageCharWidth: CGFloat = fontSize * 0.5 // Rough estimate for character width | ||
218 | + let charactersPerLine = Int(labelWidth / averageCharWidth) | ||
219 | + let maxCharactersFor4Lines = charactersPerLine * 4 | ||
220 | + | ||
221 | + let moreText = " Περισσότερα" | ||
222 | + let ellipsis = "..." | ||
223 | + let reservedCharacters = ellipsis.count + moreText.count + 15 | ||
224 | + | ||
225 | + // If text is short enough, don't truncate | ||
226 | + if fullDetailsText.count <= maxCharactersFor4Lines { | ||
227 | + shouldTruncaitDetails = false | ||
228 | + return fullDetailsText | ||
229 | + } else { | ||
230 | + shouldTruncaitDetails = true | ||
231 | + } | ||
232 | + | ||
233 | + // Calculate how many characters we can use from original text | ||
234 | + let availableCharacters = maxCharactersFor4Lines - reservedCharacters | ||
235 | + | ||
236 | + // Truncate text and add ellipsis + more | ||
237 | + let truncatedText = String(fullDetailsText.prefix(availableCharacters)) | ||
238 | + return truncatedText + ellipsis + moreText | ||
239 | + } | ||
240 | + | ||
241 | + private func createAttributedString(text: String, linkText: String) -> NSAttributedString { | ||
242 | + let attributedString = NSMutableAttributedString(string: text) | ||
243 | + | ||
244 | + // Regular text attributes | ||
245 | + let regularAttributes: [NSAttributedString.Key: Any] = [ | ||
246 | + .font: UIFont(name: "PingLCG-Regular", size: 18) ?? UIFont.systemFont(ofSize: 18), | ||
247 | + .foregroundColor: UIColor(rgb: 0x020E1C) | ||
248 | + ] | ||
249 | + | ||
250 | + // Link text attributes (blue color) | ||
251 | + let linkAttributes: [NSAttributedString.Key: Any] = [ | ||
252 | + .font: UIFont(name: "PingLCG-Regular", size: 18) ?? UIFont.systemFont(ofSize: 18), | ||
253 | + .foregroundColor: UIColor(rgb: 0x00A8E8) // Blue color | ||
254 | + ] | ||
255 | + | ||
256 | + // Apply regular attributes to entire text | ||
257 | + attributedString.addAttributes(regularAttributes, range: NSRange(location: 0, length: text.count)) | ||
258 | + | ||
259 | + // Find and style the link text | ||
260 | + if let range = text.range(of: linkText) { | ||
261 | + let nsRange = NSRange(range, in: text) | ||
262 | + attributedString.addAttributes(linkAttributes, range: nsRange) | ||
263 | + } | ||
264 | + | ||
265 | + return attributedString | ||
266 | + } | ||
267 | + | ||
268 | + | ||
191 | @objc private func toggleDetails() { | 269 | @objc private func toggleDetails() { |
192 | isDetailsExpanded.toggle() | 270 | isDetailsExpanded.toggle() |
193 | 271 | ||
194 | UIView.animate(withDuration: 0.3) { | 272 | UIView.animate(withDuration: 0.3) { |
195 | - self.detailsLabel.numberOfLines = self.isDetailsExpanded ? 0 : 3 | 273 | + self.updateDetailsText() |
196 | self.view.layoutIfNeeded() | 274 | self.view.layoutIfNeeded() |
197 | } | 275 | } |
198 | } | 276 | } |
199 | - | 277 | + |
200 | @objc private func toggleCouponCode() { | 278 | @objc private func toggleCouponCode() { |
201 | isCouponCodeExpanded.toggle() | 279 | isCouponCodeExpanded.toggle() |
202 | 280 | ... | ... |
-
Please register or login to post a comment