Fabric: Conversion function between NSAttributedString and AttributedStringBox

Summary:
It's nice to have those conversions between NSAttributedString and AttributedTextBox in some utils module because:
An empty string must be stored as an empty C++ string, not like an empty NSAttributedString. That allows deferring this property from the object without accessing Objective-C runtime;
It's nice to hide some tedious Objective-C object wrapping/unwrapping boilerplate.

Changelog: [Internal] Fabric-specific internal change.

Reviewed By: sammy-SC

Differential Revision: D18950430

fbshipit-source-id: 842c202f243da17c47bc5cca9df6722cdcec07c5
This commit is contained in:
Valentin Shergin
2019-12-12 12:46:33 -08:00
committed by Facebook Github Bot
parent 909b0fe0e7
commit 5755129c19
3 changed files with 25 additions and 1 deletions
@@ -125,6 +125,7 @@ rn_xplat_cxx_library(
YOGA_CXX_TARGET,
react_native_xplat_target("fabric/attributedstring:attributedstring"),
react_native_xplat_target("fabric/core:core"),
react_native_xplat_target("utils:utils"),
react_native_xplat_target("fabric/debug:debug"),
react_native_xplat_target("fabric/graphics:graphics"),
react_native_xplat_target("fabric/uimanager:uimanager"),
@@ -8,6 +8,7 @@
#import <UIKit/UIKit.h>
#include <react/attributedstring/AttributedString.h>
#include <react/attributedstring/AttributedStringBox.h>
#include <react/attributedstring/TextAttributes.h>
NS_ASSUME_NONNULL_BEGIN
@@ -25,7 +26,13 @@ NSDictionary<NSAttributedStringKey, id> *RCTNSTextAttributesFromTextAttributes(
* Conversions amond `NSAttributedString`, `AttributedString` and `AttributedStringBox`.
*/
NSAttributedString *RCTNSAttributedStringFromAttributedString(
const facebook::react::AttributedString &attributedString);
facebook::react::AttributedString const &attributedString);
NSAttributedString *RCTNSAttributedStringFromAttributedStringBox(
facebook::react::AttributedStringBox const &attributedStringBox);
facebook::react::AttributedStringBox RCTAttributedStringBoxFromNSAttributedString(
NSAttributedString *nsAttributedString);
@interface RCTWeakEventEmitterWrapper : NSObject
@property (nonatomic, assign) facebook::react::SharedEventEmitter eventEmitter;
@@ -11,6 +11,7 @@
#include <react/textlayoutmanager/RCTFontProperties.h>
#include <react/textlayoutmanager/RCTFontUtils.h>
#include <react/textlayoutmanager/RCTTextPrimitivesConversions.h>
#include <react/utils/ManagedObjectWrapper.h>
using namespace facebook::react;
@@ -268,3 +269,18 @@ NSAttributedString *RCTNSAttributedStringFromAttributedString(const AttributedSt
return nsAttributedString;
}
NSAttributedString *RCTNSAttributedStringFromAttributedStringBox(AttributedStringBox const &attributedStringBox)
{
switch (attributedStringBox.getMode()) {
case AttributedStringBox::Mode::Value:
return RCTNSAttributedStringFromAttributedString(attributedStringBox.getValue());
case AttributedStringBox::Mode::OpaquePointer:
return (NSAttributedString *)unwrapManagedObject(attributedStringBox.getOpaquePointer());
}
}
AttributedStringBox RCTAttributedStringBoxFromNSAttributedString(NSAttributedString *nsAttributedString)
{
return nsAttributedString.length ? AttributedStringBox{wrapManagedObject(nsAttributedString)} : AttributedStringBox{};
}