From 4a9a38f145054f74e1e34810e25dc71b99f6453c Mon Sep 17 00:00:00 2001 From: Mark Turner Date: Thu, 13 Apr 2017 14:07:51 +1000 Subject: [PATCH 1/2] Added ability to set the line spacing. --- ...leAttributedString+ChainedAttributes.swift | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/ChainedAttributedString/NSMutableAttributedString+ChainedAttributes.swift b/ChainedAttributedString/NSMutableAttributedString+ChainedAttributes.swift index 1eac828..2177aa2 100644 --- a/ChainedAttributedString/NSMutableAttributedString+ChainedAttributes.swift +++ b/ChainedAttributedString/NSMutableAttributedString+ChainedAttributes.swift @@ -220,6 +220,31 @@ public extension NSMutableAttributedString { return self } + /** + This function adds paragraphy style with line spacing to attributed string. + + - warning: If text passed in "text" parameter is not found, attribute will be applied to whole attributed string. Only first occurence of "text" is styled. + + - parameter value - CGFloat which should be applied as linespacing to the paragraph style + - parameter text - String for which the paragraph style will be applied to (optional, default = whole attributed string) + + - returns: Modified NSMutableAttributedString + */ + func lineSpacing(_ value:CGFloat, forText text:String? = nil) -> NSMutableAttributedString { + + var attributeRange:NSRange? = nil + if let textForAttribute = text { + attributeRange = self.getRangeOfStringInSelf(textForAttribute) + } + + let paragraphStyle = NSMutableParagraphStyle() + paragraphStyle.lineSpacing = value + + self.applyAttribute(NSParagraphStyleAttributeName, withValue: paragraphStyle as AnyObject, forRange: attributeRange) + + return self + } + // MARK: Clear attributes /** From 3de9a1a48071784d5ef67a16291a432919f10309 Mon Sep 17 00:00:00 2001 From: Mark Turner Date: Thu, 13 Apr 2017 14:08:01 +1000 Subject: [PATCH 2/2] Updating example project to set the line spacing. --- Example/Example/ViewController.swift | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Example/Example/ViewController.swift b/Example/Example/ViewController.swift index c197ff6..52428d0 100644 --- a/Example/Example/ViewController.swift +++ b/Example/Example/ViewController.swift @@ -20,13 +20,14 @@ class ViewController: UIViewController { let one = "Test".attributedString() + "One".attributedString() //apply attributes - self.exampleLabel.attributedText = "This sample text shows chained attributes".attributedString() + self.exampleLabel.attributedText = "This sample text shows chained attributes\nWith line spacing as an option".attributedString() .textColor(UIColor.red, forText: "sample") .font(UIFont.boldSystemFont(ofSize: 20), forText: "This") .kernSpacing(-1, forText: "text") .strikeThrough(2, forText: "shows") .strikeThroughColor(UIColor.blue) .underline(2, forText: "attributes") + .lineSpacing(10) }