-
Notifications
You must be signed in to change notification settings - Fork 3
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
Support Multi-Attribute Strings #6
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -102,25 +102,87 @@ public enum NSAttributedStringAttribute { | |
|
||
public class NSAttributedStringAttributeBuilder { | ||
|
||
private var attributesDictionary: [String : AnyObject] = [ : ] | ||
private var clearAttributesOnNextString: Bool = false | ||
|
||
private var currentString: String = "" | ||
private var strings = [String]() | ||
|
||
var currentAttributes: [String : AnyObject] = [ : ] | ||
var attributes = [[String: AnyObject]]() | ||
|
||
public init() { } | ||
|
||
public func setAttribute(attribute: NSAttributedStringAttribute) { | ||
// Builder functions | ||
|
||
public func setAttribute(attribute: NSAttributedStringAttribute) throws { | ||
let keyValuePair = attribute.attributedStringKeyValuePair | ||
attributesDictionary.updateValue(keyValuePair.value, forKey: keyValuePair.key) | ||
currentAttributes.updateValue(keyValuePair.value, forKey: keyValuePair.key) | ||
} | ||
|
||
public func attributedStringAttributesDictionary() -> [String: AnyObject]? { | ||
return attributesDictionary | ||
public func clearAttributes() { | ||
currentAttributes.removeAll() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we add test coverage for this function |
||
} | ||
|
||
public func addAttributes(attributes: [String: AnyObject]) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Potentially we should rename this to |
||
for (key, value) in attributes { | ||
currentAttributes.updateValue(value, forKey: key) | ||
} | ||
} | ||
|
||
public func removeAttribute(attribute: NSAttributedStringAttribute) throws { | ||
let keyValuePair = attribute.attributedStringKeyValuePair | ||
currentAttributes.removeValueForKey(keyValuePair.key) | ||
} | ||
|
||
public func removeAttributes(attributes: [String: AnyObject]) { | ||
for (key, _) in attributes { | ||
currentAttributes.removeValueForKey(key) | ||
} | ||
} | ||
|
||
public func nextString(string: String) { | ||
endCurrent() | ||
currentString = string | ||
} | ||
|
||
|
||
// Internal helpers | ||
|
||
private func endCurrent() { | ||
attributes.append(currentAttributes) | ||
strings.append(currentString) | ||
if (clearAttributesOnNextString) { | ||
clearAttributes() | ||
} | ||
currentString = "" | ||
} | ||
|
||
private func finalize() -> NSAttributedString { | ||
let attrStrings = zip(strings, attributes) | ||
.map { (str: String, attr: [String: AnyObject]) in | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. wow cool use of |
||
return NSAttributedString(string: str, attributes: attr) | ||
} | ||
|
||
// TODO: Get reduce to work instead of whats below. Was showing ambiguous errors with mutable string as accumulator | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not 100% sure if what about something like this to encapsulate the logic let finalString: NSAttributedString = {
let mut = NSMutableAttributedString()
attrStrings.forEach{ mut.appendAttributedString($0) }
return NSAttributedString(attributedString: mut)
}()
return finalString |
||
let mut = NSMutableAttributedString() | ||
for attr in attrStrings { | ||
mut.appendAttributedString(attr) | ||
} | ||
return mut | ||
} | ||
} | ||
|
||
public extension NSAttributedString { | ||
|
||
public class func make(string: String, make: ((make: NSAttributedStringAttributeBuilder) -> Void)) -> NSAttributedString { | ||
let builder = NSAttributedStringAttributeBuilder() | ||
builder.nextString(string) | ||
make(make: builder) | ||
return NSAttributedString(string: string, attributes: builder.attributedStringAttributesDictionary()) | ||
return builder.finalize() | ||
} | ||
|
||
public class func make(make: ((make: NSAttributedStringAttributeBuilder) -> Void)) -> NSAttributedString { | ||
let builder = NSAttributedStringAttributeBuilder() | ||
return builder.finalize() | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you explain why this function
throws
?