Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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

/**
Expand Down
3 changes: 2 additions & 1 deletion Example/Example/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)

}

Expand Down