Working with Font Sizes

Operating system requirements: iOS 5.0 or later.

Mobile device: iPad.

Description

This example describes some methods of working with font sizes. After starting the example the following operations are executed:

Source Code

Executing the example requires to place the following code in the body of the executeExample method of the ViewController class (see the Displaying of Express Report section):

// Get font sizes
int smallest = FONT_SIZE_SMALLEST;
int small = FONT_SIZE_SMALL;
int medium = FONT_SIZE_MEDIUM;
int large = FONT_SIZE_LARGE;
int largest = FONT_SIZE_LARGEST;
// Get font names by specified sizes
NSString *sizeNameSmallest = [FontSizeViewController sizeNameForSize:smallest];
NSString *sizeNameSmall = [FontSizeViewController sizeNameForSize:small];
NSString *sizeNameMedium = [FontSizeViewController sizeNameForSize:medium];
NSString *sizeNameLarge = [FontSizeViewController sizeNameForSize:large];
NSString *sizeNameLargest = [FontSizeViewController sizeNameForSize:largest];
// Get array of font family names
NSArray *fontFamilyNames = [[NSArray alloc] initWithArray:[UIFont familyNames]];
// Get font family name from the array
NSString *fontFamilyName = [fontFamilyNames objectAtIndex:0];
// Get font objects
UIFont *font1 = [UIFont fontWithFamily:fontFamilyName bold:NO italic:NO withSize: smallest];
UIFont *font2 = [UIFont fontWithFamily:fontFamilyName bold:NO italic:NO withSize: small];
UIFont *font3 = [UIFont fontWithFamily:fontFamilyName bold:NO italic:NO withSize: medium];
UIFont *font4 = [UIFont fontWithFamily:fontFamilyName bold:NO italic:NO withSize: large];
UIFont *font5 = [UIFont fontWithFamily:fontFamilyName bold:NO italic:NO withSize: largest];
// Get font name by font family name
NSString *fontName = [UIFont fontNameWithFamily:fontFamilyName bold:NO italic:NO];
// Create strings with font description
NSString *labelText1 = [NSString stringWithFormat:@"Name: %@, size: %d, font: %@", sizeNameSmallest, smallest, fontName];
NSString *labelText2 = [NSString stringWithFormat:@"Name: %@, size: %d, font: %@", sizeNameSmall, small, fontName];
NSString *labelText3 = [NSString stringWithFormat:@"Name: %@, size: %d font: %@", sizeNameMedium,medium, fontName];
NSString *labelText4 = [NSString stringWithFormat:@"Name: %@, size: %d, font: %@", sizeNameLarge, large, fontName];
NSString *labelText5 = [NSString stringWithFormat:@"Name: %@, size: %d, font: %@", sizeNameLargest, largest, fontName];
// Create labels
UILabel *label1 = [[UILabel alloc] init];
UILabel *label2 = [[UILabel alloc] init];
UILabel *label3 = [[UILabel alloc] init];
UILabel *label4 = [[UILabel alloc] init];
UILabel *label5 = [[UILabel alloc] init];
// Set label text
[label1 setText:labelText1];
[label2 setText:labelText2];
[label3 setText:labelText3];
[label4 setText:labelText4];
[label5 setText:labelText5];
// Set label font
[label1 setFont:font1];
[label2 setFont:font2];
[label3 setFont:font3];
[label4 setFont:font4];
[label5 setFont:font5];
// Set text alignment in labels
[label1 setTextAlignment: NSTextAlignmentCenter];
[label2 setTextAlignment: NSTextAlignmentCenter];
[label3 setTextAlignment: NSTextAlignmentCenter];
[label4 setTextAlignment: NSTextAlignmentCenter];
[label5 setTextAlignment: NSTextAlignmentCenter];
// Set label borders
[label1 setFrame:CGRectMake(0, 0, 700, 50)];
[label2 setFrame:CGRectMake(0, 50, 700, 50)];
[label3 setFrame:CGRectMake(0, 100, 700, 50)];
[label4 setFrame:CGRectMake(0, 150, 700, 50)];
[label5 setFrame:CGRectMake(0, 200, 700, 50)];
// Remove all window subviews
NSArray *subviews = [self.view subviews];
for(UIView *subView in subviews)
{
    [subView removeFromSuperview];
}
// Display labels
[self.view addSubview:label1];
[self.view addSubview:label2];
[self.view addSubview:label3];
[self.view addSubview:label4];
[self.view addSubview:label5];

After executing the example the mobile device screen displays the labels containing font size names, the values corresponding to various font sizes and also the font names used to display label text:

See also:

Examples of Component Use