From 4b540bc4503e8c7776ae0ec94cf1f2a3139736fe Mon Sep 17 00:00:00 2001 From: Chris Hansen Date: Tue, 26 Feb 2019 18:08:53 -0800 Subject: [PATCH] Fix #1 - Not respecting lineSpacing when set There was an issue where if you set some custom attributes before setting attributedText on the label, it wouldn't keep those properties in the attributedString. Now when you set lineSpacing, and then set attributedText on the label, it'll pull the properties already setup and append them to the attributedString you're putting in. --- Example/Nantes/ViewController.swift | 24 ++++++++++++++++++++++++ Example/Podfile.lock | 4 ++-- Source/Classes/NantesLabel.swift | 11 +++++++++-- 3 files changed, 35 insertions(+), 4 deletions(-) diff --git a/Example/Nantes/ViewController.swift b/Example/Nantes/ViewController.swift index c68d585..f8c378e 100644 --- a/Example/Nantes/ViewController.swift +++ b/Example/Nantes/ViewController.swift @@ -32,6 +32,7 @@ final class ViewController: UIViewController { case truncatedLinkText = "Truncated link text with a longer body so we'll truncate it\nMaybe a newline for good measure" case transitInfo = "UA450" case privacyPolicyText = "By creating an account, you agree to our\nTerms of Service and Privacy Policy" + case linespace = "Test string with font and linespace set with some extra content in there so it'll wrap around and have multiple lines" } private var strings: [String] = [] @@ -62,6 +63,8 @@ final class ViewController: UIViewController { setupStrokedLabel() setupTruncatedAttributedToken() setupPrivacyPolicyLabel() + setupLineSpace() + setupUILabelLineSpace() } private func setupTitleLabel() { @@ -245,6 +248,27 @@ final class ViewController: UIViewController { label.addLink(to: URL(string: "https://www.google.com")!, withRange: (text as NSString).range(of: "Privacy Policy")) labelStackView.addArrangedSubview(label) } + + private func setupLineSpace() { + let label: NantesLabel = .init(frame: .zero) + label.numberOfLines = 0 + label.lineSpacing = 40 + label.font = .systemFont(ofSize: 14.0) + label.text = ExampleString.linespace.rawValue + + labelStackView.addArrangedSubview(label) + } + + private func setupUILabelLineSpace() { + let label: UILabel = .init(frame: .zero) + label.numberOfLines = 0 + let paragraphStyle = NSMutableParagraphStyle() + paragraphStyle.lineSpacing = 40 + let attributedText = NSAttributedString(string: ExampleString.linespace.rawValue, attributes: [NSAttributedString.Key.paragraphStyle: paragraphStyle, NSAttributedString.Key.font: UIFont.systemFont(ofSize: 14.0)]) + label.attributedText = attributedText + + labelStackView.addArrangedSubview(label) + } } extension ViewController: NantesLabelDelegate { diff --git a/Example/Podfile.lock b/Example/Podfile.lock index 5ddeb00..eaef2b0 100644 --- a/Example/Podfile.lock +++ b/Example/Podfile.lock @@ -1,5 +1,5 @@ PODS: - - Nantes (0.0.3) + - Nantes (0.0.4) DEPENDENCIES: - Nantes (from `../`) @@ -9,7 +9,7 @@ EXTERNAL SOURCES: :path: "../" SPEC CHECKSUMS: - Nantes: 72320e1ac102e6763503c27b2bc01d220f8e4acf + Nantes: f7f93de5a749e62f7367f93d2c0f15f4c6dabf09 PODFILE CHECKSUM: 1c8633395754df792140631cc4cd6cb86ec7966b diff --git a/Source/Classes/NantesLabel.swift b/Source/Classes/NantesLabel.swift index 963f3c7..5574af9 100644 --- a/Source/Classes/NantesLabel.swift +++ b/Source/Classes/NantesLabel.swift @@ -309,11 +309,18 @@ public extension NSAttributedString.Key { get { return _attributedText } set { - guard newValue != _attributedText else { + let attributes = NSAttributedString.attributes(from: self) + guard let updatedText = newValue?.mutableCopy() as? NSMutableAttributedString else { + return + } + + updatedText.addAttributes(attributes, range: NSRange(location: 0, length: updatedText.length)) + + guard updatedText != _attributedText else { return } - _attributedText = newValue + _attributedText = updatedText setNeedsFramesetter() _accessibilityElements = nil linkModels = []