Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to set paragraph alignment #5

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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 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

/**
Expand Down
2 changes: 1 addition & 1 deletion Example/Example/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class ViewController: UIViewController {
.strikeThrough(2, forText: "shows")
.strikeThroughColor(UIColor.blue)
.underline(2, forText: "attributes")

.alignment(.center)
}

}
Expand Down