Skip to content

Commit

Permalink
Fix #1 - Not respecting lineSpacing when set
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Chris Hansen committed Feb 27, 2019
1 parent 9c64575 commit 4b540bc
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
24 changes: 24 additions & 0 deletions Example/Nantes/ViewController.swift
Expand Up @@ -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] = []
Expand Down Expand Up @@ -62,6 +63,8 @@ final class ViewController: UIViewController {
setupStrokedLabel()
setupTruncatedAttributedToken()
setupPrivacyPolicyLabel()
setupLineSpace()
setupUILabelLineSpace()
}

private func setupTitleLabel() {
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions Example/Podfile.lock
@@ -1,5 +1,5 @@
PODS:
- Nantes (0.0.3)
- Nantes (0.0.4)

DEPENDENCIES:
- Nantes (from `../`)
Expand All @@ -9,7 +9,7 @@ EXTERNAL SOURCES:
:path: "../"

SPEC CHECKSUMS:
Nantes: 72320e1ac102e6763503c27b2bc01d220f8e4acf
Nantes: f7f93de5a749e62f7367f93d2c0f15f4c6dabf09

PODFILE CHECKSUM: 1c8633395754df792140631cc4cd6cb86ec7966b

Expand Down
11 changes: 9 additions & 2 deletions Source/Classes/NantesLabel.swift
Expand Up @@ -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 = []
Expand Down

0 comments on commit 4b540bc

Please sign in to comment.