From 05dee8a4db4e76257abf9c5c183e3212d2d8224e Mon Sep 17 00:00:00 2001 From: Felix Lisczyk <5102728+FelixLisczyk@users.noreply.github.com> Date: Tue, 30 Sep 2025 15:23:59 +0200 Subject: [PATCH] Fix layout issue with title font sizing in attributed strings --- .../Views/UIOnboardingTitleLabel.swift | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/Sources/UIOnboarding/Views/UIOnboardingTitleLabel.swift b/Sources/UIOnboarding/Views/UIOnboardingTitleLabel.swift index b1164a3..c81488b 100644 --- a/Sources/UIOnboarding/Views/UIOnboardingTitleLabel.swift +++ b/Sources/UIOnboarding/Views/UIOnboardingTitleLabel.swift @@ -41,15 +41,24 @@ extension UIOnboardingTitleLabel { func updateFontSize(_ fontSize: CGFloat) { guard let attributedString = attributedText else { return } - let originalFont = font let mutableAttributedString = NSMutableAttributedString(attributedString: attributedString) + + // Update the label's base font + let updatedBaseFont = font.withSize(fontSize) + self.font = updatedBaseFont + + // Update fonts in the attributed string mutableAttributedString.enumerateAttributes(in: NSRange(location: 0, length: mutableAttributedString.length), options: []) { attributes, range, _ in - if let font = attributes[.font] as? UIFont { - let multiplier = font.pointSize / (originalFont?.pointSize ?? font.pointSize) - let newFont = font.withSize(fontSize * multiplier) + if let existingFont = attributes[.font] as? UIFont { + // If font is already set in attributed string, preserve relative sizing + let multiplier = existingFont.pointSize / self.font.pointSize + let newFont = existingFont.withSize(fontSize * multiplier) mutableAttributedString.addAttribute(.font, value: newFont, range: range) + } else { + // If no font is set, apply the base font + mutableAttributedString.addAttribute(.font, value: updatedBaseFont, range: range) } }