mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
Remove duplicated category NSTextStorage+FontScaling (#41437)
Summary: We have two same categories of `NSTextStorage+FontScaling`, another one is https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Text/Text/NSTextStorage%2BFontScaling.h. Only one category is valid, so we can delete one. ## Changelog: [IOS] [FIXED] - Remove duplicated category NSTextStorage+FontScaling Pull Request resolved: https://github.com/facebook/react-native/pull/41437 Test Plan: Null. Reviewed By: rshest Differential Revision: D51253572 Pulled By: javache fbshipit-source-id: 4229351bf29699076d740defec64b61ea2151063
This commit is contained in:
committed by
Facebook GitHub Bot
parent
4c0d20dfbf
commit
6eef3661bf
-20
@@ -1,20 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
#import <UIKit/UIKit.h>
|
||||
|
||||
@interface NSTextStorage (FontScaling)
|
||||
|
||||
- (void)scaleFontSizeToFitSize:(CGSize)size
|
||||
minimumFontSize:(CGFloat)minimumFontSize
|
||||
maximumFontSize:(CGFloat)maximumFontSize;
|
||||
|
||||
- (void)scaleFontSizeWithRatio:(CGFloat)ratio
|
||||
minimumFontSize:(CGFloat)minimumFontSize
|
||||
maximumFontSize:(CGFloat)maximumFontSize;
|
||||
|
||||
@end
|
||||
-117
@@ -1,117 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
#import "NSTextStorage+FontScaling.h"
|
||||
|
||||
typedef NS_OPTIONS(NSInteger, RCTTextSizeComparisonOptions) {
|
||||
RCTTextSizeComparisonSmaller = 1 << 0,
|
||||
RCTTextSizeComparisonLarger = 1 << 1,
|
||||
RCTTextSizeComparisonWithinRange = 1 << 2,
|
||||
};
|
||||
|
||||
@implementation NSTextStorage (FontScaling)
|
||||
|
||||
- (void)scaleFontSizeToFitSize:(CGSize)size
|
||||
minimumFontSize:(CGFloat)minimumFontSize
|
||||
maximumFontSize:(CGFloat)maximumFontSize
|
||||
{
|
||||
CGFloat bottomRatio = 1.0 / 128.0;
|
||||
CGFloat topRatio = 128.0;
|
||||
CGFloat ratio = 1.0;
|
||||
|
||||
NSAttributedString *originalAttributedString = [self copy];
|
||||
|
||||
CGFloat lastRatioWhichFits = 0.02;
|
||||
|
||||
while (true) {
|
||||
[self scaleFontSizeWithRatio:ratio minimumFontSize:minimumFontSize maximumFontSize:maximumFontSize];
|
||||
|
||||
RCTTextSizeComparisonOptions comparison = [self compareToSize:size thresholdRatio:0.01];
|
||||
|
||||
if ((comparison & RCTTextSizeComparisonWithinRange) && (comparison & RCTTextSizeComparisonSmaller)) {
|
||||
return;
|
||||
} else if (comparison & RCTTextSizeComparisonSmaller) {
|
||||
bottomRatio = ratio;
|
||||
lastRatioWhichFits = ratio;
|
||||
} else {
|
||||
topRatio = ratio;
|
||||
}
|
||||
|
||||
ratio = (topRatio + bottomRatio) / 2.0;
|
||||
|
||||
CGFloat kRatioThreshold = 0.005;
|
||||
if (ABS(topRatio - bottomRatio) < kRatioThreshold || ABS(topRatio - ratio) < kRatioThreshold ||
|
||||
ABS(bottomRatio - ratio) < kRatioThreshold) {
|
||||
[self replaceCharactersInRange:(NSRange){0, self.length} withAttributedString:originalAttributedString];
|
||||
|
||||
[self scaleFontSizeWithRatio:lastRatioWhichFits minimumFontSize:minimumFontSize maximumFontSize:maximumFontSize];
|
||||
return;
|
||||
}
|
||||
|
||||
[self replaceCharactersInRange:(NSRange){0, self.length} withAttributedString:originalAttributedString];
|
||||
}
|
||||
}
|
||||
|
||||
- (RCTTextSizeComparisonOptions)compareToSize:(CGSize)size thresholdRatio:(CGFloat)thresholdRatio
|
||||
{
|
||||
NSLayoutManager *layoutManager = self.layoutManagers.firstObject;
|
||||
NSTextContainer *textContainer = layoutManager.textContainers.firstObject;
|
||||
|
||||
[layoutManager ensureLayoutForTextContainer:textContainer];
|
||||
|
||||
// Does it fit the text container?
|
||||
NSRange glyphRange = [layoutManager glyphRangeForTextContainer:textContainer];
|
||||
NSRange truncatedGlyphRange = [layoutManager truncatedGlyphRangeInLineFragmentForGlyphAtIndex:glyphRange.length - 1];
|
||||
|
||||
if (truncatedGlyphRange.location != NSNotFound) {
|
||||
return RCTTextSizeComparisonLarger;
|
||||
}
|
||||
|
||||
CGSize measuredSize = [layoutManager usedRectForTextContainer:textContainer].size;
|
||||
|
||||
// Does it fit the size?
|
||||
BOOL fitsSize = size.width >= measuredSize.width && size.height >= measuredSize.height;
|
||||
|
||||
CGSize thresholdSize = (CGSize){
|
||||
size.width * thresholdRatio,
|
||||
size.height * thresholdRatio,
|
||||
};
|
||||
|
||||
RCTTextSizeComparisonOptions result = 0;
|
||||
|
||||
result |= (fitsSize) ? RCTTextSizeComparisonSmaller : RCTTextSizeComparisonLarger;
|
||||
|
||||
if (ABS(measuredSize.width - size.width) < thresholdSize.width) {
|
||||
result = result | RCTTextSizeComparisonWithinRange;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
- (void)scaleFontSizeWithRatio:(CGFloat)ratio
|
||||
minimumFontSize:(CGFloat)minimumFontSize
|
||||
maximumFontSize:(CGFloat)maximumFontSize
|
||||
{
|
||||
[self beginEditing];
|
||||
|
||||
[self enumerateAttribute:NSFontAttributeName
|
||||
inRange:(NSRange){0, self.length}
|
||||
options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired
|
||||
usingBlock:^(UIFont *_Nullable font, NSRange range, BOOL *_Nonnull stop) {
|
||||
if (!font) {
|
||||
return;
|
||||
}
|
||||
|
||||
CGFloat fontSize = MAX(MIN(font.pointSize * ratio, maximumFontSize), minimumFontSize);
|
||||
|
||||
[self addAttribute:NSFontAttributeName value:[font fontWithSize:fontSize] range:range];
|
||||
}];
|
||||
|
||||
[self endEditing];
|
||||
}
|
||||
|
||||
@end
|
||||
+1
-1
@@ -7,9 +7,9 @@
|
||||
|
||||
#import "RCTTextLayoutManager.h"
|
||||
|
||||
#import "NSTextStorage+FontScaling.h"
|
||||
#import "RCTAttributedTextUtils.h"
|
||||
|
||||
#import <React/NSTextStorage+FontScaling.h>
|
||||
#import <React/RCTUtils.h>
|
||||
#import <react/utils/ManagedObjectWrapper.h>
|
||||
#import <react/utils/SimpleThreadSafeCache.h>
|
||||
|
||||
Reference in New Issue
Block a user