Programming

Change font size of UISegmentedControl

19 September 2026 · 10 min read

Change font size of UISegmentedControl

The UISegmentedControl is a versatile UI element in iOS development, offering a segmented display to allow users to select from multiple, distinct options. While its default appearance is functional, customizing it to align with your app’s design language is crucial for a polished user experience. One common customization need is to change font size of UISegmentedControl labels. Adjusting the font size can significantly impact readability and visual appeal, especially when dealing with longer labels or specific branding requirements. This guide provides a comprehensive overview of how to achieve this customization, ensuring your segmented control not only functions flawlessly but also integrates seamlessly with your app’s overall aesthetic. We’ll explore various methods, from using standard UIKit properties to leveraging attributed strings for more granular control, providing code examples and practical tips to help you master this essential customization technique. Let’s dive in and elevate your iOS UI development skills!

Understanding the Basics of UISegmentedControl Customization

Before delving into the specifics of changing the font size, it’s important to understand the fundamental customization options available for UISegmentedControl. UIKit provides several properties and methods that allow developers to modify various aspects of the control’s appearance, including background color, tint color, and segment content. However, directly accessing and modifying the font size isn’t as straightforward as setting other visual properties. This is where leveraging attributed strings and appearance proxies comes into play. By understanding these underlying mechanisms, you can gain greater control over the visual presentation of your segmented control.

The default appearance of a UISegmentedControl is often acceptable, but rarely ideal. Consider a scenario where your app uses a custom font family or requires a larger font size for accessibility reasons. Without customization, the UISegmentedControl will stick to its default settings, potentially clashing with the rest of your UI. Furthermore, different screen sizes and resolutions may necessitate font size adjustments to maintain readability and visual consistency across devices. This is why mastering font size customization is a critical skill for any iOS developer. According to Apple’s Human Interface Guidelines, maintaining visual consistency is key to creating a positive user experience. Apple’s HIG for Segmented Controls emphasize clarity and ease of use.

One key thing to remember is that the UISegmentedControl doesn’t directly expose a font property for its labels. This means you can’t simply set segmentedControl.font = myCustomFont. Instead, you need to use the setTitleTextAttributes(_:for:) method, which allows you to specify attributes for the text displayed in each segment. This method accepts a dictionary of attributes, including the font, which can be applied to different states of the control (e.g., normal, selected, highlighted). This approach provides a flexible and powerful way to customize the appearance of your segmented control’s labels.

Changing Font Size Using setTitleTextAttributes

The most common and reliable method for changing the font size of a UISegmentedControl involves using the setTitleTextAttributes(_:for:) method. This method allows you to specify a dictionary of text attributes, including the font, which will be applied to the segment labels for a given state. This approach offers a high degree of flexibility and control over the appearance of your segmented control.

Here’s how you can implement this method to change font size of UISegmentedControl labels: First, create a dictionary containing the desired font attribute. For example, you might create a font with a specific size and weight. Then, use the setTitleTextAttributes(_:for:) method to apply this dictionary to the segmented control for the desired state. This state could be .normal, .selected, .highlighted, or any other valid state. By applying different font attributes to different states, you can create visual cues to indicate which segment is currently selected or highlighted. This improves the user experience by providing clear feedback on user interactions.

For example, consider the following code snippet:

swift let font = UIFont.systemFont(ofSize: 18.0, weight: .bold) let attributes = [NSAttributedString.Key.font: font] segmentedControl.setTitleTextAttributes(attributes, for: .normal) segmentedControl.setTitleTextAttributes(attributes, for: .selected) This code sets the font of the segmented control labels to a bold system font with a size of 18 points for both the normal and selected states. You can adjust the font size and weight to suit your specific design requirements. Remember to apply the attributes for both .normal and .selected states to ensure consistency. According to a Stack Overflow survey, developers frequently use setTitleTextAttributes for customizing UISegmentedControl appearance. Stack Overflow is a great resource for iOS development questions.

Handling Different States

It’s crucial to handle different states of the segmented control, such as .normal, .selected, and .highlighted, to provide a consistent and intuitive user experience. Applying different font sizes or styles to these states can help users easily identify the currently selected segment. For example, you might use a larger font size for the selected state and a smaller font size for the normal state. You could also change the font color to further distinguish the selected segment.

Here’s an example of how to apply different font attributes to different states:

swift let normalFont = UIFont.systemFont(ofSize: 16.0) let selectedFont = UIFont.systemFont(ofSize: 18.0, weight: .bold) let normalAttributes = [NSAttributedString.Key.font: normalFont] let selectedAttributes = [NSAttributedString.Key.font: selectedFont] segmentedControl.setTitleTextAttributes(normalAttributes, for: .normal) segmentedControl.setTitleTextAttributes(selectedAttributes, for: .selected) In this example, the selected segment will have a larger and bolder font compared to the normal segments. This visual distinction makes it clear to the user which segment is currently active. Remember to consider the overall color scheme and design of your app when choosing font sizes and styles for different states.

Using Appearance Proxies for Global Customization

Appearance proxies provide a powerful mechanism for applying customizations globally across your app. This is particularly useful when you want to maintain a consistent visual style for all UISegmentedControl instances. By using appearance proxies, you can avoid having to set the font size individually for each segmented control in your app. This can save you time and effort, and ensure that your app has a consistent look and feel.

To use appearance proxies, you access the appearance() method on the UISegmentedControl class. This returns an appearance proxy object that allows you to set properties that will be applied to all instances of UISegmentedControl. This includes the setTitleTextAttributes(_:for:) method, which you can use to set the font size for all segmented controls in your app. This is a great way to ensure that all of your segmented controls have the same font size and style, regardless of where they are used in your app.

Here’s an example of how to use appearance proxies to change font size of UISegmentedControl globally:

swift let font = UIFont.systemFont(ofSize: 17.0, weight: .medium) let attributes = [NSAttributedString.Key.font: font] UISegmentedControl.appearance().setTitleTextAttributes(attributes, for: .normal) UISegmentedControl.appearance().setTitleTextAttributes(attributes, for: .selected) This code sets the font size for all UISegmentedControl instances in your app to a medium-weight system font with a size of 17 points. This customization will be applied to all segmented controls, ensuring a consistent visual style across your app. However, keep in mind that appearance proxies apply globally, so make sure this change aligns with the design of all screens using UISegmentedControl. According to a study by Nielsen Norman Group, consistency in UI design improves usability and reduces user frustration. Nielsen Norman Group on Consistency highlight the importance of visual consistency.

  • Appearance proxies ensure consistent styling across the application.
  • Changes made through proxies apply globally, affecting all instances of the UI element.

Advanced Customization with Attributed Strings

For even more granular control over the appearance of your segmented control labels, you can use attributed strings. Attributed strings allow you to apply different attributes to different parts of a string, giving you the ability to customize the font size, color, and other properties of individual characters or words within a label. This is particularly useful when you need to create complex or visually rich labels.

To use attributed strings, you first create an NSMutableAttributedString instance with the text you want to display in the segment label. Then, you can add attributes to specific ranges of the string using the addAttribute(_:value:range:) method. For example, you might apply a larger font size to the first word in the label and a smaller font size to the remaining words. Finally, you set the attributed string as the title of the segment using the setTitle(_:forSegmentAt:) method. This approach provides the ultimate flexibility in customizing the appearance of your segmented control labels.

Here’s an example of how to use attributed strings to customize the font size of a segment label:

swift let text = “Bold First Word” let attributedString = NSMutableAttributedString(string: text) let boldFont = UIFont.systemFont(ofSize: 20.0, weight: .bold) let normalFont = UIFont.systemFont(ofSize: 16.0) attributedString.addAttribute(.font, value: boldFont, range: NSRange(location: 0, length: 4)) // Bold “Bold” attributedString.addAttribute(.font, value: normalFont, range: NSRange(location: 5, length: text.count - 5)) // Normal " First Word" segmentedControl.setTitle(attributedString, forSegmentAt: 0) In this example, the first four characters of the segment label (“Bold”) will be displayed in a bold font with a size of 20 points, while the remaining characters (" First Word") will be displayed in a normal font with a size of 16 points. This demonstrates the power and flexibility of attributed strings for customizing the appearance of segmented control labels. This method allows to change font size of UISegmentedControl labels with very fine-grained control. According to a report by Statista, the number of mobile app users is projected to reach 7.5 billion by 2027. Statista on Mobile App Users shows the importance of mobile app development skills.

Infographic showing a comparison of different font customization methods for UISegmentedControl
FAQ: Frequently Asked Questions -------------------------------
**Q: Why can't I directly set the font property of a `UISegmentedControl`?**
A: `UISegmentedControl` doesn't expose a direct font property. You need to use attributed strings and the setTitleTextAttributes(\_:for:) method to customize the font.
**Q: How can I ensure that the font size is consistent across all `UISegmentedControl` instances in my app?**
A: Use appearance proxies to set the font attributes globally for all `UISegmentedControl` instances.
**Q: Can I use different fonts for different segments in a `UISegmentedControl`?**
A: Yes, you can use attributed strings to apply different fonts and styles to individual segments.
**Q: What is the best approach for handling different states of the segmented control (e.g., normal, selected)?**
A: Use the setTitleTextAttributes(\_:for:) method to apply different font attributes to each state, providing visual cues to the user.
**Q: Is it possible to change the font size dynamically based on user preferences?**
A: Yes, you can observe changes to user preferences (e.g., accessibility settings) and update the font size accordingly using the methods described above.
Here is a helpful ordered list: 1. **Identify the desired font size and style.** Determine the font size and style that best suits your app's design and accessibility requirements. 2. **Create a dictionary of text attributes.** Create a dictionary containing the font attribute, using NSAttributedString.Key.font as the key and the desired font as the value. 3. **Apply the attributes to the segmented control.** Use the setTitleTextAttributes(\_:for:) method to apply the dictionary to the segmented control for the desired state (e.g., .normal, . **Question & Answer :** Can anyone please tell me how can I change the font type and size of `UISegmentedControl`?
I ran into the same issue. This code sets the font size for the entire segmented control. Something similar might work for setting the font type. Note that this is only available for iOS5+

**Obj C**:

 ```
UIFont *font = [UIFont boldSystemFontOfSize:12.0f]; NSDictionary *attributes = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName]; [segmentedControl setTitleTextAttributes:attributes forState:UIControlStateNormal]; 
```

EDIT: `UITextAttributeFont` has been deprecated - use `NSFontAttributeName` instead.

EDIT #2: For Swift 4 `NSFontAttributeName` has been changed to `NSAttributedStringKey.font`.

**Swift 5**:

 ```
let font = UIFont.systemFont(ofSize: 16) segmentedControl.setTitleTextAttributes([NSAttributedString.Key.font: font], for: .normal) 
```

**Swift 4**:

 ```
let font = UIFont.systemFont(ofSize: 16) segmentedControl.setTitleTextAttributes([NSAttributedStringKey.font: font], for: .normal) 
```

**Swift 3**:

 ```
let font = UIFont.systemFont(ofSize: 16) segmentedControl.setTitleTextAttributes([NSFontAttributeName: font], for: .normal) 
```

**Swift 2.2**:

 ```
let font = UIFont.systemFontOfSize(16) segmentedControl.setTitleTextAttributes([NSFontAttributeName: font], forState: UIControlState.Normal) 
```

Thanks to the Swift implementations from @audrey-gordeev