mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-12-22 22:25:57 +00:00
Improve ASTextCellNode customization
This commit is contained in:
@@ -100,11 +100,26 @@ typedef NSUInteger ASCellNodeAnimation;
|
|||||||
*/
|
*/
|
||||||
@interface ASTextCellNode : ASCellNode
|
@interface ASTextCellNode : ASCellNode
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initializes a text cell with given text attributes and text insets
|
||||||
|
*/
|
||||||
|
- (instancetype)initWithAttributes:(NSDictionary *)textAttributes insets:(UIEdgeInsets)textInsets;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Text to display.
|
* Text to display.
|
||||||
*/
|
*/
|
||||||
@property (nonatomic, copy) NSString *text;
|
@property (nonatomic, copy) NSString *text;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A dictionary containing key-value pairs for text attributes. You can specify the font, text color, text shadow color, and text shadow offset using the keys listed in NSString UIKit Additions Reference.
|
||||||
|
*/
|
||||||
|
@property (nonatomic, copy) NSDictionary *textAttributes;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The text inset or outset for each edge. The default value is 15.0 horizontal and 11.0 vertical padding.
|
||||||
|
*/
|
||||||
|
@property (nonatomic, assign) UIEdgeInsets textInsets;
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
||||||
NS_ASSUME_NONNULL_END
|
NS_ASSUME_NONNULL_END
|
||||||
|
|||||||
@@ -9,6 +9,7 @@
|
|||||||
#import "ASCellNode+Internal.h"
|
#import "ASCellNode+Internal.h"
|
||||||
|
|
||||||
#import "ASInternalHelpers.h"
|
#import "ASInternalHelpers.h"
|
||||||
|
#import "ASEqualityHelpers.h"
|
||||||
#import <AsyncDisplayKit/_ASDisplayView.h>
|
#import <AsyncDisplayKit/_ASDisplayView.h>
|
||||||
#import <AsyncDisplayKit/ASDisplayNode+Subclasses.h>
|
#import <AsyncDisplayKit/ASDisplayNode+Subclasses.h>
|
||||||
#import <AsyncDisplayKit/ASDisplayNode+Beta.h>
|
#import <AsyncDisplayKit/ASDisplayNode+Beta.h>
|
||||||
@@ -190,46 +191,83 @@
|
|||||||
#pragma mark ASTextCellNode
|
#pragma mark ASTextCellNode
|
||||||
|
|
||||||
@interface ASTextCellNode ()
|
@interface ASTextCellNode ()
|
||||||
{
|
|
||||||
NSString *_text;
|
@property (nonatomic, strong) ASTextNode *textNode;
|
||||||
ASTextNode *_textNode;
|
|
||||||
}
|
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
||||||
|
|
||||||
@implementation ASTextCellNode
|
@implementation ASTextCellNode
|
||||||
|
|
||||||
static const CGFloat kFontSize = 18.0f;
|
static const CGFloat kASTextCellNodeDefaultFontSize = 18.0f;
|
||||||
|
static const CGFloat kASTextCellNodeDefaultHorizontalPadding = 15.0f;
|
||||||
|
static const CGFloat kASTextCellNodeDefaultVerticalPadding = 11.0f;
|
||||||
|
|
||||||
- (instancetype)init
|
- (instancetype)init
|
||||||
{
|
{
|
||||||
if (!(self = [super init]))
|
return [self initWithAttributes:[self defaultTextAttributes] insets:[self defaultTextInsets]];
|
||||||
return nil;
|
}
|
||||||
|
|
||||||
_text = @"";
|
- (instancetype)initWithAttributes:(NSDictionary *)textAttributes insets:(UIEdgeInsets)textInsets
|
||||||
|
{
|
||||||
|
self = [super init];
|
||||||
|
if (self) {
|
||||||
|
_textInsets = textInsets;
|
||||||
|
_textAttributes = [textAttributes copy];
|
||||||
_textNode = [[ASTextNode alloc] init];
|
_textNode = [[ASTextNode alloc] init];
|
||||||
[self addSubnode:_textNode];
|
[self addSubnode:_textNode];
|
||||||
|
}
|
||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
|
|
||||||
- (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize
|
- (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize
|
||||||
{
|
{
|
||||||
static const CGFloat kHorizontalPadding = 15.0f;
|
return [ASInsetLayoutSpec insetLayoutSpecWithInsets:self.textInsets child:self.textNode];
|
||||||
static const CGFloat kVerticalPadding = 11.0f;
|
}
|
||||||
UIEdgeInsets insets = UIEdgeInsetsMake(kVerticalPadding, kHorizontalPadding, kVerticalPadding, kHorizontalPadding);
|
|
||||||
return [ASInsetLayoutSpec insetLayoutSpecWithInsets:insets child:_textNode];
|
- (NSDictionary *)defaultTextAttributes
|
||||||
|
{
|
||||||
|
return @{NSFontAttributeName : [UIFont systemFontOfSize:kASTextCellNodeDefaultFontSize]};
|
||||||
|
}
|
||||||
|
|
||||||
|
- (UIEdgeInsets)defaultTextInsets
|
||||||
|
{
|
||||||
|
return UIEdgeInsetsMake(kASTextCellNodeDefaultVerticalPadding, kASTextCellNodeDefaultHorizontalPadding, kASTextCellNodeDefaultVerticalPadding, kASTextCellNodeDefaultHorizontalPadding);
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void)setTextAttributes:(NSDictionary *)textAttributes
|
||||||
|
{
|
||||||
|
ASDisplayNodeAssertNotNil(textAttributes, @"Invalid text attributes");
|
||||||
|
|
||||||
|
_textAttributes = [textAttributes copy];
|
||||||
|
|
||||||
|
[self updateAttributedString];
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void)setTextInsets:(UIEdgeInsets)textInsets
|
||||||
|
{
|
||||||
|
_textInsets = textInsets;
|
||||||
|
|
||||||
|
[self updateAttributedString];
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)setText:(NSString *)text
|
- (void)setText:(NSString *)text
|
||||||
{
|
{
|
||||||
if (_text == text)
|
if (ASObjectIsEqual(_text, text)) return;
|
||||||
return;
|
|
||||||
|
|
||||||
_text = [text copy];
|
_text = [text copy];
|
||||||
_textNode.attributedString = [[NSAttributedString alloc] initWithString:_text
|
|
||||||
attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:kFontSize]}];
|
[self updateAttributedString];
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void)updateAttributedString
|
||||||
|
{
|
||||||
|
if (_text == nil) {
|
||||||
|
_textNode.attributedString = nil;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
_textNode.attributedString = [[NSAttributedString alloc] initWithString:self.text attributes:self.textAttributes];
|
||||||
[self setNeedsLayout];
|
[self setNeedsLayout];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -9,7 +9,6 @@
|
|||||||
/* Begin PBXBuildFile section */
|
/* Begin PBXBuildFile section */
|
||||||
25A1FA851C02F7AC00193875 /* MosaicCollectionViewLayout.m in Sources */ = {isa = PBXBuildFile; fileRef = 25A1FA841C02F7AC00193875 /* MosaicCollectionViewLayout.m */; };
|
25A1FA851C02F7AC00193875 /* MosaicCollectionViewLayout.m in Sources */ = {isa = PBXBuildFile; fileRef = 25A1FA841C02F7AC00193875 /* MosaicCollectionViewLayout.m */; };
|
||||||
25A1FA881C02FCB000193875 /* ImageCellNode.m in Sources */ = {isa = PBXBuildFile; fileRef = 25A1FA871C02FCB000193875 /* ImageCellNode.m */; };
|
25A1FA881C02FCB000193875 /* ImageCellNode.m in Sources */ = {isa = PBXBuildFile; fileRef = 25A1FA871C02FCB000193875 /* ImageCellNode.m */; };
|
||||||
9B92C8811BC17D3000EE46B2 /* SupplementaryNode.m in Sources */ = {isa = PBXBuildFile; fileRef = 9B92C8801BC17D3000EE46B2 /* SupplementaryNode.m */; };
|
|
||||||
9BA2CEA11BB2579C00D18414 /* Launchboard.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 9BA2CEA01BB2579C00D18414 /* Launchboard.storyboard */; };
|
9BA2CEA11BB2579C00D18414 /* Launchboard.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 9BA2CEA01BB2579C00D18414 /* Launchboard.storyboard */; };
|
||||||
AC3C4A641A11F47200143C57 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = AC3C4A631A11F47200143C57 /* main.m */; };
|
AC3C4A641A11F47200143C57 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = AC3C4A631A11F47200143C57 /* main.m */; };
|
||||||
AC3C4A671A11F47200143C57 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = AC3C4A661A11F47200143C57 /* AppDelegate.m */; };
|
AC3C4A671A11F47200143C57 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = AC3C4A661A11F47200143C57 /* AppDelegate.m */; };
|
||||||
@@ -24,8 +23,6 @@
|
|||||||
25A1FA861C02FCB000193875 /* ImageCellNode.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ImageCellNode.h; sourceTree = "<group>"; };
|
25A1FA861C02FCB000193875 /* ImageCellNode.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ImageCellNode.h; sourceTree = "<group>"; };
|
||||||
25A1FA871C02FCB000193875 /* ImageCellNode.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ImageCellNode.m; sourceTree = "<group>"; };
|
25A1FA871C02FCB000193875 /* ImageCellNode.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ImageCellNode.m; sourceTree = "<group>"; };
|
||||||
2DBAEE96397BB913350C4530 /* Pods.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = Pods.debug.xcconfig; path = "Pods/Target Support Files/Pods/Pods.debug.xcconfig"; sourceTree = "<group>"; };
|
2DBAEE96397BB913350C4530 /* Pods.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = Pods.debug.xcconfig; path = "Pods/Target Support Files/Pods/Pods.debug.xcconfig"; sourceTree = "<group>"; };
|
||||||
9B92C87F1BC17D3000EE46B2 /* SupplementaryNode.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SupplementaryNode.h; sourceTree = "<group>"; };
|
|
||||||
9B92C8801BC17D3000EE46B2 /* SupplementaryNode.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SupplementaryNode.m; sourceTree = "<group>"; };
|
|
||||||
9BA2CEA01BB2579C00D18414 /* Launchboard.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; path = Launchboard.storyboard; sourceTree = "<group>"; };
|
9BA2CEA01BB2579C00D18414 /* Launchboard.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; path = Launchboard.storyboard; sourceTree = "<group>"; };
|
||||||
AC3C4A5E1A11F47200143C57 /* Sample.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Sample.app; sourceTree = BUILT_PRODUCTS_DIR; };
|
AC3C4A5E1A11F47200143C57 /* Sample.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Sample.app; sourceTree = BUILT_PRODUCTS_DIR; };
|
||||||
AC3C4A621A11F47200143C57 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
|
AC3C4A621A11F47200143C57 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
|
||||||
@@ -91,8 +88,6 @@
|
|||||||
25A1FA871C02FCB000193875 /* ImageCellNode.m */,
|
25A1FA871C02FCB000193875 /* ImageCellNode.m */,
|
||||||
AC3C4A8D1A11F80C00143C57 /* Images.xcassets */,
|
AC3C4A8D1A11F80C00143C57 /* Images.xcassets */,
|
||||||
AC3C4A611A11F47200143C57 /* Supporting Files */,
|
AC3C4A611A11F47200143C57 /* Supporting Files */,
|
||||||
9B92C87F1BC17D3000EE46B2 /* SupplementaryNode.h */,
|
|
||||||
9B92C8801BC17D3000EE46B2 /* SupplementaryNode.m */,
|
|
||||||
);
|
);
|
||||||
indentWidth = 2;
|
indentWidth = 2;
|
||||||
path = Sample;
|
path = Sample;
|
||||||
@@ -224,7 +219,6 @@
|
|||||||
files = (
|
files = (
|
||||||
25A1FA851C02F7AC00193875 /* MosaicCollectionViewLayout.m in Sources */,
|
25A1FA851C02F7AC00193875 /* MosaicCollectionViewLayout.m in Sources */,
|
||||||
AC3C4A6A1A11F47200143C57 /* ViewController.m in Sources */,
|
AC3C4A6A1A11F47200143C57 /* ViewController.m in Sources */,
|
||||||
9B92C8811BC17D3000EE46B2 /* SupplementaryNode.m in Sources */,
|
|
||||||
AC3C4A671A11F47200143C57 /* AppDelegate.m in Sources */,
|
AC3C4A671A11F47200143C57 /* AppDelegate.m in Sources */,
|
||||||
AC3C4A641A11F47200143C57 /* main.m in Sources */,
|
AC3C4A641A11F47200143C57 /* main.m in Sources */,
|
||||||
25A1FA881C02FCB000193875 /* ImageCellNode.m in Sources */,
|
25A1FA881C02FCB000193875 /* ImageCellNode.m in Sources */,
|
||||||
|
|||||||
@@ -1,18 +0,0 @@
|
|||||||
/* This file provided by Facebook is for non-commercial testing and evaluation
|
|
||||||
* purposes only. Facebook reserves all rights not expressly granted.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
||||||
* FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
||||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
||||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#import <AsyncDisplayKit/AsyncDisplayKit.h>
|
|
||||||
|
|
||||||
@interface SupplementaryNode : ASCellNode
|
|
||||||
|
|
||||||
- (instancetype)initWithText:(NSString *)text;
|
|
||||||
|
|
||||||
@end
|
|
||||||
@@ -1,52 +0,0 @@
|
|||||||
/* This file provided by Facebook is for non-commercial testing and evaluation
|
|
||||||
* purposes only. Facebook reserves all rights not expressly granted.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
||||||
* FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
||||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
||||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#import "SupplementaryNode.h"
|
|
||||||
|
|
||||||
#import <AsyncDisplayKit/ASDisplayNode+Subclasses.h>
|
|
||||||
#import <AsyncDisplayKit/ASInsetLayoutSpec.h>
|
|
||||||
#import <AsyncDisplayKit/ASCenterLayoutSpec.h>
|
|
||||||
|
|
||||||
@implementation SupplementaryNode {
|
|
||||||
ASTextNode *_textNode;
|
|
||||||
}
|
|
||||||
|
|
||||||
- (instancetype)initWithText:(NSString *)text
|
|
||||||
{
|
|
||||||
self = [super init];
|
|
||||||
if (self != nil) {
|
|
||||||
_textNode = [[ASTextNode alloc] init];
|
|
||||||
_textNode.attributedString = [[NSAttributedString alloc] initWithString:text
|
|
||||||
attributes:[self textAttributes]];
|
|
||||||
[self addSubnode:_textNode];
|
|
||||||
}
|
|
||||||
return self;
|
|
||||||
}
|
|
||||||
|
|
||||||
- (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize
|
|
||||||
{
|
|
||||||
ASCenterLayoutSpec *center = [[ASCenterLayoutSpec alloc] init];
|
|
||||||
center.centeringOptions = ASCenterLayoutSpecCenteringY;
|
|
||||||
center.child = _textNode;
|
|
||||||
return center;
|
|
||||||
}
|
|
||||||
|
|
||||||
#pragma mark - Text Formatting
|
|
||||||
|
|
||||||
- (NSDictionary *)textAttributes
|
|
||||||
{
|
|
||||||
return @{
|
|
||||||
NSFontAttributeName: [UIFont preferredFontForTextStyle:UIFontTextStyleHeadline],
|
|
||||||
NSForegroundColorAttributeName: [UIColor grayColor],
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
@end
|
|
||||||
@@ -13,7 +13,6 @@
|
|||||||
|
|
||||||
#import <AsyncDisplayKit/AsyncDisplayKit.h>
|
#import <AsyncDisplayKit/AsyncDisplayKit.h>
|
||||||
#import "MosaicCollectionViewLayout.h"
|
#import "MosaicCollectionViewLayout.h"
|
||||||
#import "SupplementaryNode.h"
|
|
||||||
#import "ImageCellNode.h"
|
#import "ImageCellNode.h"
|
||||||
|
|
||||||
static NSUInteger kNumberOfImages = 14;
|
static NSUInteger kNumberOfImages = 14;
|
||||||
@@ -107,8 +106,14 @@ static NSUInteger kNumberOfImages = 14;
|
|||||||
|
|
||||||
- (ASCellNode *)collectionView:(ASCollectionView *)collectionView nodeForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
|
- (ASCellNode *)collectionView:(ASCollectionView *)collectionView nodeForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
|
||||||
{
|
{
|
||||||
NSString *text = [NSString stringWithFormat:@"Section %zd", indexPath.section + 1];
|
NSDictionary *textAttributes = @{
|
||||||
return [[SupplementaryNode alloc] initWithText:text];
|
NSFontAttributeName: [UIFont preferredFontForTextStyle:UIFontTextStyleHeadline],
|
||||||
|
NSForegroundColorAttributeName: [UIColor grayColor]
|
||||||
|
};
|
||||||
|
UIEdgeInsets textInsets = UIEdgeInsetsMake(11.0, 0, 11.0, 0);
|
||||||
|
ASTextCellNode *textCellNode = [[ASTextCellNode alloc] initWithAttributes:textAttributes insets:textInsets];
|
||||||
|
textCellNode.text = [NSString stringWithFormat:@"Section %zd", indexPath.section + 1];
|
||||||
|
return textCellNode;
|
||||||
}
|
}
|
||||||
|
|
||||||
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
|
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
|
||||||
|
|||||||
Reference in New Issue
Block a user