Skip to content

Commit 0481948

Browse files
bang9facebook-github-bot
authored andcommitted
feat(iOS): added lineBreakStrategy attribute to Text/TextInput (#31272)
Summary: iOS did not support the implementation of Korean word-wrap(line-break) before iOS14. If the attribute applied, the word-wrap of Korean will works. ## Changelog <!-- Help reviewers and the release process by writing your own changelog entry. For an example, see: https://github.com/facebook/react-native/wiki/Changelog --> [iOS] [Added] - Line break strategy for Text and TextInput components Pull Request resolved: #31272 Test Plan: 1. Test build and run on above iOS 14. 2. Test it does not affect existing text components when set default(none) strategy. 3. Test whether word-wrap works with Korean when set hangul-word strategy. <img src="https://user-images.githubusercontent.com/26326015/112963967-d7f70c00-9182-11eb-9a34-8c758b80c219.png" width="300" height="" style="max-width:100%;"> Reviewed By: javache Differential Revision: D39824809 Pulled By: lunaleaps fbshipit-source-id: 42cb0385221a38c84e80d3494d1bfc1934ecf32b
1 parent a0ee6fa commit 0481948

File tree

19 files changed

+242
-1
lines changed

19 files changed

+242
-1
lines changed

Libraries/Components/TextInput/RCTTextInputViewConfig.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ const RCTTextInputViewConfig = {
149149
clearTextOnFocus: true,
150150
showSoftInputOnFocus: true,
151151
autoFocus: true,
152+
lineBreakStrategyIOS: true,
152153
...ConditionallyIgnoredEventHandlers({
153154
onChange: true,
154155
onSelectionChange: true,

Libraries/Components/TextInput/TextInput.flow.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,12 @@ type IOSProps = $ReadOnly<{|
319319
* @platform ios
320320
*/
321321
textContentType?: ?TextContentType,
322+
323+
/**
324+
* Set line break strategy on iOS.
325+
* @platform ios
326+
*/
327+
lineBreakStrategyIOS?: ?('none' | 'standard' | 'hangul-word' | 'push-out'),
322328
|}>;
323329

324330
type AndroidProps = $ReadOnly<{|

Libraries/Components/TextInput/TextInput.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,12 @@ type IOSProps = $ReadOnly<{|
352352
* @platform ios
353353
*/
354354
textContentType?: ?TextContentType,
355+
356+
/**
357+
* Set line break strategy on iOS.
358+
* @platform ios
359+
*/
360+
lineBreakStrategyIOS?: ?('none' | 'standard' | 'hangul-word' | 'push-out'),
355361
|}>;
356362

357363
type AndroidProps = $ReadOnly<{|

Libraries/Text/BaseText/RCTBaseTextViewManager.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ - (RCTShadowView *)shadowView
4242
RCT_REMAP_SHADOW_PROPERTY(lineHeight, textAttributes.lineHeight, CGFloat)
4343
RCT_REMAP_SHADOW_PROPERTY(textAlign, textAttributes.alignment, NSTextAlignment)
4444
RCT_REMAP_SHADOW_PROPERTY(writingDirection, textAttributes.baseWritingDirection, NSWritingDirection)
45+
RCT_REMAP_SHADOW_PROPERTY(lineBreakStrategyIOS, textAttributes.lineBreakStrategy, NSLineBreakStrategy)
4546
// Decoration
4647
RCT_REMAP_SHADOW_PROPERTY(textDecorationColor, textAttributes.textDecorationColor, UIColor)
4748
RCT_REMAP_SHADOW_PROPERTY(textDecorationStyle, textAttributes.textDecorationStyle, NSUnderlineStyle)

Libraries/Text/RCTTextAttributes.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ extern NSString *const RCTTextAttributesTagAttributeName;
4141
@property (nonatomic, assign) CGFloat lineHeight;
4242
@property (nonatomic, assign) NSTextAlignment alignment;
4343
@property (nonatomic, assign) NSWritingDirection baseWritingDirection;
44+
@property (nonatomic, assign) NSLineBreakStrategy lineBreakStrategy;
4445
// Decoration
4546
@property (nonatomic, strong, nullable) UIColor *textDecorationColor;
4647
@property (nonatomic, assign) NSUnderlineStyle textDecorationStyle;

Libraries/Text/RCTTextAttributes.m

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ - (instancetype)init
2727
_maxFontSizeMultiplier = NAN;
2828
_alignment = NSTextAlignmentNatural;
2929
_baseWritingDirection = NSWritingDirectionNatural;
30+
_lineBreakStrategy = NSLineBreakStrategyNone;
3031
_textShadowRadius = NAN;
3132
_opacity = NAN;
3233
_textTransform = RCTTextTransformUndefined;
@@ -66,6 +67,7 @@ - (void)applyTextAttributes:(RCTTextAttributes *)textAttributes
6667
_baseWritingDirection = textAttributes->_baseWritingDirection != NSWritingDirectionNatural
6768
? textAttributes->_baseWritingDirection
6869
: _baseWritingDirection; // *
70+
_lineBreakStrategy = textAttributes->_lineBreakStrategy ?: _lineBreakStrategy;
6971

7072
// Decoration
7173
_textDecorationColor = textAttributes->_textDecorationColor ?: _textDecorationColor;
@@ -117,6 +119,13 @@ - (NSParagraphStyle *)effectiveParagraphStyle
117119
isParagraphStyleUsed = YES;
118120
}
119121

122+
if (_lineBreakStrategy != NSLineBreakStrategyNone) {
123+
if (@available(iOS 14.0, *)) {
124+
paragraphStyle.lineBreakStrategy = _lineBreakStrategy;
125+
isParagraphStyleUsed = YES;
126+
}
127+
}
128+
120129
if (!isnan(_lineHeight)) {
121130
CGFloat lineHeight = _lineHeight * self.effectiveFontSizeMultiplier;
122131
paragraphStyle.minimumLineHeight = lineHeight;
@@ -318,7 +327,7 @@ - (BOOL)isEqual:(RCTTextAttributes *)textAttributes
318327
RCTTextAttributesCompareFloats(_letterSpacing) &&
319328
// Paragraph Styles
320329
RCTTextAttributesCompareFloats(_lineHeight) && RCTTextAttributesCompareFloats(_alignment) &&
321-
RCTTextAttributesCompareOthers(_baseWritingDirection) &&
330+
RCTTextAttributesCompareOthers(_baseWritingDirection) && RCTTextAttributesCompareOthers(_lineBreakStrategy) &&
322331
// Decoration
323332
RCTTextAttributesCompareObjects(_textDecorationColor) && RCTTextAttributesCompareOthers(_textDecorationStyle) &&
324333
RCTTextAttributesCompareOthers(_textDecorationLine) &&

Libraries/Text/TextNativeComponent.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ const textViewConfig = {
4545
onInlineViewLayout: true,
4646
dataDetectorType: true,
4747
android_hyphenationFrequency: true,
48+
lineBreakStrategyIOS: true,
4849
},
4950
directEventTypes: {
5051
topTextLayout: {

Libraries/Text/TextProps.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,4 +236,11 @@ export type TextProps = $ReadOnly<{|
236236
* See https://reactnative.dev/docs/text#supperhighlighting
237237
*/
238238
suppressHighlighting?: ?boolean,
239+
240+
/**
241+
* Set line break strategy on iOS.
242+
*
243+
* See https://reactnative.dev/docs/text.html#linebreakstrategyios
244+
*/
245+
lineBreakStrategyIOS?: ?('none' | 'standard' | 'hangul-word' | 'push-out'),
239246
|}>;

React/Base/RCTConvert.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ typedef NSURL RCTFileURL;
6565
+ (NSTextAlignment)NSTextAlignment:(id)json;
6666
+ (NSUnderlineStyle)NSUnderlineStyle:(id)json;
6767
+ (NSWritingDirection)NSWritingDirection:(id)json;
68+
+ (NSLineBreakStrategy)NSLineBreakStrategy:(id)json;
6869
+ (UITextAutocapitalizationType)UITextAutocapitalizationType:(id)json;
6970
+ (UITextFieldViewMode)UITextFieldViewMode:(id)json;
7071
+ (UIKeyboardType)UIKeyboardType:(id)json;

React/Base/RCTConvert.m

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,25 @@ + (NSLocale *)NSLocale:(id)json
375375
NSWritingDirectionNatural,
376376
integerValue)
377377

378+
+ (NSLineBreakStrategy)NSLineBreakStrategy:(id)json RCT_DYNAMIC
379+
{
380+
if (@available(iOS 14.0, *)) {
381+
static NSDictionary *mapping;
382+
static dispatch_once_t onceToken;
383+
dispatch_once(&onceToken, ^{
384+
mapping = @{
385+
@"none" : @(NSLineBreakStrategyNone),
386+
@"standard" : @(NSLineBreakStrategyStandard),
387+
@"hangul-word" : @(NSLineBreakStrategyHangulWordPriority),
388+
@"push-out" : @(NSLineBreakStrategyPushOut)
389+
};
390+
});
391+
return RCTConvertEnumValue("NSLineBreakStrategy", mapping, @(NSLineBreakStrategyNone), json).integerValue;
392+
} else {
393+
return NSLineBreakStrategyNone;
394+
}
395+
}
396+
378397
RCT_ENUM_CONVERTER(
379398
UITextAutocapitalizationType,
380399
(@{

0 commit comments

Comments
 (0)