Skip to content
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

feat(iOS): line break mode cpp changes and new functions #46130

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/react-native/Libraries/Text/RCTTextAttributes.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ extern NSString *const RCTTextAttributesTagAttributeName;
@property (nonatomic, assign) NSTextAlignment alignment;
@property (nonatomic, assign) NSWritingDirection baseWritingDirection;
@property (nonatomic, assign) NSLineBreakStrategy lineBreakStrategy;
@property (nonatomic, assign) NSLineBreakMode lineBreakMode;
// Decoration
@property (nonatomic, strong, nullable) UIColor *textDecorationColor;
@property (nonatomic, assign) NSUnderlineStyle textDecorationStyle;
Expand Down
8 changes: 8 additions & 0 deletions packages/react-native/Libraries/Text/RCTTextAttributes.mm
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ - (instancetype)init
_alignment = NSTextAlignmentNatural;
_baseWritingDirection = NSWritingDirectionNatural;
_lineBreakStrategy = NSLineBreakStrategyNone;
_lineBreakMode = NSLineBreakByWordWrapping;
_textShadowRadius = NAN;
_opacity = NAN;
_textTransform = RCTTextTransformUndefined;
Expand Down Expand Up @@ -70,6 +71,7 @@ - (void)applyTextAttributes:(RCTTextAttributes *)textAttributes
? textAttributes->_baseWritingDirection
: _baseWritingDirection; // *
_lineBreakStrategy = textAttributes->_lineBreakStrategy ?: _lineBreakStrategy;
_lineBreakMode = textAttributes->_lineBreakMode ?: _lineBreakMode;

// Decoration
_textDecorationColor = textAttributes->_textDecorationColor ?: _textDecorationColor;
Expand Down Expand Up @@ -127,6 +129,11 @@ - (NSParagraphStyle *)effectiveParagraphStyle
isParagraphStyleUsed = YES;
}
}

if (_lineBreakMode != NSLineBreakByWordWrapping) {
paragraphStyle.lineBreakMode = _lineBreakMode;
isParagraphStyleUsed = YES;
}

if (!isnan(_lineHeight)) {
CGFloat lineHeight = _lineHeight * self.effectiveFontSizeMultiplier;
Expand Down Expand Up @@ -336,6 +343,7 @@ - (BOOL)isEqual:(RCTTextAttributes *)textAttributes
// Paragraph Styles
RCTTextAttributesCompareFloats(_lineHeight) && RCTTextAttributesCompareFloats(_alignment) &&
RCTTextAttributesCompareOthers(_baseWritingDirection) && RCTTextAttributesCompareOthers(_lineBreakStrategy) &&
RCTTextAttributesCompareOthers(_lineBreakMode) &&
// Decoration
RCTTextAttributesCompareObjects(_textDecorationColor) && RCTTextAttributesCompareOthers(_textDecorationStyle) &&
RCTTextAttributesCompareOthers(_textDecorationLine) &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class TextAttributes : public DebugStringConvertible {
std::optional<TextAlignment> alignment{};
std::optional<WritingDirection> baseWritingDirection{};
std::optional<LineBreakStrategy> lineBreakStrategy{};
std::optional<LineBreakMode> lineBreakMode{};

// Decoration
SharedColor textDecorationColor{};
Expand Down Expand Up @@ -128,6 +129,7 @@ struct hash<facebook::react::TextAttributes> {
textAttributes.textAlignVertical,
textAttributes.baseWritingDirection,
textAttributes.lineBreakStrategy,
textAttributes.lineBreakMode,
textAttributes.textDecorationColor,
textAttributes.textDecorationLineType,
textAttributes.textDecorationStyle,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,15 @@ enum class LineBreakStrategy {
// system uses for standard UI labels.
};

enum class LineBreakMode {
Word, // Wrap at word boundaries, default
Char, // Wrap at character boundaries
Clip, // Simply clip
Head, // Truncate at head of line: "...wxyz"
Middle, // Truncate middle of line: "ab...yz"
Tail // Truncate at tail of line: "abcd..."
};

enum class TextDecorationLineType {
None,
Underline,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,11 @@ inline static CGFloat RCTEffectiveFontSizeMultiplierFromTextAttributes(const Tex
RCTNSLineBreakStrategyFromLineBreakStrategy(textAttributes.lineBreakStrategy.value());
isParagraphStyleUsed = YES;
}

if (textAttributes.lineBreakMode.has_value()) {
paragraphStyle.lineBreakMode = RCTNSLineBreakModeFromLineBreakMode(textAttributes.lineBreakMode.value());
isParagraphStyleUsed = YES;
}

if (!isnan(textAttributes.lineHeight)) {
CGFloat lineHeight = textAttributes.lineHeight * RCTEffectiveFontSizeMultiplierFromTextAttributes(textAttributes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,25 @@ inline static NSLineBreakStrategy RCTNSLineBreakStrategyFromLineBreakStrategy(
}
}

inline static NSLineBreakMode RCTNSLineBreakModeFromLineBreakMode(
facebook::react::LineBreakMode lineBreakMode)
{
switch (lineBreakMode) {
case facebook::react::LineBreakMode::Word:
return NSLineBreakByWordWrapping;
case facebook::react::LineBreakMode::Char:
return NSLineBreakByCharWrapping;
case facebook::react::LineBreakMode::Clip:
return NSLineBreakByClipping;
case facebook::react::LineBreakMode::Head:
return NSLineBreakByTruncatingHead;
case facebook::react::LineBreakMode::Middle:
return NSLineBreakByTruncatingMiddle;
case facebook::react::LineBreakMode::Tail:
return NSLineBreakByTruncatingTail;
}
}

inline static RCTFontStyle RCTFontStyleFromFontStyle(facebook::react::FontStyle fontStyle)
{
switch (fontStyle) {
Expand Down
Loading