قالب وردپرس درنا توس
Home / IOS Development / # 293: Modern assigned strings with attributed 🖋 – Little pieces of cocoa

# 293: Modern assigned strings with attributed 🖋 – Little pieces of cocoa



Subjects

Topics

AttributedString s is a great way to describe and add rich styled text to our apps.

I've covered some different approaches and solutions for composing AttributedString s over the years.

Today we will check out a new library, Submitted by Nicholas Maccharoli, which stands out as a modern approach to the task. Let's have a look. We start by looking at the "standard" way of using AttributedString say Foundation :

  la    font    =    UIFont   (  name :    "AvenirNext"     size ~~ POS = HEADCOMP :    1
8.0
)! NSAttributedString [ : "Han Solo" Attributes : [ NSForegroundColorAttributeName : UIColor ] black NSFontAttributeName : font ]

Not too bad, but we can do better. Let's try the same, but with Attribute:

  "Han Solo" .   attributed to   (  with :    Attributes    {   $ 0  .]   foreground   (  color .      black ).     writing   (  writing )  }) 

Tidy!

"Why not use AttributedString API provided with Foundation ?"

This is a fair question.

Imagine building an app that had many different assigned string styles. With the attribute, we get a strong API so that we can exclude a few types. We are also able to omit the long, correct key names.

Finally, (and perhaps most importantly), we use some type of closure composition technique.

This involves accepting a Swift closure in one initializes and gives us the anonymous argument $ 0 to play with. Then we can call function call to $ 0 to add attributes.

All this leads to a dramatic reduction in manual writing (even with autocomplete in Xcode writing tons of attribute collections stops being fun, quickly.

It also allows us to work more efficiently and accurately. We can lean on our syntactic stenography and rely on Swift's system to reach the finish line.

Heritage

Before we go, let's talk about one of the most common hiccups we can get in when we write AttributedString -related code. Inheritance.

We've all been there. We compose a AttributedString to enter a UILabel . We are pumped because we have translated the style exactly from the original design to the code. The design requires that one of the words in the label should be another color, by weight. though, with the attribute we can l see this issue quickly:

  la   ] base    =    attributes   ()     font   (  UIFont   (  name .    "AvenirNext" [19659014]    size ~~ POS = HEADCOMP :!.    18.0 )   ) 

  la    highlighted    =    the base     for       color :  .   red ) 

  "He Solo is the Captain of" .   attributed to   19659015] with :    base ) 
      +    "Millennium Falcon" .   attributed   (  with :    highlighted ) 

Very fun. We can define a basic set of attributes in Attributes () and then compose new sets that inherit all the attributes of the base set.

Then we are Support s support to the operator + to write some super clean code.

and 19659005] Pro Tip: This functionality also allows us to easily build a reusable set of Attributes () keep them on one place and sprinkle them throughout our code.

Learn more about attribute at git.io/attributed.


Source link