88#import " NSTextStorage+FontScaling.h"
99
1010typedef NS_OPTIONS (NSInteger , RCTTextSizeComparisonOptions) {
11- RCTTextSizeComparisonSmaller = 1 << 0 ,
12- RCTTextSizeComparisonLarger = 1 << 1 ,
11+ RCTTextSizeComparisonSmaller = 1 << 0 ,
12+ RCTTextSizeComparisonLarger = 1 << 1 ,
1313 RCTTextSizeComparisonWithinRange = 1 << 2 ,
1414};
1515
1616@implementation NSTextStorage (FontScaling)
1717
1818- (void )scaleFontSizeToFitSize : (CGSize)size
1919 minimumFontSize : (CGFloat)minimumFontSize
20- maximumFontSize : (CGFloat)maximumFontSize
21- {
22- CGFloat bottomRatio = 1.0 /128.0 ;
20+ maximumFontSize : (CGFloat)maximumFontSize {
21+ CGFloat bottomRatio = 1.0 / 128.0 ;
2322 CGFloat topRatio = 128.0 ;
2423 CGFloat ratio = 1.0 ;
2524
@@ -32,13 +31,11 @@ - (void)scaleFontSizeToFitSize:(CGSize)size
3231 minimumFontSize: minimumFontSize
3332 maximumFontSize: maximumFontSize];
3433
35- RCTTextSizeComparisonOptions comparsion =
36- [ self compareToSize: size thresholdRatio: 0.01 ];
34+ RCTTextSizeComparisonOptions comparsion = [ self compareToSize: size
35+ thresholdRatio: 0.01 ];
3736
38- if (
39- (comparsion & RCTTextSizeComparisonWithinRange) &&
40- (comparsion & RCTTextSizeComparisonSmaller)
41- ) {
37+ if ((comparsion & RCTTextSizeComparisonWithinRange) &&
38+ (comparsion & RCTTextSizeComparisonSmaller)) {
4239 return ;
4340 } else if (comparsion & RCTTextSizeComparisonSmaller) {
4441 bottomRatio = ratio;
@@ -50,11 +47,9 @@ - (void)scaleFontSizeToFitSize:(CGSize)size
5047 ratio = (topRatio + bottomRatio) / 2.0 ;
5148
5249 CGFloat kRatioThreshold = 0.005 ;
53- if (
54- ABS (topRatio - bottomRatio) < kRatioThreshold ||
50+ if (ABS (topRatio - bottomRatio) < kRatioThreshold ||
5551 ABS (topRatio - ratio) < kRatioThreshold ||
56- ABS (bottomRatio - ratio) < kRatioThreshold
57- ) {
52+ ABS (bottomRatio - ratio) < kRatioThreshold ) {
5853 [self replaceCharactersInRange: (NSRange ){0 , self.length }
5954 withAttributedString: originalAttributedString];
6055
@@ -69,37 +64,38 @@ - (void)scaleFontSizeToFitSize:(CGSize)size
6964 }
7065}
7166
72-
73- - (RCTTextSizeComparisonOptions)compareToSize : (CGSize)size thresholdRatio : (CGFloat)thresholdRatio
74- {
67+ - (RCTTextSizeComparisonOptions)compareToSize : (CGSize)size
68+ thresholdRatio : (CGFloat)thresholdRatio {
7569 NSLayoutManager *layoutManager = self.layoutManagers .firstObject ;
7670 NSTextContainer *textContainer = layoutManager.textContainers .firstObject ;
7771
7872 [layoutManager ensureLayoutForTextContainer: textContainer];
7973
8074 // Does it fit the text container?
8175 NSRange glyphRange = [layoutManager glyphRangeForTextContainer: textContainer];
82- NSRange truncatedGlyphRange = [layoutManager truncatedGlyphRangeInLineFragmentForGlyphAtIndex: glyphRange.length - 1 ];
76+ NSRange truncatedGlyphRange = [layoutManager
77+ truncatedGlyphRangeInLineFragmentForGlyphAtIndex: glyphRange.length - 1 ];
8378
8479 if (truncatedGlyphRange.location != NSNotFound ) {
8580 return RCTTextSizeComparisonLarger;
8681 }
8782
88- CGSize measuredSize = [layoutManager usedRectForTextContainer: textContainer].size ;
83+ CGSize measuredSize =
84+ [layoutManager usedRectForTextContainer: textContainer].size ;
8985
9086 // Does it fit the size?
9187 BOOL fitsSize =
92- size.width >= measuredSize.width &&
93- size.height >= measuredSize.height ;
88+ size.width >= measuredSize.width && size.height >= measuredSize.height ;
9489
9590 CGSize thresholdSize = (CGSize){
96- size.width * thresholdRatio,
97- size.height * thresholdRatio,
91+ size.width * thresholdRatio,
92+ size.height * thresholdRatio,
9893 };
9994
10095 RCTTextSizeComparisonOptions result = 0 ;
10196
102- result |= (fitsSize) ? RCTTextSizeComparisonSmaller : RCTTextSizeComparisonLarger;
97+ result |=
98+ (fitsSize) ? RCTTextSizeComparisonSmaller : RCTTextSizeComparisonLarger;
10399
104100 if (ABS (measuredSize.width - size.width ) < thresholdSize.width ) {
105101 result = result | RCTTextSizeComparisonWithinRange;
@@ -110,26 +106,28 @@ - (RCTTextSizeComparisonOptions)compareToSize:(CGSize)size thresholdRatio:(CGFlo
110106
111107- (void )scaleFontSizeWithRatio : (CGFloat)ratio
112108 minimumFontSize : (CGFloat)minimumFontSize
113- maximumFontSize : (CGFloat)maximumFontSize
114- {
109+ maximumFontSize : (CGFloat)maximumFontSize {
115110 [self beginEditing ];
116111
117- [self enumerateAttribute: NSFontAttributeName
118- inRange: (NSRange ){0 , self.length }
119- options: NSAttributedStringEnumerationLongestEffectiveRangeNotRequired
120- usingBlock:
121- ^(UIFont *_Nullable font, NSRange range, BOOL *_Nonnull stop) {
122- if (!font) {
123- return ;
124- }
125-
126- CGFloat fontSize = MAX (MIN (font.pointSize * ratio, maximumFontSize), minimumFontSize);
127-
128- [self addAttribute: NSFontAttributeName
129- value: [font fontWithSize: fontSize]
130- range: range];
131- }
132- ];
112+ [self
113+ enumerateAttribute: NSFontAttributeName
114+ inRange: (NSRange ){0 , self.length }
115+ options:
116+ NSAttributedStringEnumerationLongestEffectiveRangeNotRequired
117+ usingBlock: ^(
118+ UIFont *_Nullable font, NSRange range, BOOL *_Nonnull stop) {
119+ if (!font) {
120+ return ;
121+ }
122+
123+ CGFloat fontSize =
124+ MAX (MIN (font.pointSize * ratio, maximumFontSize),
125+ minimumFontSize);
126+
127+ [self addAttribute: NSFontAttributeName
128+ value: [font fontWithSize: fontSize]
129+ range: range];
130+ }];
133131
134132 [self endEditing ];
135133}
0 commit comments