diff --git a/StringStylizer/Operator.swift b/StringStylizer/Operator.swift index 7e4670e..d302ddc 100644 --- a/StringStylizer/Operator.swift +++ b/StringStylizer/Operator.swift @@ -39,6 +39,62 @@ public func + (left: NSAttributedString, right: NSAttributedString) -> NSAttribu return concat } +/** + combine two NSAttributedString object + + + - paramter left:NSAttributedString? + - paramter right:NSAttributedString + + - returns: NSAttributedString + */ +public func + (left: NSAttributedString?, right: NSAttributedString) -> NSAttributedString { + let concat = NSMutableAttributedString() + if let left = left { + concat.append(left) + } + concat.append(right) + return concat +} + +/** + combine two NSAttributedString object + + + - paramter left:NSAttributedString + - paramter right:NSAttributedString? + + - returns: NSAttributedString + */ +public func + (left: NSAttributedString, right: NSAttributedString?) -> NSAttributedString { + let concat = NSMutableAttributedString() + concat.append(left) + if let right = right { + concat.append(right) + } + return concat +} + +/** + combine two NSAttributedString object + + + - paramter left:NSAttributedString? + - paramter right:NSAttributedString? + + - returns: NSAttributedString + */ +public func + (left: NSAttributedString?, right: NSAttributedString?) -> NSAttributedString { + let concat = NSMutableAttributedString() + if let left = left { + concat.append(left) + } + if let right = right { + concat.append(right) + } + return concat +} + /** append NSAttributedString object @@ -70,3 +126,23 @@ public func + (left: NSAttributedString, right: NSTextAttachment) -> NSAttribute public func + (left: NSTextAttachment, right: NSAttributedString) -> NSAttributedString { return NSAttributedString(attachment: left) + right } + +/// combine two NSAttributedString and NSTextAttachment objects +/// +/// - Parameters: +/// - left: NSAttributedString +/// - right: NSTextAttachment? +/// - Returns: combinded NSAttributedString +public func + (left: NSAttributedString, right: NSTextAttachment?) -> NSAttributedString { + return left + right.flatMap(NSAttributedString.init(attachment:)) +} + +/// combine two NSTextAttachment and NSAttributedString objects +/// +/// - Parameters: +/// - left: NSTextAttachment? +/// - right: NSAttributedString +/// - Returns: combinded NSAttributedString +public func + (left: NSTextAttachment?, right: NSAttributedString) -> NSAttributedString { + return left.flatMap(NSAttributedString.init(attachment:)) + right +} diff --git a/StringStylizerTests/StringStylizerTests.swift b/StringStylizerTests/StringStylizerTests.swift index 1f76267..399ffc2 100755 --- a/StringStylizerTests/StringStylizerTests.swift +++ b/StringStylizerTests/StringStylizerTests.swift @@ -309,6 +309,32 @@ class StringStylizerTests: XCTestCase { XCTAssert(combined.isEqual(expected), "optional StringStylized strings do not add any content to the resulting NSAttributedString") } + func testOptionalAttributedString() { + let optional: NSAttributedString? = "a".stylize().attr + let notOptional: NSAttributedString = "b".stylize().attr + + do { + let actual = optional + notOptional + + let expected = NSAttributedString(string: "ab") + XCTAssertEqual(actual, expected) + } + + do { + let actual = notOptional + optional + + let expected = NSAttributedString(string: "ba") + XCTAssertEqual(actual, expected) + } + + do { + let actual = optional + optional + + let expected = NSAttributedString(string: "aa") + XCTAssertEqual(actual, expected) + } + } + // MARK:- private fileprivate func rgb(_ rgb: UInt, alpha: Double) -> UIColor { return UIColor(