Rightpoint / BonMot

Beautiful, easy attributed strings in Swift
MIT License
3.54k stars 196 forks source link

Fixes an issue where `byAdding(.extraAttributes(_:))` removes previously assigned `extraAttributes`. #430

Closed stleamist closed 1 year ago

stleamist commented 1 year ago

Issue

byAdding(stringStyle:) preserves previously assigned extraAttributes, but byAdding(_:) doesn't.

let style = StringStyle()
    .byAdding(stringStyle: StringStyle(.extraAttributes([.backgroundColor: UIColor.white])))
    .byAdding(stringStyle: StringStyle(.extraAttributes([.foregroundColor: UIColor.black])))

BONAssert(attributes: style.attributes, key: .backgroundColor, value: UIColor.white) // ✅
BONAssert(attributes: style.attributes, key: .foregroundColor, value: UIColor.black) // ✅
let style = StringStyle()
    .byAdding(.extraAttributes([.backgroundColor: UIColor.white]))
    .byAdding(.extraAttributes([.foregroundColor: UIColor.black])) // overwrites previous line

BONAssert(attributes: style.attributes, key: .backgroundColor, value: UIColor.white) // ❌
BONAssert(attributes: style.attributes, key: .foregroundColor, value: UIColor.black) // ✅

Solution

Fixed update(part:) method to use add(extraAttributes:) for .extraAttributes(_:), like byAdding(stringStyle:) does.

ZevEisenberg commented 1 year ago

Thank you! This is a great catch. Would you mind swapping UIColor for BONColor, a platform-agnostic alias that will allow the tests to run on macOS?

stleamist commented 1 year ago

Okay! I’ll swap it right now :)

stleamist commented 1 year ago

Hi @ZevEisenberg,

Could you review this PR, please?

ZevEisenberg commented 1 year ago

Merged and released! Thanks for your contribution. It makes me happy to know BonMot has active users!