Skip to content

Commit

Permalink
feat: linebreak mode cpp code changes added
Browse files Browse the repository at this point in the history
  • Loading branch information
shubhamguptadream11 committed Aug 21, 2024
1 parent 387560a commit a4445ab
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 0 deletions.
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

0 comments on commit a4445ab

Please sign in to comment.