I have a request from a customer to use a certain font in a iOS 7 project because it has medieval numbers.
Is there any way to activate those numbers for a NSAttributedString? As default lining numbers are used, that are included in the font as-well.
Here is an example. Both lines have the same font with no variant (Regular), once with medieval numbers activated, the second wit the default lining numbers.
Text figures (also known as non-lining, lowercase, old style, ranging, hanging, medieval, billing, or antique figures or numerals) are numerals designed with varying heights in a fashion that resembles a typical line of running text, hence the name.
Lining figures are all the same height, and align on the baseline and (most commonly) the cap height, thus the name aligning. Old style figures approximate lowercase characters in that they have an x-height as well as fixed-arrangement ascenders and descenders.
Lining Figures (LF)A modern style of numerals also known as short ranging figures or regular numerals, lining figures are all the same height and all figures sit on the baseline. They are generally the same height as the uppercase letters in the typeface.
Lining figures (also called aligning, cap, or modern figures) approximate capital letters in that they are uniform in height, and generally align with the baseline and the cap height. In some traditional typefaces, certain numerals extend slightly above and/or below the baseline and/or the cap height.
These are called lowercase numbers and can be turned on using UIFontDescriptor
.
First, you need to import CoreText for some constants:
#import <CoreText/SFNTLayoutTypes.h>
or
@import CoreText.SFNTLayoutTypes;
Then create font using font descriptor. Here I use Georgia family:
NSDictionary *lowercaseNumbers = @{
UIFontFeatureTypeIdentifierKey: @(kNumberCaseType),
UIFontFeatureSelectorIdentifierKey: @(kLowerCaseNumbersSelector),
};
UIFontDescriptor *descriptor = [[UIFontDescriptor alloc] initWithFontAttributes:
@{
UIFontDescriptorFamilyAttribute: @"Georgia",
UIFontDescriptorFeatureSettingsAttribute:@[ lowercaseNumbers ],
}];
UIFont *font = [UIFont fontWithDescriptor:descriptor size:15];
Result:
Edit: As @Random832 pointed out, Georgia has only lowercase numbers, so the result is irrelevant. However, @vikingosegundo confirmed this code works on supported fonts. Thanks.
The top line was generated with
UIFont *font = [UIFont fontWithName:@"DIN Next LT Pro" size:12];
if (font)
label.font = font;
the second line with
NSDictionary *lowercaseNumbers = @{ UIFontFeatureTypeIdentifierKey:@(kNumberCaseType), UIFontFeatureSelectorIdentifierKey: @(kLowerCaseNumbersSelector)};
UIFontDescriptor *descriptor = [[UIFontDescriptor alloc] initWithFontAttributes:
@{UIFontDescriptorFamilyAttribute: @"DIN Next LT Pro",UIFontDescriptorFeatureSettingsAttribute:@[ lowercaseNumbers ]}];
UIFont *font = [UIFont fontWithDescriptor:descriptor size:12];
if (font)
label.font = font;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With