From 9bf0540e492f875940b9f401c97b9b1457ad5cc7 Mon Sep 17 00:00:00 2001 From: Mark Turner Date: Thu, 20 Jul 2017 11:30:21 +0100 Subject: [PATCH 1/2] Added ability to set the paragraph alignment. --- ...leAttributedString+ChainedAttributes.swift | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/ChainedAttributedString/NSMutableAttributedString+ChainedAttributes.swift b/ChainedAttributedString/NSMutableAttributedString+ChainedAttributes.swift index 1eac828..e7ad517 100644 --- a/ChainedAttributedString/NSMutableAttributedString+ChainedAttributes.swift +++ b/ChainedAttributedString/NSMutableAttributedString+ChainedAttributes.swift @@ -220,6 +220,31 @@ public extension NSMutableAttributedString { return self } + /** + This function adds paragraph style with text alignment 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 alignment - NSTextAlignment which should be applied as alignment 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 alignment(_ alignment:NSTextAlignment, forText text:String? = nil) -> NSMutableAttributedString { + + var attributeRange:NSRange? = nil + if let textForAttribute = text { + attributeRange = self.getRangeOfStringInSelf(textForAttribute) + } + + let paragraphStyle = NSMutableParagraphStyle() + paragraphStyle.alignment = .center + + self.applyAttribute(NSParagraphStyleAttributeName, withValue: paragraphStyle as AnyObject, forRange: attributeRange) + + return self + } + // MARK: Clear attributes /** From 6a2219a449e5f72afb4bf96476491390eb46985c Mon Sep 17 00:00:00 2001 From: Mark Turner Date: Thu, 20 Jul 2017 11:30:44 +0100 Subject: [PATCH 2/2] Updating example project to set the paragraph alignment. --- Example/Example/ViewController.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Example/Example/ViewController.swift b/Example/Example/ViewController.swift index c197ff6..137b843 100644 --- a/Example/Example/ViewController.swift +++ b/Example/Example/ViewController.swift @@ -27,7 +27,7 @@ class ViewController: UIViewController { .strikeThrough(2, forText: "shows") .strikeThroughColor(UIColor.blue) .underline(2, forText: "attributes") - + .alignment(.center) } }