Working with text is a key task when creating interfaces for modern mobile applications. From font selection to adjusting character spacing — every aspect affects the perception of the product. According to Apple NSAttributedString Documentation, proper text formatting is directly related to readability and interface accessibility.
Key Takeaways
NSAttributedString is the fundamental iOS class for working with formatted text. It allows applying attributes to individual characters or ranges: font via UIFont, color via UIColor, character spacing via kern, and line spacing via NSMutableParagraphStyle. In mobile apps, NSAttributedString is used everywhere — from UILabel to complex UITextView with editing support.
UIFont is the main class for working with fonts on iOS. It supports system typefaces, custom fonts, and Dynamic Type. UIFont(name:size:) loads a font by name, UIFont.systemFont(ofSize:weight:) loads the system font. UIFontDescriptor allows modifying an existing font: changing size, weight, or adding a transformation matrix. For custom fonts, the .ttf file is added to the project and registered in Info.plist under the Fonts provided by application key.
let attributes: [NSAttributedString.Key: Any] = [
.font: UIFont(name: "HelveticaNeue-Bold", size: 18) as Any,
.foregroundColor: UIColor.darkGray as Any,
.kern: 1.5 as Any
]
let attributedString = NSAttributedString(
string: "Форматированный текст",
attributes: attributes
)
label.attributedText = attributedString
NSMutableParagraphStyle is a mutable version of ParagraphStyle for configuring paragraph display. It controls alignment, line spacing (lineSpacing), indentation (headIndent, tailIndent), and truncation mode (lineBreakMode). ParagraphStyle is critical for readability of long texts on mobile devices: proper lineSpacing prevents line merging, and lineBreakMode prevents word clipping. The paragraphSpacing setting defines the space between paragraphs.
SpannableString is the main tool for text formatting on Android. It works through a mechanism of Span objects, each responsible for one aspect: StyleSpan for bold or italic, ForegroundColorSpan for color, URLSpan for links, UnderlineSpan for underline. SpannableStringBuilder is the equivalent of NSMutableAttributedString, allowing you to build text from multiple fragments. Understanding SpannableString is essential for creating flexible interfaces with formatted text in mobile apps.
Typeface is the class for working with fonts on Android. It supports system typefaces Roboto and Noto, as well as custom fonts. Typeface.createFromResource() loads a font from res/font/, Typeface.createFromAsset() loads from the assets folder. Android 8.0+ supports font families in XML, combining multiple font files into a single resource. A custom font for text in mobile apps on Android is set via the android:fontFamily attribute or programmatically using setTypeface().
TextMeasurer (API 29+) is a modern tool for measuring text without binding to a View. It calculates width, height, and character positions before rendering. On older APIs, Paint.measureText() is used for single-line text or StaticLayout for multi-line text. TextMeasurer is especially useful in custom Views where text is drawn via Canvas.drawText(). Correct measurement prevents character clipping in the interface.
val spannable = SpannableString("Жирный текст с цветом")
spannable.setSpan(
StyleSpan(Typeface.BOLD),
0, 6,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
)
spannable.setSpan(
ForegroundColorSpan(Color.RED),
14, 19,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
)
textView.text = spannable
Typography in mobile apps is a comprehensive configuration of fonts, character spacing, and line spacing. Proper typography in mobile development directly affects text readability and the visual perception of the interface. Let's look at the key parameters available on both platforms.
Kerning is the adjustment of space between specific character pairs (AV, To, Wa). Tracking is uniform character spacing for the entire text or a portion of it. On iOS, kerning is set via NSKernAttributeName, on Android — via the letterSpacing property of TextView. Negative values bring characters closer together, positive values spread them apart. Typography in mobile development requires careful tuning of these parameters: for large headings, character spacing is usually increased, for body text it remains standard.
| Parameter | iOS | Android |
|---|---|---|
| Styled String | NSAttributedString | SpannableString |
| Font | UIFont / CTFont | Typeface |
| Character Spacing | NSKernAttributeName | letterSpacing |
| Line Spacing | lineSpacing | lineSpacingExtra |
| Ligatures | NSLigatureAttributeName | OpenType features |
| Ellipsis | lineBreakMode .byTruncatingTail | android:ellipsize |
Ligatures are special glyphs that replace combinations of two or more characters: fi, fl, ffi. On iOS, ligatures are controlled via NSLigatureAttributeName: 0 — disabled, 1 — standard, 2 — all available. On Android, ligatures are controlled at the font level through OpenType features. Enabling ligatures improves text appearance, especially in serif typefaces. Typography in mobile development uses ligatures primarily for headings and logos.
Custom fonts are one of the main tools for creating a unique visual style for an app. They help highlight the brand and improve the user experience. In mobile apps, custom fonts are used for headings, logos, accent elements, and corporate identity.
Variable Fonts are a modern font technology where a single file contains multiple styles: from thin to extra bold, from narrow to wide. This reduces app size by replacing several font files with one. On iOS, Variable Fonts are supported via UIFont with the weight parameter. On Android — via Typeface.Builder specifying variability axes wght, wdth, slnt. Using Variable Fonts simplifies typography in mobile apps: the developer doesn't need to load separate files for each style.
let customFont = UIFont(name: "CustomFont-Regular", size: 16)
let scaledFont = UIFontMetrics.default
.scaledFont(for: customFont)
label.font = scaledFont
label.adjustsFontForContentSizeCategory(true)
NSTextAttachment on iOS allows embedding UIImage inside NSAttributedString — the image aligns to the baseline and wraps with text. On Android, the equivalent is ImageSpan inside SpannableString. Both approaches are useful for chat with emojis, news feeds with icons, and forms with custom checkboxes. It's important to properly scale the image to match the font height so that text and graphics look harmonious.
Beyond basic formatting, text in mobile apps supports additional visual effects: shadows, outlines, strikethrough, and underline. These tools expand the possibilities of typography but require careful application to maintain readability.
Text shadow on iOS is set via NSShadow with offset, blurRadius, and color. Outline — via NSStrokeWidthAttributeName: negative values create fill with outline, positive values create only the contour. Strikethrough — NSUnderlineStyle (.single, .thick, .double). On Android, TextView shadow is configured via android:shadowRadius, android:shadowDx, android:shadowDy. SpannableString supports StrikethroughSpan and UnderlineSpan. Text outline on Android is implemented via OutlineSpan or a custom font.
Ellipsis is text truncation with an ellipsis when the container overflows. On iOS, it's configured via lineBreakMode .byTruncatingTail and numberOfLines. On Android — via android:ellipsize="end" and android:maxLines. Marquee is a scrolling text animation available on Android via android:ellipsize="marquee". On iOS, there is no built-in equivalent — the MarqueeLabel library is used. Ellipsis is mandatory for all single-line texts in mobile apps to avoid content clipping.
Dynamic Type is an adaptive text system in iOS that adjusts font size based on the user's Accessibility settings. Use UIFontMetrics to scale custom fonts. Dynamic Type is mandatory for Accessibility-compliant apps. On Android, the equivalent is sp units and scaledPixels, which scale according to system text size settings.
Frequently Asked Questions
NSAttributedString is an iOS class for creating formatted text with font, color, and style attributes at the character and range level.
SpannableString is an Android class for text markup via Span objects: ForegroundColorSpan, StyleSpan, URLSpan, UnderlineSpan.
Add the .ttf file to the project, specify it in Info.plist under the Fonts provided by application key, then use UIFont(name:).
Place the .ttf file in res/font/, create an XML font family, and specify android:fontFamily or use Typeface.createFromAsset().
Kerning is the space between character pairs. Ligatures are special glyphs for letter combinations. They are controlled via NSKernAttributeName and NSLigatureAttributeName.
Summary
We will develop a mobile application turnkey
IT Sectr creates iOS and Android applications for startups and businesses since 2017. We will advise you and propose the best solution.