diff --git a/SwiftyAttributes/Sources/common/Operators.swift b/SwiftyAttributes/Sources/common/Operators.swift index 50a00e1..17efba0 100644 --- a/SwiftyAttributes/Sources/common/Operators.swift +++ b/SwiftyAttributes/Sources/common/Operators.swift @@ -16,3 +16,22 @@ public func + (lhs: NSAttributedString, rhs: NSAttributedString) -> NSMutableAtt combinedString.append(rhs) return combinedString } + +/** + Addition and assign operator. Creates a concatenated string and assigns it to the left value. + */ +public func += (lhs: inout NSAttributedString, rhs: NSAttributedString) { + lhs = lhs + rhs +} + +public func += (lhs: inout NSMutableAttributedString, rhs: NSAttributedString) { + lhs = lhs + rhs +} + +public func += (lhs: inout NSAttributedString, rhs: NSMutableAttributedString) { + lhs = lhs + rhs +} + +public func += (lhs: inout NSMutableAttributedString, rhs: NSMutableAttributedString) { + lhs = lhs + rhs +} diff --git a/SwiftyAttributesTests/Operators_Tests.swift b/SwiftyAttributesTests/Operators_Tests.swift index ce2e772..131b481 100644 --- a/SwiftyAttributesTests/Operators_Tests.swift +++ b/SwiftyAttributesTests/Operators_Tests.swift @@ -24,5 +24,69 @@ class Operators_Tests: XCTestCase { ] XCTAssertEqual(newString.attributes(at: lhs.length + 1, effectiveRange: nil) as NSDictionary, rightAttributes as NSDictionary) } + + // NSAttributedString + NSAttributedString + func testOverloadedAssignedAdditionOperator1() { + let lhs = "Hello".withFont(.systemFont(ofSize: 19)) as NSAttributedString + let rhs = "World".withTextColor(.magenta).withBackgroundColor(.orange).withFont(.boldSystemFont(ofSize: 24)) as NSAttributedString + var newString = lhs + newString += rhs + let leftAttributes: [NSAttributedString.Key: NSObject] = [.font: Font.systemFont(ofSize: 19)] + XCTAssertEqual(newString.attributes(at: 0, effectiveRange: nil) as NSDictionary, leftAttributes as NSDictionary) + let rightAttributes: [NSAttributedString.Key: NSObject] = [ + .foregroundColor: Color.magenta, + .backgroundColor: Color.orange, + .font: Font.boldSystemFont(ofSize: 24) + ] + XCTAssertEqual(newString.attributes(at: lhs.length + 1, effectiveRange: nil) as NSDictionary, rightAttributes as NSDictionary) + } + + // NSMutableAttributedString + NSAttributedString + func testOverloadedAssignedAdditionOperator2() { + let lhs = "Hello".withFont(.systemFont(ofSize: 19)) + let rhs = "World".withTextColor(.magenta).withBackgroundColor(.orange).withFont(.boldSystemFont(ofSize: 24)) as NSAttributedString + var newString = lhs + newString += rhs + let leftAttributes: [NSAttributedString.Key: NSObject] = [.font: Font.systemFont(ofSize: 19)] + XCTAssertEqual(newString.attributes(at: 0, effectiveRange: nil) as NSDictionary, leftAttributes as NSDictionary) + let rightAttributes: [NSAttributedString.Key: NSObject] = [ + .foregroundColor: Color.magenta, + .backgroundColor: Color.orange, + .font: Font.boldSystemFont(ofSize: 24) + ] + XCTAssertEqual(newString.attributes(at: lhs.length + 1, effectiveRange: nil) as NSDictionary, rightAttributes as NSDictionary) + } + + // NSAttributedString + NSMutableAttributedString + func testOverloadedAssignedAdditionOperator3() { + let lhs = "Hello".withFont(.systemFont(ofSize: 19)) as NSAttributedString + let rhs = "World".withTextColor(.magenta).withBackgroundColor(.orange).withFont(.boldSystemFont(ofSize: 24)) + var newString = lhs + newString += rhs + let leftAttributes: [NSAttributedString.Key: NSObject] = [.font: Font.systemFont(ofSize: 19)] + XCTAssertEqual(newString.attributes(at: 0, effectiveRange: nil) as NSDictionary, leftAttributes as NSDictionary) + let rightAttributes: [NSAttributedString.Key: NSObject] = [ + .foregroundColor: Color.magenta, + .backgroundColor: Color.orange, + .font: Font.boldSystemFont(ofSize: 24) + ] + XCTAssertEqual(newString.attributes(at: lhs.length + 1, effectiveRange: nil) as NSDictionary, rightAttributes as NSDictionary) + } + + // NSMutableAttributedString + NSMutableAttributedString + func testOverloadedAssignedAdditionOperator4() { + let lhs = "Hello".withFont(.systemFont(ofSize: 19)) + let rhs = "World".withTextColor(.magenta).withBackgroundColor(.orange).withFont(.boldSystemFont(ofSize: 24)) + var newString = lhs + newString += rhs + let leftAttributes: [NSAttributedString.Key: NSObject] = [.font: Font.systemFont(ofSize: 19)] + XCTAssertEqual(newString.attributes(at: 0, effectiveRange: nil) as NSDictionary, leftAttributes as NSDictionary) + let rightAttributes: [NSAttributedString.Key: NSObject] = [ + .foregroundColor: Color.magenta, + .backgroundColor: Color.orange, + .font: Font.boldSystemFont(ofSize: 24) + ] + XCTAssertEqual(newString.attributes(at: lhs.length + 1, effectiveRange: nil) as NSDictionary, rightAttributes as NSDictionary) + } }