Skip to content

Commit 7d77744

Browse files
committed
feat: added line break strategy attribute and example
1 parent 72d45de commit 7d77744

File tree

10 files changed

+126
-379
lines changed

10 files changed

+126
-379
lines changed

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(ios_lineBreakStrategy, 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: 15 additions & 5 deletions
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;
@@ -61,6 +62,7 @@ - (void)applyTextAttributes:(RCTTextAttributes *)textAttributes
6162
_lineHeight = !isnan(textAttributes->_lineHeight) ? textAttributes->_lineHeight : _lineHeight;
6263
_alignment = textAttributes->_alignment != NSTextAlignmentNatural ? textAttributes->_alignment : _alignment; // *
6364
_baseWritingDirection = textAttributes->_baseWritingDirection != NSWritingDirectionNatural ? textAttributes->_baseWritingDirection : _baseWritingDirection; // *
65+
_lineBreakStrategy = textAttributes->_lineBreakStrategy != NSLineBreakStrategyNone ? textAttributes->_lineBreakStrategy : _lineBreakStrategy;
6466

6567
// Decoration
6668
_textDecorationColor = textAttributes->_textDecorationColor ?: _textDecorationColor;
@@ -93,27 +95,34 @@ - (NSParagraphStyle *)effectiveParagraphStyle
9395
alignment = NSTextAlignmentRight;
9496
}
9597
}
96-
98+
9799
paragraphStyle.alignment = alignment;
98100
isParagraphStyleUsed = YES;
99101
}
100-
102+
101103
if (_baseWritingDirection != NSWritingDirectionNatural) {
102104
paragraphStyle.baseWritingDirection = _baseWritingDirection;
103105
isParagraphStyleUsed = YES;
104106
}
105-
107+
108+
if (_lineBreakStrategy != NSLineBreakStrategyNone) {
109+
if (@available(iOS 14.0, *)) {
110+
paragraphStyle.lineBreakStrategy = _lineBreakStrategy;
111+
isParagraphStyleUsed = YES;
112+
}
113+
}
114+
106115
if (!isnan(_lineHeight)) {
107116
CGFloat lineHeight = _lineHeight * self.effectiveFontSizeMultiplier;
108117
paragraphStyle.minimumLineHeight = lineHeight;
109118
paragraphStyle.maximumLineHeight = lineHeight;
110119
isParagraphStyleUsed = YES;
111120
}
112-
121+
113122
if (isParagraphStyleUsed) {
114123
return [paragraphStyle copy];
115124
}
116-
125+
117126
return nil;
118127
}
119128

@@ -294,6 +303,7 @@ - (BOOL)isEqual:(RCTTextAttributes *)textAttributes
294303
RCTTextAttributesCompareFloats(_lineHeight) &&
295304
RCTTextAttributesCompareFloats(_alignment) &&
296305
RCTTextAttributesCompareOthers(_baseWritingDirection) &&
306+
RCTTextAttributesCompareOthers(_lineBreakStrategy) &&
297307
// Decoration
298308
RCTTextAttributesCompareObjects(_textDecorationColor) &&
299309
RCTTextAttributesCompareOthers(_textDecorationStyle) &&

Libraries/Text/TextNativeComponent.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export const NativeText: HostComponent<NativeTextProps> = (createReactNativeComp
4242
onInlineViewLayout: true,
4343
dataDetectorType: true,
4444
android_hyphenationFrequency: true,
45+
ios_lineBreakStrategy: true,
4546
},
4647
directEventTypes: {
4748
topTextLayout: {

Libraries/Text/TextProps.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,4 +199,11 @@ export type TextProps = $ReadOnly<{|
199199
* See https://reactnative.dev/docs/text.html#supperhighlighting
200200
*/
201201
suppressHighlighting?: ?boolean,
202+
203+
/**
204+
* Set line break strategy on iOS.
205+
*
206+
* See https://reactnative.dev/docs/text.html#ios_linebreakstrategy
207+
*/
208+
ios_lineBreakStrategy?: ?('none' | 'standard' | 'hangul-word' | 'push-out'),
202209
|}>;

React/Base/RCTConvert.h

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

React/Base/RCTConvert.m

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,38 @@ + (NSLocale *)NSLocale:(id)json
366366
NSWritingDirectionNatural,
367367
integerValue)
368368

369+
+ (NSLineBreakStrategy)NSLineBreakStrategy:(id)json RCT_DYNAMIC
370+
{
371+
static NSDictionary *mapping;
372+
static dispatch_once_t onceToken;
373+
dispatch_once(&onceToken, ^{
374+
if (@available(iOS 14.0, *)) {
375+
mapping = @{
376+
@"none" : @(NSLineBreakStrategyNone),
377+
#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 140000
378+
@"standard" : @(NSLineBreakStrategyStandard),
379+
@"hangul-word" : @(NSLineBreakStrategyHangulWordPriority),
380+
@"push-out": @(NSLineBreakStrategyPushOut)
381+
#else
382+
@"standard" : @(NSLineBreakStrategyNone),
383+
@"hangul-word" : @(NSLineBreakStrategyNone),
384+
@"push-out": @(NSLineBreakStrategyNone)
385+
#endif
386+
};
387+
} else {
388+
mapping = @{
389+
@"none" : @(NSLineBreakStrategyNone),
390+
@"standard" : @(NSLineBreakStrategyNone),
391+
@"hangul-word" : @(NSLineBreakStrategyNone),
392+
@"push-out": @(NSLineBreakStrategyNone)
393+
};
394+
}
395+
});
396+
397+
NSLineBreakStrategy type = RCTConvertEnumValue("NSLineBreakStrategy", mapping, @(NSLineBreakStrategyNone), json).integerValue;
398+
return type;
399+
}
400+
369401
RCT_ENUM_CONVERTER(
370402
UITextAutocapitalizationType,
371403
(@{

ReactCommon/react/renderer/attributedstring/conversions.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -746,6 +746,10 @@ inline folly::dynamic toDynamic(const TextAttributes &textAttributes) {
746746
_textAttributes(
747747
"baseWritingDirection", toString(*textAttributes.baseWritingDirection));
748748
}
749+
if (textAttributes.lineBreakStrategy.has_value()) {
750+
_textAttributes(
751+
"lineBreakStrategy", toString(*textAttributes.lineBreakStrategy));
752+
}
749753
// Decoration
750754
if (textAttributes.textDecorationColor) {
751755
_textAttributes(

0 commit comments

Comments
 (0)