Rightpoint / BonMot

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

Remove NSAttributedStringKey.kern from last character of styled strings #290

Closed ZevEisenberg closed 6 years ago

ZevEisenberg commented 7 years ago

fixes #284

ZevEisenberg commented 6 years ago

I got a great suggestion from @fcanas, @danielpunkass, and @mattmassicotte about avoiding thread-local storage: when constructing strings using styled(with:overrideParts:), in cases where we strip the final-character kerning attribute, add our own custom com.raizlabs.bonmot.kerningAttributeRemovedHere attribute to the last character of the string. Then, in composed(of:baseStyle:separator:), look for that attribute, and if it's there and it's not the last item, reinstate the kerning attribute. I like this better than thread-local, so I'm going to take a crack at switching to this method.

Oh, and @mattmassicotte pointed out that what I probably wanted in the first place was queue-local storage, not thread-local storage. Good distinction to make!

ZevEisenberg commented 6 years ago

The new method also has the nice bonus of working with any string created with styled(with:overrideParts:), instead of just those created with that method but only when passed to composed(of:baseStyle:separator) directly. I see this as a win.

mattmassicotte commented 6 years ago

👋 I’ll have discussions about TLS any time!

Great work fixing this!