2014-11-24 21:51:43 +01:00
|
|
|
//
|
2019-02-14 22:20:29 +01:00
|
|
|
// Copyright (c) 2019 Open Whisper Systems. All rights reserved.
|
2014-11-24 21:51:43 +01:00
|
|
|
//
|
|
|
|
|
|
|
|
#import "UIFont+OWS.h"
|
2019-02-21 00:07:08 +01:00
|
|
|
#import <SignalCoreKit/iOSVersions.h>
|
2014-11-24 21:51:43 +01:00
|
|
|
|
2018-01-25 19:53:39 +01:00
|
|
|
NS_ASSUME_NONNULL_BEGIN
|
|
|
|
|
2014-11-24 21:51:43 +01:00
|
|
|
@implementation UIFont (OWS)
|
|
|
|
|
2017-11-30 22:19:36 +01:00
|
|
|
+ (UIFont *)ows_thinFontWithSize:(CGFloat)size
|
|
|
|
{
|
2018-02-23 18:46:04 +01:00
|
|
|
return [UIFont systemFontOfSize:size weight:UIFontWeightThin];
|
2014-11-24 21:51:43 +01:00
|
|
|
}
|
|
|
|
|
2017-11-30 22:19:36 +01:00
|
|
|
+ (UIFont *)ows_lightFontWithSize:(CGFloat)size
|
|
|
|
{
|
2018-02-23 18:46:04 +01:00
|
|
|
return [UIFont systemFontOfSize:size weight:UIFontWeightLight];
|
2014-11-24 21:51:43 +01:00
|
|
|
}
|
|
|
|
|
2017-11-30 22:19:36 +01:00
|
|
|
+ (UIFont *)ows_regularFontWithSize:(CGFloat)size
|
|
|
|
{
|
2018-02-23 18:46:04 +01:00
|
|
|
return [UIFont systemFontOfSize:size weight:UIFontWeightRegular];
|
2014-11-24 21:51:43 +01:00
|
|
|
}
|
|
|
|
|
2017-11-30 22:19:36 +01:00
|
|
|
+ (UIFont *)ows_mediumFontWithSize:(CGFloat)size
|
|
|
|
{
|
2018-02-23 18:46:04 +01:00
|
|
|
return [UIFont systemFontOfSize:size weight:UIFontWeightMedium];
|
2014-12-06 19:26:01 +01:00
|
|
|
}
|
|
|
|
|
2017-11-30 22:19:36 +01:00
|
|
|
+ (UIFont *)ows_boldFontWithSize:(CGFloat)size
|
|
|
|
{
|
2015-09-01 19:22:08 +02:00
|
|
|
return [UIFont boldSystemFontOfSize:size];
|
2014-11-24 21:51:43 +01:00
|
|
|
}
|
|
|
|
|
2019-03-08 06:05:58 +01:00
|
|
|
+ (UIFont *)ows_monospacedDigitFontWithSize:(CGFloat)size;
|
|
|
|
{
|
|
|
|
return [self monospacedDigitSystemFontOfSize:size weight:UIFontWeightRegular];
|
|
|
|
}
|
|
|
|
|
2017-03-14 21:01:12 +01:00
|
|
|
#pragma mark - Icon Fonts
|
|
|
|
|
|
|
|
+ (UIFont *)ows_fontAwesomeFont:(CGFloat)size
|
|
|
|
{
|
|
|
|
return [UIFont fontWithName:@"FontAwesome" size:size];
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (UIFont *)ows_dripIconsFont:(CGFloat)size
|
|
|
|
{
|
|
|
|
return [UIFont fontWithName:@"dripicons-v2" size:size];
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (UIFont *)ows_elegantIconsFont:(CGFloat)size
|
|
|
|
{
|
|
|
|
return [UIFont fontWithName:@"ElegantIcons" size:size];
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - Dynamic Type
|
2015-12-26 17:27:27 +01:00
|
|
|
|
2018-04-06 22:28:34 +02:00
|
|
|
+ (UIFont *)ows_dynamicTypeTitle1Font
|
2017-11-30 22:19:36 +01:00
|
|
|
{
|
2018-04-06 22:28:34 +02:00
|
|
|
return [UIFont preferredFontForTextStyle:UIFontTextStyleTitle1];
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (UIFont *)ows_dynamicTypeTitle2Font
|
|
|
|
{
|
|
|
|
return [UIFont preferredFontForTextStyle:UIFontTextStyleTitle2];
|
2015-12-26 17:27:27 +01:00
|
|
|
}
|
|
|
|
|
2018-04-06 22:28:34 +02:00
|
|
|
+ (UIFont *)ows_dynamicTypeTitle3Font
|
|
|
|
{
|
|
|
|
return [UIFont preferredFontForTextStyle:UIFontTextStyleTitle3];
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (UIFont *)ows_dynamicTypeHeadlineFont
|
|
|
|
{
|
|
|
|
return [UIFont preferredFontForTextStyle:UIFontTextStyleHeadline];
|
|
|
|
}
|
|
|
|
|
2018-07-09 21:28:14 +02:00
|
|
|
+ (UIFont *)ows_dynamicTypeBodyFont
|
2016-10-06 21:47:45 +02:00
|
|
|
{
|
2018-07-09 21:28:14 +02:00
|
|
|
return [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
|
2016-10-06 21:47:45 +02:00
|
|
|
}
|
|
|
|
|
2018-07-09 21:28:14 +02:00
|
|
|
+ (UIFont *)ows_dynamicTypeSubheadlineFont
|
|
|
|
{
|
|
|
|
return [UIFont preferredFontForTextStyle:UIFontTextStyleSubheadline];
|
2018-04-06 22:28:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
+ (UIFont *)ows_dynamicTypeFootnoteFont
|
2017-04-17 21:13:15 +02:00
|
|
|
{
|
|
|
|
return [UIFont preferredFontForTextStyle:UIFontTextStyleFootnote];
|
|
|
|
}
|
|
|
|
|
2018-04-06 22:28:34 +02:00
|
|
|
+ (UIFont *)ows_dynamicTypeCaption1Font
|
2017-11-30 22:19:36 +01:00
|
|
|
{
|
2018-04-06 22:28:34 +02:00
|
|
|
return [UIFont preferredFontForTextStyle:UIFontTextStyleCaption1];
|
2016-10-06 15:44:25 +02:00
|
|
|
}
|
|
|
|
|
2018-04-06 22:28:34 +02:00
|
|
|
+ (UIFont *)ows_dynamicTypeCaption2Font
|
2017-08-14 22:18:54 +02:00
|
|
|
{
|
2018-04-06 22:28:34 +02:00
|
|
|
return [UIFont preferredFontForTextStyle:UIFontTextStyleCaption2];
|
|
|
|
}
|
|
|
|
|
2019-02-14 22:20:29 +01:00
|
|
|
#pragma mark - Dynamic Type Clamped
|
|
|
|
|
|
|
|
+ (UIFont *)preferredFontForTextStyleClamped:(UIFontTextStyle)fontTextStyle
|
|
|
|
{
|
2019-02-14 22:22:51 +01:00
|
|
|
// We clamp the dynamic type sizes at the max size available
|
|
|
|
// without "larger accessibility sizes" enabled.
|
2019-02-14 22:20:29 +01:00
|
|
|
static NSDictionary<UIFontTextStyle, NSNumber *> *maxPointSizeMap = nil;
|
|
|
|
static dispatch_once_t onceToken;
|
|
|
|
dispatch_once(&onceToken, ^{
|
2019-02-21 00:07:08 +01:00
|
|
|
NSMutableDictionary<UIFontTextStyle, NSNumber *> *map = [@{
|
2019-02-14 22:20:29 +01:00
|
|
|
UIFontTextStyleTitle1 : @(34.0),
|
|
|
|
UIFontTextStyleTitle2 : @(28.0),
|
|
|
|
UIFontTextStyleTitle3 : @(26.0),
|
|
|
|
UIFontTextStyleHeadline : @(23.0),
|
|
|
|
UIFontTextStyleBody : @(23.0),
|
|
|
|
UIFontTextStyleSubheadline : @(21.0),
|
|
|
|
UIFontTextStyleFootnote : @(19.0),
|
|
|
|
UIFontTextStyleCaption1 : @(18.0),
|
|
|
|
UIFontTextStyleCaption2 : @(17.0),
|
2019-02-21 00:07:08 +01:00
|
|
|
} mutableCopy];
|
|
|
|
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(11, 0)) {
|
|
|
|
map[UIFontTextStyleLargeTitle] = @(40.0);
|
|
|
|
}
|
|
|
|
maxPointSizeMap = map;
|
2019-02-14 22:20:29 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
UIFont *font = [UIFont preferredFontForTextStyle:fontTextStyle];
|
|
|
|
NSNumber *_Nullable maxPointSize = maxPointSizeMap[fontTextStyle];
|
|
|
|
if (maxPointSize) {
|
|
|
|
if (maxPointSize.floatValue < font.pointSize) {
|
|
|
|
return [font fontWithSize:maxPointSize.floatValue];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
OWSFailDebug(@"Missing max point size for style: %@", fontTextStyle);
|
|
|
|
}
|
|
|
|
|
|
|
|
return font;
|
|
|
|
}
|
|
|
|
|
2019-02-15 20:19:12 +01:00
|
|
|
+ (UIFont *)ows_dynamicTypeLargeTitle1ClampedFont
|
|
|
|
{
|
2019-02-21 00:07:08 +01:00
|
|
|
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(11, 0)) {
|
|
|
|
return [UIFont preferredFontForTextStyleClamped:UIFontTextStyleLargeTitle];
|
|
|
|
} else {
|
|
|
|
return [UIFont preferredFontForTextStyleClamped:UIFontTextStyleTitle1];
|
|
|
|
}
|
2019-02-15 20:19:12 +01:00
|
|
|
}
|
|
|
|
|
2019-02-14 22:20:29 +01:00
|
|
|
+ (UIFont *)ows_dynamicTypeTitle1ClampedFont
|
|
|
|
{
|
|
|
|
return [UIFont preferredFontForTextStyleClamped:UIFontTextStyleTitle1];
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (UIFont *)ows_dynamicTypeTitle2ClampedFont
|
|
|
|
{
|
|
|
|
return [UIFont preferredFontForTextStyleClamped:UIFontTextStyleTitle2];
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (UIFont *)ows_dynamicTypeTitle3ClampedFont
|
|
|
|
{
|
|
|
|
return [UIFont preferredFontForTextStyleClamped:UIFontTextStyleTitle3];
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (UIFont *)ows_dynamicTypeHeadlineClampedFont
|
|
|
|
{
|
|
|
|
return [UIFont preferredFontForTextStyleClamped:UIFontTextStyleHeadline];
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (UIFont *)ows_dynamicTypeBodyClampedFont
|
|
|
|
{
|
|
|
|
return [UIFont preferredFontForTextStyleClamped:UIFontTextStyleBody];
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (UIFont *)ows_dynamicTypeSubheadlineClampedFont
|
|
|
|
{
|
|
|
|
return [UIFont preferredFontForTextStyleClamped:UIFontTextStyleSubheadline];
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (UIFont *)ows_dynamicTypeFootnoteClampedFont
|
|
|
|
{
|
|
|
|
return [UIFont preferredFontForTextStyleClamped:UIFontTextStyleFootnote];
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (UIFont *)ows_dynamicTypeCaption1ClampedFont
|
|
|
|
{
|
|
|
|
return [UIFont preferredFontForTextStyleClamped:UIFontTextStyleCaption1];
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (UIFont *)ows_dynamicTypeCaption2ClampedFont
|
|
|
|
{
|
|
|
|
return [UIFont preferredFontForTextStyleClamped:UIFontTextStyleCaption2];
|
|
|
|
}
|
|
|
|
|
2018-04-06 22:28:34 +02:00
|
|
|
#pragma mark - Styles
|
|
|
|
|
|
|
|
- (UIFont *)ows_italic
|
|
|
|
{
|
|
|
|
return [self styleWithSymbolicTraits:UIFontDescriptorTraitItalic];
|
|
|
|
}
|
|
|
|
|
2018-04-10 19:02:33 +02:00
|
|
|
- (UIFont *)ows_bold
|
|
|
|
{
|
|
|
|
return [self styleWithSymbolicTraits:UIFontDescriptorTraitBold];
|
|
|
|
}
|
|
|
|
|
2018-04-06 22:28:34 +02:00
|
|
|
- (UIFont *)styleWithSymbolicTraits:(UIFontDescriptorSymbolicTraits)symbolicTraits
|
|
|
|
{
|
|
|
|
UIFontDescriptor *fontDescriptor = [self.fontDescriptor fontDescriptorWithSymbolicTraits:symbolicTraits];
|
|
|
|
UIFont *font = [UIFont fontWithDescriptor:fontDescriptor size:0];
|
2018-09-06 19:01:24 +02:00
|
|
|
OWSAssertDebug(font);
|
2018-04-06 22:28:34 +02:00
|
|
|
return font ?: self;
|
|
|
|
}
|
|
|
|
|
2018-04-16 18:39:11 +02:00
|
|
|
- (UIFont *)ows_mediumWeight
|
2018-04-06 22:28:34 +02:00
|
|
|
{
|
|
|
|
// The recommended approach of deriving "medium" weight fonts for dynamic
|
|
|
|
// type fonts is:
|
|
|
|
//
|
|
|
|
// [UIFontDescriptor fontDescriptorByAddingAttributes:...]
|
|
|
|
//
|
|
|
|
// But this doesn't seem to work in practice on iOS 11 using UIFontWeightMedium.
|
|
|
|
|
|
|
|
UIFont *derivedFont = [UIFont systemFontOfSize:self.pointSize weight:UIFontWeightMedium];
|
2018-04-09 22:27:05 +02:00
|
|
|
return derivedFont;
|
2017-08-14 22:18:54 +02:00
|
|
|
}
|
|
|
|
|
2014-11-24 21:51:43 +01:00
|
|
|
@end
|
2018-01-25 19:53:39 +01:00
|
|
|
|
|
|
|
NS_ASSUME_NONNULL_END
|