---
title: "preferredFont(forTextStyle:compatibleWith:)"
framework: uikit
role: symbol
role_heading: Type Method
path: "uikit/uifont/preferredfont(fortextstyle:compatiblewith:)"
---

# preferredFont(forTextStyle:compatibleWith:)

Returns an instance of the system font for the appropriate text style and traits.

## Declaration

```swift
class func preferredFont(forTextStyle style: UIFont.TextStyle, compatibleWith traitCollection: UITraitCollection?) -> UIFont
```

## Parameters

- `style`: The text style for which to return a font. See doc://com.apple.uikit/documentation/UIKit/UIFont/TextStyle for recognized values.
- `traitCollection`: The traits to use when determining which font to return.

## Return Value

Return Value The system font associated with the specified text style and traits.

## Discussion

Discussion To create a styled font based on a custom font, use a UIFontMetrics object. Because fonts are immutable, any element that adjusts for an updated content size category does not modify the font itself. Instead, the element replaces the assigned font with a new instance based on the original settings.

## See Also

### Creating Fonts

- [Scaling fonts automatically](uikit/scaling-fonts-automatically.md)
- [Creating self-sizing table view cells](uikit/creating-self-sizing-table-view-cells.md)
- [preferredFont(forTextStyle:)](uikit/uifont/preferredfont(fortextstyle:).md)
- [UIFont.TextStyle](uikit/uifont/textstyle.md)
- [init(name:size:)](uikit/uifont/init(name:size:).md)
- [init(descriptor:size:)](uikit/uifont/init(descriptor:size:).md)
- [withSize(_:)](uikit/uifont/withsize(_:).md)
