-
Notifications
You must be signed in to change notification settings - Fork 24.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
iOS: Support allowFontScaling on TextInput
Summary: Currently, only `Text` supports the `allowFontScaling` prop. This commit adds support for it on `TextInput`. As part of this change, the TextInput setters for font attributes (e.g. size, weight) had to be refactored. The problem with them is that they use RCTFont's helpers which create a new font based on an existing font. These helpers lose information. In particular, they lose the scaleMultiplier. For example, suppose the font size is 12 and the device's font multiplier is set to 1.5. So we'd create a font with size 12 and scaleMultiplier 1.5 which is an effective size of 18 (which is the only thing stored in the font). Next, suppose the device's font multiplier changes to 1. So we'd use an RCTFont helper to create a new font based on the existing font but with a scaleMultiplier of 1. However, the font didn't store the font size (12) and scaleMultiplier (1.5) separately. It just knows the (effective) font size of 18. So RCTFont thinks the new font has a font size of 18 and a scaleMultiplier of 1 so its effective font size is 18. This is incorrect and it should have been 12. To fix this, the font attributes are now all stored individually. Anytime one of them changes, updateFont is called which recreates the font from scratch. This happens to fix some bugs around fontStyle and fontWeight which were reported several times before: #13730, #12738, #2140, #8533. Created a test app where I verified that `allowFontScaling` works properly for `TextInputs` for all values (`undefined`, `true`, `false`) for a variety of `TextInputs`: - Singleline TextInput - Singleline TextInput's placeholder - Multiline TextInput - Multiline TextInput's placeholder - Multiline TextInput using children instead of `value` Also, verified switching `fontSize`, `fontWeight`, `fontStyle` and `fontFamily` through a bunch of combinations works properly. Lastly, my team has been using this change in our app. Adam Comella Microsoft Corp. Closes #14030 Reviewed By: TheSavior Differential Revision: D5899959 Pulled By: shergin fbshipit-source-id: c8c8c4d4d670cd2a142286e79bfffef3b58cecd3
- Loading branch information
1 parent
cd74e46
commit 9c4ec30
Showing
11 changed files
with
213 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/** | ||
* Copyright (c) 2015-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
|
||
#import <UIKit/UIKit.h> | ||
|
||
#import "RCTFontAttributesDelegate.h" | ||
|
||
@class RCTAccessibilityManager; | ||
|
||
@interface RCTFontAttributes : NSObject | ||
|
||
@property (nonatomic, weak) id<RCTFontAttributesDelegate> delegate; | ||
|
||
@property (readonly, nonatomic, strong) UIFont *font; | ||
|
||
@property (nonatomic, assign) BOOL allowFontScaling; | ||
@property (nonatomic, copy) NSString *fontFamily; | ||
@property (nonatomic, strong) NSNumber *fontSize; | ||
@property (nonatomic, assign) CGFloat fontSizeMultiplier; | ||
@property (nonatomic, copy) NSString *fontStyle; | ||
@property (nonatomic, copy) NSString *fontWeight; | ||
|
||
- (instancetype)initWithAccessibilityManager:(RCTAccessibilityManager *)accessibilityManager; | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/** | ||
* Copyright (c) 2015-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
|
||
#import "RCTFontAttributes.h" | ||
|
||
#import <React/RCTAccessibilityManager.h> | ||
#import <React/RCTAssert.h> | ||
#import <React/RCTFont.h> | ||
#import <React/RCTLog.h> | ||
|
||
@interface RCTFontAttributes () | ||
{ | ||
RCTAccessibilityManager *_accessibilityManager; | ||
} | ||
|
||
@property (nonatomic, strong) UIFont *font; | ||
|
||
@end | ||
|
||
@implementation RCTFontAttributes | ||
|
||
- (instancetype)initWithAccessibilityManager:(RCTAccessibilityManager *)accessibilityManager | ||
{ | ||
RCTAssertParam(accessibilityManager); | ||
|
||
if (self = [super init]) { | ||
_accessibilityManager = accessibilityManager; | ||
_fontSizeMultiplier = _accessibilityManager.multiplier; | ||
|
||
[[NSNotificationCenter defaultCenter] addObserver:self | ||
selector:@selector(contentSizeMultiplierDidChange) | ||
name:RCTAccessibilityManagerDidUpdateMultiplierNotification | ||
object:_accessibilityManager]; | ||
|
||
[self updateFont]; | ||
} | ||
return self; | ||
} | ||
|
||
- (void)dealloc | ||
{ | ||
[[NSNotificationCenter defaultCenter] removeObserver:self]; | ||
} | ||
|
||
- (void)contentSizeMultiplierDidChange | ||
{ | ||
self.fontSizeMultiplier = _accessibilityManager.multiplier; | ||
} | ||
|
||
- (void)setAllowFontScaling:(BOOL)allowFontScaling | ||
{ | ||
_allowFontScaling = allowFontScaling; | ||
[self updateFont]; | ||
} | ||
|
||
- (void)setFontFamily:(NSString *)fontFamily | ||
{ | ||
_fontFamily = fontFamily; | ||
[self updateFont]; | ||
} | ||
|
||
- (void)setFontSize:(NSNumber *)fontSize | ||
{ | ||
_fontSize = fontSize; | ||
[self updateFont]; | ||
} | ||
|
||
- (void)setFontSizeMultiplier:(CGFloat)fontSizeMultiplier | ||
{ | ||
_fontSizeMultiplier = fontSizeMultiplier; | ||
|
||
if (_fontSizeMultiplier == 0) { | ||
RCTLogError(@"fontSizeMultiplier value must be > zero."); | ||
_fontSizeMultiplier = 1.0; | ||
} | ||
|
||
[self updateFont]; | ||
} | ||
|
||
- (void)setFontStyle:(NSString *)fontStyle | ||
{ | ||
_fontStyle = fontStyle; | ||
[self updateFont]; | ||
} | ||
|
||
- (void)setFontWeight:(NSString *)fontWeight | ||
{ | ||
_fontWeight = fontWeight; | ||
[self updateFont]; | ||
} | ||
|
||
- (void)updateFont | ||
{ | ||
CGFloat scaleMultiplier = self.allowFontScaling ? self.fontSizeMultiplier : 1.0; | ||
self.font = [RCTFont updateFont:nil | ||
withFamily:self.fontFamily | ||
size:self.fontSize | ||
weight:self.fontWeight | ||
style:self.fontStyle | ||
variant:nil | ||
scaleMultiplier:scaleMultiplier]; | ||
|
||
[self.delegate fontAttributesDidChangeWithFont:self.font]; | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/** | ||
* Copyright (c) 2015-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
|
||
@protocol RCTFontAttributesDelegate <NSObject> | ||
|
||
- (void)fontAttributesDidChangeWithFont:(UIFont *)font; | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.