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 "RCTText.h"
|
|
|
|
|
|
|
|
#import "RCTShadowText.h"
|
|
|
|
#import "RCTUtils.h"
|
2015-03-26 09:58:06 +00:00
|
|
|
#import "UIView+React.h"
|
2015-02-20 04:10:52 +00:00
|
|
|
|
|
|
|
@implementation RCTText
|
|
|
|
{
|
|
|
|
NSTextStorage *_textStorage;
|
2015-11-03 22:45:46 +00:00
|
|
|
NSMutableArray<UIView *> *_reactSubviews;
|
2015-07-24 15:25:51 +00:00
|
|
|
CAShapeLayer *_highlightLayer;
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (instancetype)initWithFrame:(CGRect)frame
|
|
|
|
{
|
|
|
|
if ((self = [super initWithFrame:frame])) {
|
2015-08-17 14:35:34 +00:00
|
|
|
_textStorage = [NSTextStorage new];
|
2015-05-29 13:04:05 +00:00
|
|
|
_reactSubviews = [NSMutableArray array];
|
2015-02-20 04:10:52 +00:00
|
|
|
|
2015-05-14 20:45:00 +00:00
|
|
|
self.isAccessibilityElement = YES;
|
2015-05-14 16:38:45 +00:00
|
|
|
self.accessibilityTraits |= UIAccessibilityTraitStaticText;
|
|
|
|
|
2015-04-22 02:13:40 +00:00
|
|
|
self.opaque = NO;
|
2015-02-20 04:10:52 +00:00
|
|
|
self.contentMode = UIViewContentModeRedraw;
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2015-06-19 15:51:05 +00:00
|
|
|
- (NSString *)description
|
|
|
|
{
|
|
|
|
NSString *superDescription = super.description;
|
|
|
|
NSRange semicolonRange = [superDescription rangeOfString:@";"];
|
|
|
|
NSString *replacement = [NSString stringWithFormat:@"; reactTag: %@; text: %@", self.reactTag, self.textStorage.string];
|
|
|
|
return [superDescription stringByReplacingCharactersInRange:semicolonRange withString:replacement];
|
|
|
|
}
|
|
|
|
|
2015-05-28 16:29:27 +00:00
|
|
|
- (void)reactSetFrame:(CGRect)frame
|
|
|
|
{
|
|
|
|
// Text looks super weird if its frame is animated.
|
|
|
|
// This disables the frame animation, without affecting opacity, etc.
|
|
|
|
[UIView performWithoutAnimation:^{
|
|
|
|
[super reactSetFrame:frame];
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
Disable background color propagation for everything except text nodes
Summary:
public
Blending semitransparent pixels against their background is fairly a fairly expensive operation on mobile GPUs. To reduce blending, React Native has a system called "background color propagation", where the background color of parent views is automatically inherited by child views unless explicitly overridden. This means that translucent pixels can be blended directly against a known background color, avoiding the need to do this dynamically on the GPU.
In practice, this is only useful for views that do their own drawing, which is basically just `<Image/>` and `<Text/>` components, and for image components it only really matters when the image has an alpha component.
The automatic background propagation is a bit of a hack, and often does the wrong thing - for example if a view overflows its bounds, or if it overlaps a sibling, the background color will often be incorrect and need to be manually disabled. Because the only place that it provides a significant performance benefit is for text, this diff disables the behavior for everything except `<Text/>` nodes. It might still be useful for `<Image/>` nodes too, but looking through the examples in UIExplorer, the number of places where it does the wrong thing for images outnumbers the cases where it provides significant reduction in blending.
Note that this diff does not prevent you from eliminating blending on image components by manually setting an opaque background color, nor does it stop you from disabling color propagation on text components by manually setting a transparent background.
Reviewed By: javache
Differential Revision: D2811031
fb-gh-sync-id: 2eb08918c9031c582a3dd2d40e04b27a663dac82
2016-01-08 11:37:25 +00:00
|
|
|
- (void)reactSetInheritedBackgroundColor:(UIColor *)inheritedBackgroundColor
|
|
|
|
{
|
|
|
|
self.backgroundColor = inheritedBackgroundColor;
|
|
|
|
}
|
|
|
|
|
2015-05-29 13:04:05 +00:00
|
|
|
- (void)insertReactSubview:(UIView *)subview atIndex:(NSInteger)atIndex
|
|
|
|
{
|
|
|
|
[_reactSubviews insertObject:subview atIndex:atIndex];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)removeReactSubview:(UIView *)subview
|
|
|
|
{
|
|
|
|
[_reactSubviews removeObject:subview];
|
|
|
|
}
|
|
|
|
|
2015-11-03 22:45:46 +00:00
|
|
|
- (NSArray<UIView *> *)reactSubviews
|
2015-05-29 13:04:05 +00:00
|
|
|
{
|
|
|
|
return _reactSubviews;
|
|
|
|
}
|
|
|
|
|
2015-05-27 01:39:37 +00:00
|
|
|
- (void)setTextStorage:(NSTextStorage *)textStorage
|
2015-02-20 04:10:52 +00:00
|
|
|
{
|
2016-03-10 15:45:17 +00:00
|
|
|
if (_textStorage != textStorage) {
|
|
|
|
_textStorage = textStorage;
|
|
|
|
[self setNeedsDisplay];
|
|
|
|
}
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
2015-04-23 23:00:34 +00:00
|
|
|
- (void)drawRect:(CGRect)rect
|
2015-02-20 04:10:52 +00:00
|
|
|
{
|
2015-08-24 10:14:33 +00:00
|
|
|
NSLayoutManager *layoutManager = _textStorage.layoutManagers.firstObject;
|
|
|
|
NSTextContainer *textContainer = layoutManager.textContainers.firstObject;
|
2015-05-27 01:39:37 +00:00
|
|
|
CGRect textFrame = UIEdgeInsetsInsetRect(self.bounds, _contentInset);
|
|
|
|
NSRange glyphRange = [layoutManager glyphRangeForTextContainer:textContainer];
|
2015-07-24 15:25:51 +00:00
|
|
|
|
2015-05-27 01:39:37 +00:00
|
|
|
[layoutManager drawBackgroundForGlyphRange:glyphRange atPoint:textFrame.origin];
|
|
|
|
[layoutManager drawGlyphsForGlyphRange:glyphRange atPoint:textFrame.origin];
|
2015-07-24 15:25:51 +00:00
|
|
|
|
|
|
|
__block UIBezierPath *highlightPath = nil;
|
2015-08-03 12:55:48 +00:00
|
|
|
NSRange characterRange = [layoutManager characterRangeForGlyphRange:glyphRange actualGlyphRange:NULL];
|
|
|
|
[layoutManager.textStorage enumerateAttribute:RCTIsHighlightedAttributeName inRange:characterRange options:0 usingBlock:^(NSNumber *value, NSRange range, BOOL *_) {
|
|
|
|
if (!value.boolValue) {
|
|
|
|
return;
|
2015-07-24 15:25:51 +00:00
|
|
|
}
|
2015-08-03 12:55:48 +00:00
|
|
|
|
|
|
|
[layoutManager enumerateEnclosingRectsForGlyphRange:range withinSelectedGlyphRange:range inTextContainer:textContainer usingBlock:^(CGRect enclosingRect, __unused BOOL *__) {
|
|
|
|
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectInset(enclosingRect, -2, -2) cornerRadius:2];
|
|
|
|
if (highlightPath) {
|
|
|
|
[highlightPath appendPath:path];
|
|
|
|
} else {
|
|
|
|
highlightPath = path;
|
|
|
|
}
|
|
|
|
}];
|
2015-07-24 15:25:51 +00:00
|
|
|
}];
|
|
|
|
|
|
|
|
if (highlightPath) {
|
|
|
|
if (!_highlightLayer) {
|
|
|
|
_highlightLayer = [CAShapeLayer layer];
|
|
|
|
_highlightLayer.fillColor = [UIColor colorWithWhite:0 alpha:0.25].CGColor;
|
|
|
|
[self.layer addSublayer:_highlightLayer];
|
|
|
|
}
|
2015-07-27 14:19:31 +00:00
|
|
|
_highlightLayer.position = (CGPoint){_contentInset.left, _contentInset.top};
|
2015-07-24 15:25:51 +00:00
|
|
|
_highlightLayer.path = highlightPath.CGPath;
|
|
|
|
} else {
|
|
|
|
[_highlightLayer removeFromSuperlayer];
|
|
|
|
_highlightLayer = nil;
|
|
|
|
}
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSNumber *)reactTagAtPoint:(CGPoint)point
|
|
|
|
{
|
2015-05-27 01:39:37 +00:00
|
|
|
NSNumber *reactTag = self.reactTag;
|
2015-02-20 04:10:52 +00:00
|
|
|
|
2015-05-27 01:39:37 +00:00
|
|
|
CGFloat fraction;
|
2015-08-24 10:14:33 +00:00
|
|
|
NSLayoutManager *layoutManager = _textStorage.layoutManagers.firstObject;
|
|
|
|
NSTextContainer *textContainer = layoutManager.textContainers.firstObject;
|
2015-05-27 01:39:37 +00:00
|
|
|
NSUInteger characterIndex = [layoutManager characterIndexForPoint:point
|
|
|
|
inTextContainer:textContainer
|
|
|
|
fractionOfDistanceBetweenInsertionPoints:&fraction];
|
2015-02-20 04:10:52 +00:00
|
|
|
|
|
|
|
// If the point is not before (fraction == 0.0) the first character and not
|
|
|
|
// after (fraction == 1.0) the last character, then the attribute is valid.
|
|
|
|
if (_textStorage.length > 0 && (fraction > 0 || characterIndex > 0) && (fraction < 1 || characterIndex < _textStorage.length - 1)) {
|
|
|
|
reactTag = [_textStorage attribute:RCTReactTagAttributeName atIndex:characterIndex effectiveRange:NULL];
|
|
|
|
}
|
2015-05-27 01:39:37 +00:00
|
|
|
return reactTag;
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
2015-08-03 12:55:48 +00:00
|
|
|
|
|
|
|
- (void)didMoveToWindow
|
|
|
|
{
|
|
|
|
[super didMoveToWindow];
|
|
|
|
|
|
|
|
if (!self.window) {
|
|
|
|
self.layer.contents = nil;
|
|
|
|
if (_highlightLayer) {
|
|
|
|
[_highlightLayer removeFromSuperlayer];
|
|
|
|
_highlightLayer = nil;
|
|
|
|
}
|
|
|
|
} else if (_textStorage.length) {
|
|
|
|
[self setNeedsDisplay];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-14 16:38:45 +00:00
|
|
|
#pragma mark - Accessibility
|
|
|
|
|
|
|
|
- (NSString *)accessibilityLabel
|
|
|
|
{
|
|
|
|
return _textStorage.string;
|
|
|
|
}
|
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
@end
|