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
This commit is contained in:
Valentin Shergin
2019-12-04 18:36:49 -08:00
committed by Facebook Github Bot
parent c4876d0313
commit d12385cae6
3 changed files with 47 additions and 21 deletions
@@ -19,13 +19,13 @@ NS_ASSUME_NONNULL_BEGIN
*/
@interface RCTTextLayoutManager : NSObject
- (facebook::react::Size)
measureWithAttributedString:
(facebook::react::AttributedString)attributedString
paragraphAttributes:
(facebook::react::ParagraphAttributes)paragraphAttributes
layoutConstraints:
(facebook::react::LayoutConstraints)layoutConstraints;
- (facebook::react::Size)measureAttributedString:(facebook::react::AttributedString)attributedString
paragraphAttributes:(facebook::react::ParagraphAttributes)paragraphAttributes
layoutConstraints:(facebook::react::LayoutConstraints)layoutConstraints;
- (facebook::react::Size)measureNSAttributedString:(NSAttributedString *)attributedString
paragraphAttributes:(facebook::react::ParagraphAttributes)paragraphAttributes
layoutConstraints:(facebook::react::LayoutConstraints)layoutConstraints;
- (void)drawAttributedString:(facebook::react::AttributedString)attributedString
paragraphAttributes:
@@ -32,16 +32,15 @@ static NSLineBreakMode RCTNSLineBreakModeFromEllipsizeMode(EllipsizeMode ellipsi
}
}
- (facebook::react::Size)
measureWithAttributedString:(AttributedString)attributedString
paragraphAttributes:(ParagraphAttributes)paragraphAttributes
layoutConstraints:(LayoutConstraints)layoutConstraints {
- (facebook::react::Size)measureNSAttributedString:(NSAttributedString *)attributedString
paragraphAttributes:(ParagraphAttributes)paragraphAttributes
layoutConstraints:(LayoutConstraints)layoutConstraints
{
CGSize maximumSize = CGSize{layoutConstraints.maximumSize.width,
layoutConstraints.maximumSize.height};
NSTextStorage *textStorage = [self
_textStorageAndLayoutManagerWithAttributesString:[self _nsAttributedStringFromAttributedString:attributedString]
paragraphAttributes:paragraphAttributes
size:maximumSize];
NSTextStorage *textStorage = [self _textStorageAndLayoutManagerWithAttributesString:attributedString
paragraphAttributes:paragraphAttributes
size:maximumSize];
NSLayoutManager *layoutManager = textStorage.layoutManagers.firstObject;
NSTextContainer *textContainer = layoutManager.textContainers.firstObject;
@@ -55,6 +54,15 @@ static NSLineBreakMode RCTNSLineBreakModeFromEllipsizeMode(EllipsizeMode ellipsi
return facebook::react::Size{size.width, size.height};
}
- (facebook::react::Size)measureAttributedString:(AttributedString)attributedString
paragraphAttributes:(ParagraphAttributes)paragraphAttributes
layoutConstraints:(LayoutConstraints)layoutConstraints
{
return [self measureNSAttributedString:[self _nsAttributedStringFromAttributedString:attributedString]
paragraphAttributes:paragraphAttributes
layoutConstraints:layoutConstraints];
}
- (void)drawAttributedString:(AttributedString)attributedString
paragraphAttributes:(ParagraphAttributes)paragraphAttributes
frame:(CGRect)frame {
@@ -7,6 +7,8 @@
#include "TextLayoutManager.h"
#include <react/utils/ManagedObjectWrapper.h>
#import "RCTTextLayoutManager.h"
namespace facebook {
@@ -34,15 +36,31 @@ Size TextLayoutManager::measure(
ParagraphAttributes paragraphAttributes,
LayoutConstraints layoutConstraints) const
{
auto &attributedString = attributedStringBox.getValue();
RCTTextLayoutManager *textLayoutManager = (__bridge RCTTextLayoutManager *)self_;
return measureCache_.get(
MeasureCacheKey{attributedString, paragraphAttributes, layoutConstraints}, [&](MeasureCacheKey const &key) {
RCTTextLayoutManager *textLayoutManager = (__bridge RCTTextLayoutManager *)self_;
return [textLayoutManager measureWithAttributedString:attributedString
switch (attributedStringBox.getMode()) {
case AttributedStringBox::Mode::Value: {
auto &attributedString = attributedStringBox.getValue();
return measureCache_.get(
MeasureCacheKey{attributedString, paragraphAttributes, layoutConstraints}, [&](MeasureCacheKey const &key) {
return [textLayoutManager measureAttributedString:attributedString
paragraphAttributes:paragraphAttributes
layoutConstraints:layoutConstraints];
});
});
break;
}
case AttributedStringBox::Mode::OpaquePointer: {
NSAttributedString *nsAttributedString =
(NSAttributedString *)unwrapManagedObject(attributedStringBox.getOpaquePointer());
return [textLayoutManager measureNSAttributedString:nsAttributedString
paragraphAttributes:paragraphAttributes
layoutConstraints:layoutConstraints];
break;
}
}
}
} // namespace react