From 732c3a4f4e2ce5f748969d12e0a7f5803e8260ba Mon Sep 17 00:00:00 2001 From: Valentin Shergin Date: Tue, 17 Jul 2018 17:51:17 -0700 Subject: [PATCH] Fabric: Propper support for `accessibilityLabel` in RCTParagraphComponentView Summary: @public This approach is basically copying exising implementation that we have in RCTTextView (D5806097). Changes in `AttributedString` is quite trivial. Reviewed By: mdvacca Differential Revision: D8740000 fbshipit-source-id: 276afdf93d777f7ccb99ca8ee5a18a880de2acbf --- .../Text/RCTParagraphComponentView.mm | 16 ++++++++++++++++ .../fabric/attributedstring/AttributedString.cpp | 8 ++++++++ .../fabric/attributedstring/AttributedString.h | 5 +++++ 3 files changed, 29 insertions(+) diff --git a/React/Fabric/Mounting/ComponentViews/Text/RCTParagraphComponentView.mm b/React/Fabric/Mounting/ComponentViews/Text/RCTParagraphComponentView.mm index ecdc55569cf..d9e55caf1e7 100644 --- a/React/Fabric/Mounting/ComponentViews/Text/RCTParagraphComponentView.mm +++ b/React/Fabric/Mounting/ComponentViews/Text/RCTParagraphComponentView.mm @@ -68,4 +68,20 @@ using namespace facebook::react; frame:frame]; } +#pragma mark - Accessibility + +- (NSString *)accessibilityLabel +{ + NSString *superAccessibilityLabel = [super accessibilityLabel]; + if (superAccessibilityLabel) { + return superAccessibilityLabel; + } + + if (!_paragraphLocalData) { + return nil; + } + + return RCTNSStringFromString(_paragraphLocalData->getAttributedString().getString()); +} + @end diff --git a/ReactCommon/fabric/attributedstring/AttributedString.cpp b/ReactCommon/fabric/attributedstring/AttributedString.cpp index b5eb74af1aa..094b28ed074 100644 --- a/ReactCommon/fabric/attributedstring/AttributedString.cpp +++ b/ReactCommon/fabric/attributedstring/AttributedString.cpp @@ -39,6 +39,14 @@ const std::vector &AttributedString::getFragments() const { return fragments_; } +std::string AttributedString::getString() const { + std::string string; + for (const auto &fragment : fragments_) { + string += fragment.string; + } + return string; +} + #pragma mark - DebugStringConvertible SharedDebugStringConvertibleList AttributedString::getDebugChildren() const { diff --git a/ReactCommon/fabric/attributedstring/AttributedString.h b/ReactCommon/fabric/attributedstring/AttributedString.h index 4fa5ac9b466..560560cebbc 100644 --- a/ReactCommon/fabric/attributedstring/AttributedString.h +++ b/ReactCommon/fabric/attributedstring/AttributedString.h @@ -61,6 +61,11 @@ public: */ const Fragments &getFragments() const; + /* + * Returns a string constructed from all strings in all fragments. + */ + std::string getString() const; + #pragma mark - DebugStringConvertible SharedDebugStringConvertibleList getDebugChildren() const override;