mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-12-24 07:05:35 +00:00
Renamed ASStackText... to ASBaselineStack...
This commit is contained in:
@@ -7,6 +7,7 @@
|
||||
*/
|
||||
|
||||
#import <AsyncDisplayKit/ASControlNode.h>
|
||||
#import <AsyncDisplayKit/ASBaselineStackLayoutable.h>
|
||||
|
||||
|
||||
@protocol ASTextNodeDelegate;
|
||||
@@ -30,7 +31,7 @@ typedef NS_ENUM(NSUInteger, ASTextNodeHighlightStyle) {
|
||||
@abstract Draws interactive rich text.
|
||||
@discussion Backed by TextKit.
|
||||
*/
|
||||
@interface ASTextNode : ASControlNode <ASStackTextLayoutable>
|
||||
@interface ASTextNode : ASControlNode <ASBaselineStackLayoutable>
|
||||
|
||||
/**
|
||||
@abstract The attributed string to show.
|
||||
|
||||
@@ -9,15 +9,16 @@
|
||||
*/
|
||||
|
||||
#import <AsyncDisplayKit/ASStackLayoutSpec.h>
|
||||
#import <AsyncDisplayKit/ASBaselineStackLayoutable.h>
|
||||
|
||||
typedef NS_ENUM(NSUInteger, ASStackTextLayoutBaselineAlignment) {
|
||||
typedef NS_ENUM(NSUInteger, ASBaselineStackLayoutBaselineAlignment) {
|
||||
/** No baseline alignment. This is only valid for a vertical stack */
|
||||
ASStackTextLayoutBaselineAlignmentNone,
|
||||
ASBaselineStackLayoutBaselineAlignmentNone,
|
||||
/** Align all children to the first baseline. This is only valid for a horizontal stack */
|
||||
ASStackTextLayoutBaselineAlignmentFirst,
|
||||
ASBaselineStackLayoutBaselineAlignmentFirst,
|
||||
/** Align all children to the last baseline. This is useful when a text node wraps and you want to align
|
||||
to the bottom baseline. This is only valid for a horizontal stack */
|
||||
ASStackTextLayoutBaselineAlignmentLast,
|
||||
ASBaselineStackLayoutBaselineAlignmentLast,
|
||||
};
|
||||
|
||||
|
||||
@@ -26,24 +27,24 @@ typedef struct {
|
||||
ASStackLayoutSpecStyle stackLayoutStyle;
|
||||
|
||||
/** The type of baseline alignment */
|
||||
ASStackTextLayoutBaselineAlignment baselineAlignment;
|
||||
} ASStackTextLayoutSpecStyle;
|
||||
ASBaselineStackLayoutBaselineAlignment baselineAlignment;
|
||||
} ASBaselineStackLayoutSpecStyle;
|
||||
|
||||
|
||||
/**
|
||||
A specialized version of a stack layout that aligns its children on a baseline. This spec only works with
|
||||
ASStackTextLayoutable children.
|
||||
ASBaselineStackLayoutable children.
|
||||
|
||||
If the spec is created with a horizontal direction, the children will be laid on a common baseline.
|
||||
If the spec is created with a vertical direction, a child's vertical spacing will be measured from its
|
||||
baseline instead of from the child's bounding box.
|
||||
*/
|
||||
@interface ASStackTextLayoutSpec : ASLayoutSpec <ASStackTextLayoutable>
|
||||
@interface ASBaselineStackLayoutSpec : ASLayoutSpec <ASBaselineStackLayoutable>
|
||||
|
||||
/**
|
||||
@param style Specifies how children are laid out.
|
||||
@param children ASTextLayoutable children to be positioned.
|
||||
*/
|
||||
+ (instancetype)newWithStyle:(ASStackTextLayoutSpecStyle)style children:(NSArray *)children;
|
||||
+ (instancetype)newWithStyle:(ASBaselineStackLayoutSpecStyle)style children:(NSArray *)children;
|
||||
|
||||
@end
|
||||
@@ -8,7 +8,7 @@
|
||||
*
|
||||
*/
|
||||
|
||||
#import "ASStackTextLayoutSpec.h"
|
||||
#import "ASBaselineStackLayoutSpec.h"
|
||||
#import "ASStackLayoutable.h"
|
||||
|
||||
#import <numeric>
|
||||
@@ -21,13 +21,13 @@
|
||||
#import "ASStackLayoutSpecUtilities.h"
|
||||
#import "ASStackPositionedLayout.h"
|
||||
#import "ASStackUnpositionedLayout.h"
|
||||
#import "ASStackTextPositionedLayout.h"
|
||||
#import "ASBaselineStackPositionedLayout.h"
|
||||
#import "ASThread.h"
|
||||
|
||||
|
||||
@implementation ASStackTextLayoutSpec
|
||||
@implementation ASBaselineStackLayoutSpec
|
||||
{
|
||||
ASStackTextLayoutSpecStyle _textStyle;
|
||||
ASBaselineStackLayoutSpecStyle _textStyle;
|
||||
std::vector<id<ASStackLayoutable>> _stackChildren;
|
||||
ASDN::RecursiveMutex _propertyLock;
|
||||
}
|
||||
@@ -35,16 +35,16 @@
|
||||
@synthesize ascender = _ascender;
|
||||
@synthesize descender = _descender;
|
||||
|
||||
+ (instancetype)newWithStyle:(ASStackTextLayoutSpecStyle)style children:(NSArray *)children
|
||||
+ (instancetype)newWithStyle:(ASBaselineStackLayoutSpecStyle)style children:(NSArray *)children
|
||||
{
|
||||
ASDisplayNodeAssert((style.stackLayoutStyle.direction == ASStackLayoutDirectionHorizontal && style.baselineAlignment != ASStackTextLayoutBaselineAlignmentNone) || style.stackLayoutStyle.direction == ASStackLayoutDirectionVertical, @"baselineAlignment is set to none. If you don't need baseline alignment please use ASStackLayoutSpec");
|
||||
ASDisplayNodeAssert((style.stackLayoutStyle.direction == ASStackLayoutDirectionHorizontal && style.baselineAlignment != ASBaselineStackLayoutBaselineAlignmentNone) || style.stackLayoutStyle.direction == ASStackLayoutDirectionVertical, @"baselineAlignment is set to none. If you don't need baseline alignment please use ASStackLayoutSpec");
|
||||
|
||||
ASStackTextLayoutSpec *spec = [super new];
|
||||
ASBaselineStackLayoutSpec *spec = [super new];
|
||||
if (spec) {
|
||||
spec->_textStyle = style;
|
||||
spec->_stackChildren = std::vector<id<ASStackLayoutable>>();
|
||||
for (id<ASStackTextLayoutable> child in children) {
|
||||
ASDisplayNodeAssert([child conformsToProtocol:@protocol(ASStackTextLayoutable)], @"child must conform to ASStackLayoutable");
|
||||
for (id<ASBaselineStackLayoutable> child in children) {
|
||||
ASDisplayNodeAssert([child conformsToProtocol:@protocol(ASBaselineStackLayoutable)], @"child must conform to ASStackLayoutable");
|
||||
|
||||
spec->_stackChildren.push_back(child);
|
||||
}
|
||||
@@ -63,7 +63,7 @@
|
||||
|
||||
const auto unpositionedLayout = ASStackUnpositionedLayout::compute(_stackChildren, stackStyle, constrainedSize);
|
||||
const auto positionedLayout = ASStackPositionedLayout::compute(unpositionedLayout, stackStyle, constrainedSize);
|
||||
const auto baselinePositionedLayout = ASStackTextPositionedLayout::compute(positionedLayout, _textStyle, constrainedSize);
|
||||
const auto baselinePositionedLayout = ASBaselineStackPositionedLayout::compute(positionedLayout, _textStyle, constrainedSize);
|
||||
|
||||
const CGSize finalSize = directionSize(stackStyle.direction, unpositionedLayout.stackDimensionSum, baselinePositionedLayout.crossSize);
|
||||
|
||||
23
AsyncDisplayKit/Layout/ASBaselineStackLayoutable.h
Normal file
23
AsyncDisplayKit/Layout/ASBaselineStackLayoutable.h
Normal file
@@ -0,0 +1,23 @@
|
||||
//
|
||||
// ASBaselineStackLayoutable.h
|
||||
// AsyncDisplayKit
|
||||
//
|
||||
// Created by Ricky Cancro on 8/21/15.
|
||||
// Copyright (c) 2015 Facebook. All rights reserved.
|
||||
//
|
||||
|
||||
#import "ASStackLayoutable.h"
|
||||
|
||||
@protocol ASBaselineStackLayoutable <ASStackLayoutable>
|
||||
|
||||
/**
|
||||
* @abstract The distance from the top of the layoutable object to its baseline
|
||||
*/
|
||||
@property (nonatomic, readwrite) CGFloat ascender;
|
||||
|
||||
/**
|
||||
* @abstract The distance from the bottom of the layoutable object to its baseline
|
||||
*/
|
||||
@property (nonatomic, readwrite) CGFloat descender;
|
||||
|
||||
@end
|
||||
@@ -50,10 +50,3 @@
|
||||
@property (nonatomic, readwrite) ASStackLayoutAlignSelf alignSelf;
|
||||
|
||||
@end
|
||||
|
||||
@protocol ASStackTextLayoutable <ASStackLayoutable>
|
||||
|
||||
@property (nonatomic, readwrite) CGFloat ascender;
|
||||
@property (nonatomic, readwrite) CGFloat descender;
|
||||
|
||||
@end
|
||||
|
||||
@@ -10,17 +10,17 @@
|
||||
|
||||
#import "ASLayout.h"
|
||||
#import "ASDimension.h"
|
||||
#import "ASStackTextLayoutSpec.h"
|
||||
#import "ASBaselineStackLayoutSpec.h"
|
||||
#import "ASStackPositionedLayout.h"
|
||||
|
||||
struct ASStackTextPositionedLayout {
|
||||
struct ASBaselineStackPositionedLayout {
|
||||
const std::vector<ASLayout *> sublayouts;
|
||||
const CGFloat crossSize;
|
||||
const CGFloat ascender;
|
||||
const CGFloat descender;
|
||||
|
||||
/** Given a positioned layout, computes each child position using baseline alignment. */
|
||||
static ASStackTextPositionedLayout compute(const ASStackPositionedLayout &positionedLayout,
|
||||
const ASStackTextLayoutSpecStyle &textStyle,
|
||||
static ASBaselineStackPositionedLayout compute(const ASStackPositionedLayout &positionedLayout,
|
||||
const ASBaselineStackLayoutSpecStyle &textStyle,
|
||||
const ASSizeRange &constrainedSize);
|
||||
};
|
||||
@@ -8,39 +8,39 @@
|
||||
*
|
||||
*/
|
||||
|
||||
#import "ASStackTextPositionedLayout.h"
|
||||
#import "ASBaselineStackPositionedLayout.h"
|
||||
|
||||
#import "ASLayoutSpecUtilities.h"
|
||||
#import "ASStackLayoutSpecUtilities.h"
|
||||
|
||||
static CGFloat baselineForItem(const ASStackTextLayoutSpecStyle &style,
|
||||
static CGFloat baselineForItem(const ASBaselineStackLayoutSpecStyle &style,
|
||||
const ASLayout *layout) {
|
||||
|
||||
__weak id<ASStackTextLayoutable> textChild = (id<ASStackTextLayoutable>) layout.layoutableObject;
|
||||
__weak id<ASBaselineStackLayoutable> textChild = (id<ASBaselineStackLayoutable>) layout.layoutableObject;
|
||||
switch (style.baselineAlignment) {
|
||||
case ASStackTextLayoutBaselineAlignmentNone:
|
||||
case ASBaselineStackLayoutBaselineAlignmentNone:
|
||||
return 0;
|
||||
case ASStackTextLayoutBaselineAlignmentFirst:
|
||||
case ASBaselineStackLayoutBaselineAlignmentFirst:
|
||||
return textChild.ascender;
|
||||
case ASStackTextLayoutBaselineAlignmentLast:
|
||||
case ASBaselineStackLayoutBaselineAlignmentLast:
|
||||
return layout.size.height + textChild.descender;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static CGFloat baselineOffset(const ASStackTextLayoutSpecStyle &style,
|
||||
static CGFloat baselineOffset(const ASBaselineStackLayoutSpecStyle &style,
|
||||
const ASLayout *l,
|
||||
const CGFloat maxAscender,
|
||||
const CGFloat maxBaseline)
|
||||
{
|
||||
if (style.stackLayoutStyle.direction == ASStackLayoutDirectionHorizontal) {
|
||||
__weak id<ASStackTextLayoutable> textChild = (id<ASStackTextLayoutable>)l.layoutableObject;
|
||||
__weak id<ASBaselineStackLayoutable> textChild = (id<ASBaselineStackLayoutable>)l.layoutableObject;
|
||||
switch (style.baselineAlignment) {
|
||||
case ASStackTextLayoutBaselineAlignmentFirst:
|
||||
case ASBaselineStackLayoutBaselineAlignmentFirst:
|
||||
return maxAscender - textChild.ascender;
|
||||
case ASStackTextLayoutBaselineAlignmentLast:
|
||||
case ASBaselineStackLayoutBaselineAlignmentLast:
|
||||
return maxBaseline - baselineForItem(style, l);
|
||||
case ASStackTextLayoutBaselineAlignmentNone:
|
||||
case ASBaselineStackLayoutBaselineAlignmentNone:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@@ -55,8 +55,8 @@ static CGFloat maxDimensionForLayout(const ASLayout *l,
|
||||
return maxDimension;
|
||||
}
|
||||
|
||||
ASStackTextPositionedLayout ASStackTextPositionedLayout::compute(const ASStackPositionedLayout &positionedLayout,
|
||||
const ASStackTextLayoutSpecStyle &textStyle,
|
||||
ASBaselineStackPositionedLayout ASBaselineStackPositionedLayout::compute(const ASStackPositionedLayout &positionedLayout,
|
||||
const ASBaselineStackLayoutSpecStyle &textStyle,
|
||||
const ASSizeRange &constrainedSize)
|
||||
{
|
||||
ASStackLayoutSpecStyle stackStyle = textStyle.stackLayoutStyle;
|
||||
@@ -68,16 +68,16 @@ ASStackTextPositionedLayout ASStackTextPositionedLayout::compute(const ASStackPo
|
||||
});
|
||||
const CGFloat maxBaseline = baselineIt == positionedLayout.sublayouts.end() ? 0 : baselineForItem(textStyle, *baselineIt);
|
||||
|
||||
// find the largest ascender for all children. This value will be used in offset computation as well as sent back to the ASStackTextLayoutSpec as its ascender.
|
||||
// find the largest ascender for all children. This value will be used in offset computation as well as sent back to the ASBaselineStackLayoutSpec as its ascender.
|
||||
const auto ascenderIt = std::max_element(positionedLayout.sublayouts.begin(), positionedLayout.sublayouts.end(), [&](const ASLayout *a, const ASLayout *b){
|
||||
return ((id<ASStackTextLayoutable>)a.layoutableObject).ascender < ((id<ASStackTextLayoutable>)b.layoutableObject).ascender;
|
||||
return ((id<ASBaselineStackLayoutable>)a.layoutableObject).ascender < ((id<ASBaselineStackLayoutable>)b.layoutableObject).ascender;
|
||||
});
|
||||
const CGFloat maxAscender = baselineIt == positionedLayout.sublayouts.end() ? 0 : ((id<ASStackTextLayoutable>)(*ascenderIt).layoutableObject).ascender;
|
||||
const CGFloat maxAscender = baselineIt == positionedLayout.sublayouts.end() ? 0 : ((id<ASBaselineStackLayoutable>)(*ascenderIt).layoutableObject).ascender;
|
||||
|
||||
CGPoint p = CGPointZero;
|
||||
BOOL first = YES;
|
||||
auto stackedChildren = AS::map(positionedLayout.sublayouts, [&](ASLayout *l) -> ASLayout *{
|
||||
__weak id<ASStackTextLayoutable> textChild = (id<ASStackTextLayoutable>) l.layoutableObject;
|
||||
__weak id<ASBaselineStackLayoutable> textChild = (id<ASBaselineStackLayoutable>) l.layoutableObject;
|
||||
p = p + directionPoint(stackStyle.direction, textChild.spacingBefore, 0);
|
||||
if (first) {
|
||||
// if this is the first item use the previously computed start point
|
||||
@@ -108,11 +108,11 @@ ASStackTextPositionedLayout ASStackTextPositionedLayout::compute(const ASStackPo
|
||||
const auto maxCrossSize = crossDimension(stackStyle.direction, constrainedSize.max);
|
||||
const CGFloat crossSize = MIN(MAX(minCrossSize, largestChildCrossSize), maxCrossSize);
|
||||
|
||||
// find the child with the largest height. Use that child's descender as the descender to pass back to the ASStackTextLayoutSpec.
|
||||
// find the child with the largest height. Use that child's descender as the descender to pass back to the ASBaselineStackLayoutSpec.
|
||||
const auto descenderIt = std::max_element(stackedChildren.begin(), stackedChildren.end(), [&](const ASLayout *a, const ASLayout *b){
|
||||
return a.position.y + a.size.height < b.position.y + b.size.height;
|
||||
});
|
||||
const CGFloat minDescender = descenderIt == stackedChildren.end() ? 0 : ((id<ASStackTextLayoutable>)(*descenderIt).layoutableObject).descender;
|
||||
const CGFloat minDescender = descenderIt == stackedChildren.end() ? 0 : ((id<ASBaselineStackLayoutable>)(*descenderIt).layoutableObject).descender;
|
||||
|
||||
return {stackedChildren, crossSize, maxAscender, minDescender};
|
||||
}
|
||||
Reference in New Issue
Block a user