iOS Developer Library — Pre-Release

Developer

UIKit Framework Reference UIFont Class Reference

Options
Deployment Target:

On This Page
Language:

UIFont

The UIFont class provides the interface for getting and setting font information. The class provides you with access to the font’s characteristics and also provides the system with access to the font’s glyph information, which is used during layout. You use font objects by passing them to methods that accept them as a parameter.

You do not create UIFont objects using the alloc and init methods. Instead, you use class methods of UIFont, such as preferredFontForTextStyle:, to look up and retrieve the desired font object. These methods check for an existing font object with the specified characteristics and return it if it exists. Otherwise, they create a new font object based on the desired font characteristics.

Font objects are immutable and so it is safe to use them from multiple threads in your app.

Inheritance


Conforms To


Import Statement


Swift

import UIKit

Objective-C

@import UIKit;

Availability


Available in iOS 2.0 and later.
  • Returns an instance of the font associated with the text style and scaled appropriately for the user's selected content size category.

    Declaration

    Swift

    class func preferredFontForTextStyle(_ style: String) -> UIFont

    Objective-C

    + (UIFont *)preferredFontForTextStyle:(NSString *)style

    Parameters

    style

    The text style for which to return a font. See "Text Styles” in UIFontDescriptor Class Reference for recognized values.

    Return Value

    The font associated with the specified text style.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 7.0 and later.

  • Creates and returns a font object for the specified font name and size.

    Declaration

    Swift

    init?(name fontName: String, size fontSize: CGFloat) -> UIFont

    Objective-C

    + (UIFont *)fontWithName:(NSString *)fontName size:(CGFloat)fontSize

    Parameters

    fontName

    The fully specified name of the font. This name incorporates both the font family name and the specific style information for the font.

    fontSize

    The size (in points) to which the font is scaled. This value must be greater than 0.0.

    Return Value

    A font object of the specified name and size.

    Discussion

    You can use the fontNamesForFamilyName: method to retrieve the specific font names for a given font family.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 2.0 and later.

  • Returns a font object that is the same as the receiver but which has the specified size instead.

    Declaration

    Swift

    func fontWithSize(_ fontSize: CGFloat) -> UIFont

    Objective-C

    - (UIFont *)fontWithSize:(CGFloat)fontSize

    Parameters

    fontSize

    The desired size (in points) of the new font object. This value must be greater than 0.0.

    Return Value

    A font object of the specified size.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 2.0 and later.

  • Returns the font object used for standard interface items in the specified size.

    Declaration

    Swift

    class func systemFontOfSize(_ fontSize: CGFloat) -> UIFont

    Objective-C

    + (UIFont *)systemFontOfSize:(CGFloat)fontSize

    Parameters

    fontSize

    The size (in points) to which the font is scaled. This value must be greater than 0.0.

    Return Value

    A font object of the specified size.

    Discussion

    Instead of using this method to get a font, it’s often more appropriate to use preferredFontForTextStyle: because that method respects the user’s selected content size category.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 2.0 and later.

  • Returns the font object used for standard interface items in the specified size and weight.

    Declaration

    Swift

    class func systemFontOfSize(_ fontSize: CGFloat, weight weight: CGFloat) -> UIFont!

    Objective-C

    + (UIFont *)systemFontOfSize:(CGFloat)fontSize weight:(CGFloat)weight

    Parameters

    fontSize

    The size (in points) to which the font is scaled. This value must be greater than 0.0.

    weight

    The weight of the font, specified by a UIFontDescriptor font weight constant (for a list of values you can use, see Font Weights). Avoid passing an arbitrary floating-point number for weight, because a font might not include a variant for every weight.

    Return Value

    A font object of the specified size and weight.

    Discussion

    Instead of using this method to get a font, it’s often more appropriate to use preferredFontForTextStyle: because that method respects the user’s selected content size category.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 8.2 and later.

  • Returns the font object used for standard interface items that are rendered in boldface type in the specified size.

    Declaration

    Swift

    class func boldSystemFontOfSize(_ fontSize: CGFloat) -> UIFont

    Objective-C

    + (UIFont *)boldSystemFontOfSize:(CGFloat)fontSize

    Parameters

    fontSize

    The size (in points) to which the font is scaled. This value must be greater than 0.0.

    Return Value

    A font object of the specified size.

    Discussion

    Instead of using this method to get a font, it’s often more appropriate to use preferredFontForTextStyle: because that method respects the user’s selected content size category.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 2.0 and later.

  • Returns the font object used for standard interface items that are rendered in italic type in the specified size.

    Declaration

    Swift

    class func italicSystemFontOfSize(_ fontSize: CGFloat) -> UIFont

    Objective-C

    + (UIFont *)italicSystemFontOfSize:(CGFloat)fontSize

    Parameters

    fontSize

    The size (in points) to which the font is scaled. This value must be greater than 0.0.

    Return Value

    A font object of the specified size.

    Discussion

    Instead of using this method to get a font, it’s often more appropriate to use preferredFontForTextStyle: because that method respects the user’s selected content size category.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 2.0 and later.

  • Returns an array of font family names available on the system.

    Declaration

    Swift

    class func familyNames() -> [AnyObject]

    Objective-C

    + (NSArray *)familyNames

    Return Value

    An array of NSString objects, each of which contains the name of a font family.

    Discussion

    Font family names correspond to the base name of a font, such as Times New Roman. You can pass the returned strings to the fontNamesForFamilyName: method to retrieve a list of font names available for that family. You can then use the corresponding font name to retrieve an actual font object.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 2.0 and later.

  • Returns an array of font names available in a particular font family.

    Declaration

    Swift

    class func fontNamesForFamilyName(_ familyName: String) -> [AnyObject]

    Objective-C

    + (NSArray *)fontNamesForFamilyName:(NSString *)familyName

    Parameters

    familyName

    The name of the font family. Use the familyNames method to get an array of the available font family names on the system.

    Return Value

    An array of NSString objects, each of which contains a font name associated with the specified family.

    Discussion

    You can pass the returned strings as parameters to the fontWithName:size: method to retrieve an actual font object.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 2.0 and later.

  • The font family name. (read-only)

    Declaration

    Swift

    var familyName: String { get }

    Objective-C

    @property(nonatomic, readonly, retain) NSString *familyName

    Discussion

    A family name is a name such as Times New Roman that identifies one or more specific fonts. The value in this property is intended for an application’s internal usage only and should not be displayed.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 2.0 and later.

  • fontName fontName Property

    The font face name. (read-only)

    Declaration

    Swift

    var fontName: String { get }

    Objective-C

    @property(nonatomic, readonly, retain) NSString *fontName

    Discussion

    The font name is a name such as HelveticaBold that incorporates the family name and any specific style information for the font. The value in this property is intended for an application’s internal usage only and should not be displayed.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 2.0 and later.

  • pointSize pointSize Property

    The receiver’s point size, or the effective vertical point size for a font with a nonstandard matrix. (read-only)

    Declaration

    Swift

    var pointSize: CGFloat { get }

    Objective-C

    @property(nonatomic, readonly) CGFloat pointSize

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 2.0 and later.

  • ascender ascender Property

    The top y-coordinate, offset from the baseline, of the receiver’s longest ascender. (read-only)

    Declaration

    Swift

    var ascender: CGFloat { get }

    Objective-C

    @property(nonatomic, readonly) CGFloat ascender

    Discussion

    The ascender value is measured in points.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 2.0 and later.

    See Also

    descender

  • descender descender Property

    The bottom y-coordinate, offset from the baseline, of the receiver’s longest descender. (read-only)

    Declaration

    Swift

    var descender: CGFloat { get }

    Objective-C

    @property(nonatomic, readonly) CGFloat descender

    Discussion

    The descender value is measured in points. This value may be positive or negative. For example, if the longest descender extends 2 points below the baseline, this method returns -2.0 .

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 2.0 and later.

    See Also

    ascender

  • leading leading Property

    The receiver’s leading information. (read-only)

    Use the lineHeight property instead.

    Declaration

    Swift

    var leading: CGFloat { get }

    Objective-C

    @property(nonatomic, readonly) CGFloat leading

    Discussion

    The leading value represents the spacing between lines of text and is measured (in points) from baseline to baseline.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 2.0 and later.

  • capHeight capHeight Property

    The receiver’s cap height information. (read-only)

    Declaration

    Swift

    var capHeight: CGFloat { get }

    Objective-C

    @property(nonatomic, readonly) CGFloat capHeight

    Discussion

    This value measures (in points) the height of a capital character.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 2.0 and later.

  • xHeight xHeight Property

    The x-height of the receiver. (read-only)

    Declaration

    Swift

    var xHeight: CGFloat { get }

    Objective-C

    @property(nonatomic, readonly) CGFloat xHeight

    Discussion

    This value measures (in points) the height of the lowercase character "x".

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 2.0 and later.

  • The height of text lines (measured in points). (read-only)

    Declaration

    Swift

    var lineHeight: CGFloat { get }

    Objective-C

    @property(nonatomic, readonly) CGFloat lineHeight

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 4.0 and later.

  • Returns the standard font size used for labels.

    Declaration

    Swift

    class func labelFontSize() -> CGFloat

    Objective-C

    + (CGFloat)labelFontSize

    Return Value

    The standard label font size in points.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 2.0 and later.

  • Returns the standard font size used for buttons.

    Declaration

    Swift

    class func buttonFontSize() -> CGFloat

    Objective-C

    + (CGFloat)buttonFontSize

    Return Value

    The standard button font size in points.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 2.0 and later.

  • Returns the size of the standard small system font.

    Declaration

    Swift

    class func smallSystemFontSize() -> CGFloat

    Objective-C

    + (CGFloat)smallSystemFontSize

    Return Value

    The standard small system font size in points.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 2.0 and later.

  • Returns the size of the standard system font.

    Declaration

    Swift

    class func systemFontSize() -> CGFloat

    Objective-C

    + (CGFloat)systemFontSize

    Return Value

    The standard system font size in points.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 2.0 and later.

  • Returns a font descriptor that describes the font.

    Declaration

    Swift

    func fontDescriptor() -> UIFontDescriptor

    Objective-C

    - (UIFontDescriptor *)fontDescriptor

    Return Value

    A font descriptor that describes the font.

    Discussion

    The font descriptor contains a mutable dictionary of optional attributes for creating a UIFont object. See UIFontDescriptor Class Reference for more information.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 7.0 and later.

  • Returns a font matching the given font descriptor.

    Declaration

    Swift

    init(descriptor descriptor: UIFontDescriptor, size pointSize: CGFloat) -> UIFont

    Objective-C

    + (UIFont *)fontWithDescriptor:(UIFontDescriptor *)descriptor size:(CGFloat)pointSize

    Parameters

    descriptor

    The font descriptor to match.

    pointSize

    The size in points to which the font is scaled. If greater than 0.0, it has precedence over UIFontDescriptorSizeAttribute in descriptor.

    Return Value

    A font object for the specified descriptor and size.

    Discussion

    In most cases, you can simply use fontWithName:size: to create standard scaled fonts.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 7.0 and later.