Search code examples
iosswiftxcodestoryboarduilabel

UILabel: What is the difference between Plain and Attributed Text?


I'm a newbie to iOS programming and I've tried looking around for answers but somehow can't find the answer to what I assume should be a basic question.

enter image description here

I noticed both is able to do the same thing, I am able to change the label name, set a custom font or system font and change the font weight, font size and stuff. So the question is, if Plain is able to do those, why and when do I use Attributed?

Thanks.


Solution

  • You can understand difference between plain and attributed from their name.

    Plain is String and as per Apple docs

    String

    A Unicode string value is a series of characters, such as "Swift", that forms a collection. Strings in Swift are Unicode correct and locale insensitive, and are designed to be efficient

    And as for Attributed String allow you to format ranges of text with custom colors, fonts, underlines, shadows, and more Apple Docs

    NSAttributedString

    A string that has associated attributes (such as visual style, hyperlinks, or accessibility data) for portions of its text.An NSAttributedString object manages character strings and associated sets of attributes (for example, font and kerning) that apply to individual characters or ranges of characters in the string. An association of characters and their attributes is called an attributed string. The cluster’s two public classes, NSAttributedString and NSMutableAttributedString, declare the programmatic interface for read-only attributed strings and modifiable attributed strings, respectively.

    Unless you have a specific reason to use AttributedString, you can pretty much forget that it exists.

    enter image description here

    Here is a good Medium Article on attributed String