-
Notifications
You must be signed in to change notification settings - Fork 24.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
iOS: Support allowFontScaling on TextInput #14030
Changes from all commits
4268fba
6f5b337
82d0033
a2520cd
c5fd415
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
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 |
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
|
||
#import <React/RCTConvert.h> | ||
#import <React/RCTEventDispatcher.h> | ||
#import <React/RCTFont.h> | ||
#import <React/RCTUIManager.h> | ||
#import <React/RCTUtils.h> | ||
#import <React/UIView+React.h> | ||
|
@@ -57,6 +58,8 @@ - (instancetype)initWithBridge:(RCTBridge *)bridge | |
|
||
_backedTextInput.textInputDelegate = self; | ||
|
||
self.font = self.fontAttributes.font; | ||
|
||
[self addSubview:_backedTextInput]; | ||
} | ||
return self; | ||
|
@@ -70,6 +73,18 @@ - (instancetype)initWithBridge:(RCTBridge *)bridge | |
return _backedTextInput; | ||
} | ||
|
||
- (void)fontAttributesDidChangeWithFont:(UIFont *)font | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (We can consider moving this to superclass.) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This overrides the superclass's implementation of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (This does not block landing. We can do it in the future.) |
||
{ | ||
[super fontAttributesDidChangeWithFont:font]; | ||
|
||
// Because the font changed, the TextInput may take up more space now so we call | ||
// invalidateContentSize. However, if there's currently no text to render, then | ||
// there's no need to call invalidateContentSize. | ||
if ([self text].length != 0) { | ||
[self invalidateContentSize]; | ||
} | ||
} | ||
|
||
#pragma mark - RCTComponent | ||
|
||
- (void)insertReactSubview:(UIView *)subview atIndex:(NSInteger)index | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It ensures that
_backedTextInput's
font gets initialized toRCTFontAttributes's
default font. This is not necessarily the same as_backedTextInput's
default font.