Skip to content

Commit d12385c

Browse files
sherginfacebook-github-bot
authored andcommitted
Fabric: Support for AttributedStringBox::Mode::OpaquePointer in RCTTextLayoutManager
Summary: Now RCTTextLayoutManager (and TextLayoutManager) not only accept `AttributedStringBox` but also is capable to measure such kind of string when it contains a pointer to NSAttributedString. The same can be implemented for Android when/if needed. Changelog: [Internal] Fabric-specific internal change. Reviewed By: sammy-SC Differential Revision: D18670791 fbshipit-source-id: f19089de64d00e1290767310a500ade4cede4685
1 parent c4876d0 commit d12385c

File tree

3 files changed

+47
-21
lines changed

3 files changed

+47
-21
lines changed

ReactCommon/fabric/textlayoutmanager/platform/ios/RCTTextLayoutManager.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ NS_ASSUME_NONNULL_BEGIN
1919
*/
2020
@interface RCTTextLayoutManager : NSObject
2121

22-
- (facebook::react::Size)
23-
measureWithAttributedString:
24-
(facebook::react::AttributedString)attributedString
25-
paragraphAttributes:
26-
(facebook::react::ParagraphAttributes)paragraphAttributes
27-
layoutConstraints:
28-
(facebook::react::LayoutConstraints)layoutConstraints;
22+
- (facebook::react::Size)measureAttributedString:(facebook::react::AttributedString)attributedString
23+
paragraphAttributes:(facebook::react::ParagraphAttributes)paragraphAttributes
24+
layoutConstraints:(facebook::react::LayoutConstraints)layoutConstraints;
25+
26+
- (facebook::react::Size)measureNSAttributedString:(NSAttributedString *)attributedString
27+
paragraphAttributes:(facebook::react::ParagraphAttributes)paragraphAttributes
28+
layoutConstraints:(facebook::react::LayoutConstraints)layoutConstraints;
2929

3030
- (void)drawAttributedString:(facebook::react::AttributedString)attributedString
3131
paragraphAttributes:

ReactCommon/fabric/textlayoutmanager/platform/ios/RCTTextLayoutManager.mm

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,15 @@ static NSLineBreakMode RCTNSLineBreakModeFromEllipsizeMode(EllipsizeMode ellipsi
3232
}
3333
}
3434

35-
- (facebook::react::Size)
36-
measureWithAttributedString:(AttributedString)attributedString
37-
paragraphAttributes:(ParagraphAttributes)paragraphAttributes
38-
layoutConstraints:(LayoutConstraints)layoutConstraints {
35+
- (facebook::react::Size)measureNSAttributedString:(NSAttributedString *)attributedString
36+
paragraphAttributes:(ParagraphAttributes)paragraphAttributes
37+
layoutConstraints:(LayoutConstraints)layoutConstraints
38+
{
3939
CGSize maximumSize = CGSize{layoutConstraints.maximumSize.width,
4040
layoutConstraints.maximumSize.height};
41-
NSTextStorage *textStorage = [self
42-
_textStorageAndLayoutManagerWithAttributesString:[self _nsAttributedStringFromAttributedString:attributedString]
43-
paragraphAttributes:paragraphAttributes
44-
size:maximumSize];
41+
NSTextStorage *textStorage = [self _textStorageAndLayoutManagerWithAttributesString:attributedString
42+
paragraphAttributes:paragraphAttributes
43+
size:maximumSize];
4544

4645
NSLayoutManager *layoutManager = textStorage.layoutManagers.firstObject;
4746
NSTextContainer *textContainer = layoutManager.textContainers.firstObject;
@@ -55,6 +54,15 @@ static NSLineBreakMode RCTNSLineBreakModeFromEllipsizeMode(EllipsizeMode ellipsi
5554
return facebook::react::Size{size.width, size.height};
5655
}
5756

57+
- (facebook::react::Size)measureAttributedString:(AttributedString)attributedString
58+
paragraphAttributes:(ParagraphAttributes)paragraphAttributes
59+
layoutConstraints:(LayoutConstraints)layoutConstraints
60+
{
61+
return [self measureNSAttributedString:[self _nsAttributedStringFromAttributedString:attributedString]
62+
paragraphAttributes:paragraphAttributes
63+
layoutConstraints:layoutConstraints];
64+
}
65+
5866
- (void)drawAttributedString:(AttributedString)attributedString
5967
paragraphAttributes:(ParagraphAttributes)paragraphAttributes
6068
frame:(CGRect)frame {

ReactCommon/fabric/textlayoutmanager/platform/ios/TextLayoutManager.mm

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
#include "TextLayoutManager.h"
99

10+
#include <react/utils/ManagedObjectWrapper.h>
11+
1012
#import "RCTTextLayoutManager.h"
1113

1214
namespace facebook {
@@ -34,15 +36,31 @@
3436
ParagraphAttributes paragraphAttributes,
3537
LayoutConstraints layoutConstraints) const
3638
{
37-
auto &attributedString = attributedStringBox.getValue();
39+
RCTTextLayoutManager *textLayoutManager = (__bridge RCTTextLayoutManager *)self_;
40+
41+
switch (attributedStringBox.getMode()) {
42+
case AttributedStringBox::Mode::Value: {
43+
auto &attributedString = attributedStringBox.getValue();
3844

39-
return measureCache_.get(
40-
MeasureCacheKey{attributedString, paragraphAttributes, layoutConstraints}, [&](MeasureCacheKey const &key) {
41-
RCTTextLayoutManager *textLayoutManager = (__bridge RCTTextLayoutManager *)self_;
42-
return [textLayoutManager measureWithAttributedString:attributedString
45+
return measureCache_.get(
46+
MeasureCacheKey{attributedString, paragraphAttributes, layoutConstraints}, [&](MeasureCacheKey const &key) {
47+
return [textLayoutManager measureAttributedString:attributedString
4348
paragraphAttributes:paragraphAttributes
4449
layoutConstraints:layoutConstraints];
45-
});
50+
});
51+
break;
52+
}
53+
54+
case AttributedStringBox::Mode::OpaquePointer: {
55+
NSAttributedString *nsAttributedString =
56+
(NSAttributedString *)unwrapManagedObject(attributedStringBox.getOpaquePointer());
57+
58+
return [textLayoutManager measureNSAttributedString:nsAttributedString
59+
paragraphAttributes:paragraphAttributes
60+
layoutConstraints:layoutConstraints];
61+
break;
62+
}
63+
}
4664
}
4765

4866
} // namespace react

0 commit comments

Comments
 (0)