2015-03-23 20:28:42 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*/
|
2015-02-20 04:10:52 +00:00
|
|
|
|
|
|
|
#import "RCTTextManager.h"
|
|
|
|
|
|
|
|
#import "RCTAssert.h"
|
|
|
|
#import "RCTConvert.h"
|
|
|
|
#import "RCTLog.h"
|
|
|
|
#import "RCTShadowRawText.h"
|
|
|
|
#import "RCTShadowText.h"
|
|
|
|
#import "RCTSparseArray.h"
|
|
|
|
#import "RCTText.h"
|
|
|
|
#import "UIView+ReactKit.h"
|
|
|
|
|
|
|
|
@implementation RCTTextManager
|
|
|
|
|
|
|
|
- (UIView *)view
|
|
|
|
{
|
|
|
|
return [[RCTText alloc] init];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (RCTShadowView *)shadowView
|
|
|
|
{
|
|
|
|
return [[RCTShadowText alloc] init];
|
|
|
|
}
|
|
|
|
|
2015-03-17 14:04:39 +00:00
|
|
|
#pragma mark - View properties
|
|
|
|
|
2015-03-26 04:29:28 +00:00
|
|
|
RCT_REMAP_VIEW_PROPERTY(containerBackgroundColor, backgroundColor, UIColor)
|
|
|
|
RCT_CUSTOM_VIEW_PROPERTY(numberOfLines, NSInteger, RCTText)
|
2015-02-20 04:10:52 +00:00
|
|
|
{
|
|
|
|
NSLineBreakMode truncationMode = NSLineBreakByClipping;
|
|
|
|
view.numberOfLines = json ? [RCTConvert NSInteger:json] : defaultView.numberOfLines;
|
|
|
|
if (view.numberOfLines > 0) {
|
|
|
|
truncationMode = NSLineBreakByTruncatingTail;
|
|
|
|
}
|
|
|
|
view.lineBreakMode = truncationMode;
|
|
|
|
}
|
|
|
|
|
2015-03-17 14:04:39 +00:00
|
|
|
#pragma mark - Shadow properties
|
|
|
|
|
2015-03-26 04:29:28 +00:00
|
|
|
RCT_EXPORT_SHADOW_PROPERTY(writingDirection, NSWritingDirection)
|
|
|
|
RCT_EXPORT_SHADOW_PROPERTY(color, UIColor)
|
|
|
|
RCT_EXPORT_SHADOW_PROPERTY(fontFamily, NSString)
|
|
|
|
RCT_EXPORT_SHADOW_PROPERTY(fontSize, CGFloat)
|
|
|
|
RCT_EXPORT_SHADOW_PROPERTY(fontWeight, NSString)
|
|
|
|
RCT_EXPORT_SHADOW_PROPERTY(fontStyle, NSString)
|
|
|
|
RCT_EXPORT_SHADOW_PROPERTY(isHighlighted, BOOL)
|
|
|
|
RCT_EXPORT_SHADOW_PROPERTY(lineHeight, CGFloat)
|
|
|
|
RCT_EXPORT_SHADOW_PROPERTY(maxNumberOfLines, NSInteger)
|
|
|
|
RCT_EXPORT_SHADOW_PROPERTY(shadowOffset, CGSize)
|
|
|
|
RCT_EXPORT_SHADOW_PROPERTY(textAlign, NSTextAlignment)
|
|
|
|
RCT_REMAP_SHADOW_PROPERTY(backgroundColor, textBackgroundColor, UIColor)
|
|
|
|
RCT_CUSTOM_SHADOW_PROPERTY(containerBackgroundColor, UIColor, RCTShadowText)
|
2015-03-01 23:33:55 +00:00
|
|
|
{
|
|
|
|
view.backgroundColor = json ? [RCTConvert UIColor:json] : defaultView.backgroundColor;
|
|
|
|
view.isBGColorExplicitlySet = json ? YES : defaultView.isBGColorExplicitlySet;
|
|
|
|
}
|
2015-03-26 04:29:28 +00:00
|
|
|
RCT_CUSTOM_SHADOW_PROPERTY(numberOfLines, NSInteger, RCTShadowText)
|
2015-02-20 04:10:52 +00:00
|
|
|
{
|
|
|
|
NSLineBreakMode truncationMode = NSLineBreakByClipping;
|
2015-03-01 23:33:55 +00:00
|
|
|
view.maxNumberOfLines = json ? [RCTConvert NSInteger:json] : defaultView.maxNumberOfLines;
|
|
|
|
if (view.maxNumberOfLines > 0) {
|
2015-02-20 04:10:52 +00:00
|
|
|
truncationMode = NSLineBreakByTruncatingTail;
|
|
|
|
}
|
2015-03-01 23:33:55 +00:00
|
|
|
view.truncationMode = truncationMode;
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
2015-03-01 23:33:55 +00:00
|
|
|
- (RCTViewManagerUIBlock)uiBlockToAmendWithShadowView:(RCTShadowText *)shadowView
|
2015-02-20 04:10:52 +00:00
|
|
|
{
|
2015-03-01 23:33:55 +00:00
|
|
|
//TODO: This could be a cleaner replacement for uiBlockToAmendWithShadowViewRegistry
|
|
|
|
return nil;
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: the purpose of this block is effectively just to copy properties from the shadow views
|
|
|
|
// to their equivalent UIViews. In this case, the property being copied is the attributed text,
|
|
|
|
// but the same principle could be used to copy any property. The implementation is really ugly tho
|
|
|
|
// because the RCTViewManager doesn't retain a reference to the views that it manages, so it basically
|
|
|
|
// has to search the entire view hierarchy for relevant views. Not awesome. This seems like something
|
|
|
|
// where we could introduce a generic solution - perhaps a method on RCTShadowView that is called after
|
|
|
|
// layout to copy its properties across?
|
|
|
|
- (RCTViewManagerUIBlock)uiBlockToAmendWithShadowViewRegistry:(RCTSparseArray *)shadowViewRegistry
|
|
|
|
{
|
|
|
|
NSMutableArray *uiBlocks = [NSMutableArray new];
|
|
|
|
|
|
|
|
// TODO: are modules global, or specific to a given rootView?
|
|
|
|
for (RCTShadowView *rootView in shadowViewRegistry.allObjects) {
|
|
|
|
if (![rootView isReactRootView]) {
|
|
|
|
// This isn't a root view
|
|
|
|
continue;
|
|
|
|
}
|
2015-03-23 20:28:42 +00:00
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
if (![rootView isTextDirty]) {
|
|
|
|
// No text processing to be done
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: this is a slightly weird way to do this - a recursive approach would be cleaner
|
|
|
|
RCTSparseArray *reactTaggedAttributedStrings = [[RCTSparseArray alloc] init];
|
|
|
|
NSMutableArray *queue = [NSMutableArray arrayWithObject:rootView];
|
|
|
|
for (NSInteger i = 0; i < [queue count]; i++) {
|
|
|
|
RCTShadowView *shadowView = queue[i];
|
|
|
|
RCTAssert([shadowView isTextDirty], @"Don't process any nodes that don't have dirty text");
|
|
|
|
|
|
|
|
if ([shadowView isKindOfClass:[RCTShadowText class]]) {
|
|
|
|
RCTShadowText *shadowText = (RCTShadowText *)shadowView;
|
|
|
|
reactTaggedAttributedStrings[shadowText.reactTag] = [shadowText attributedString];
|
|
|
|
} else if ([shadowView isKindOfClass:[RCTShadowRawText class]]) {
|
|
|
|
RCTLogError(@"Raw text cannot be used outside of a <Text> tag. Not rendering string: '%@'", [(RCTShadowRawText *)shadowView text]);
|
|
|
|
} else {
|
|
|
|
for (RCTShadowView *child in [shadowView reactSubviews]) {
|
|
|
|
if ([child isTextDirty]) {
|
|
|
|
[queue addObject:child];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[shadowView setTextComputed];
|
|
|
|
}
|
|
|
|
|
|
|
|
[uiBlocks addObject:^(RCTUIManager *uiManager, RCTSparseArray *viewRegistry) {
|
|
|
|
[reactTaggedAttributedStrings enumerateObjectsUsingBlock:^(NSAttributedString *attributedString, NSNumber *reactTag, BOOL *stop) {
|
|
|
|
RCTText *text = viewRegistry[reactTag];
|
|
|
|
text.attributedText = attributedString;
|
|
|
|
}];
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
|
|
|
return ^(RCTUIManager *uiManager, RCTSparseArray *viewRegistry) {
|
|
|
|
for (RCTViewManagerUIBlock shadowBlock in uiBlocks) {
|
|
|
|
shadowBlock(uiManager, viewRegistry);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|