UILabel:纯文本和属性文本有什么区别?

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

我是 iOS 编程的新手,我尝试四处寻找答案,但不知何故找不到我认为应该是基本问题的答案。

我注意到两者都可以做同样的事情,我可以更改标签名称、设置自定义字体或系统字体并更改字体粗细、字体大小等。所以问题是,如果 Plain 能够做到这些,我为什么以及何时使用 Attributed?

谢谢。

plain 和 attributed 的区别从名字就可以看出来

PlainString 并且根据 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

至于 Attributed String 允许您使用自定义颜色、字体、下划线、阴影等设置文本范围的格式 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.

除非您有特定的理由使用 AttributedString,否则您几乎可以忘记它的存在。

这是一个很好的 Medium Article 属性字符串