ASTextNode 文本位置 如果只是简单的AXTextNode替换UILabel 提升性能 并没有设置文本位置的属性 自定义一个HTTextNode
ASDK ASDK(Texture)
.h
#import <AssetsLibrary/AssetsLibrary.h>
//ASTextNode文本居中
@interfaceHTTextNode :ASDisplayNode
- (instancetype)initWithText:(NSString*)text;
@property(nonatomic, strong) NSAttributedString *attributedText;
//可以设置文本位置样式
@property(nonatomic, assign) ASCenterLayoutSpecCenteringOptions option;
@end
.m
#import "HTTextNode.h"
@interface HTTextNode ()
@property (nonatomic, strong) ASTextNode *textNode;
@end
@implementation HTTextNode
- (instancetype)initWithText:(NSString*)text
{
self= [superinit];
if(self!=nil) {
_textNode= [[ASTextNodealloc]init];
_textNode.attributedText = [[NSAttributedString alloc] initWithString:text
attributes:[selftextAttributes]];
[selfaddSubnode:_textNode];
}
return self;
}
-(void)setFrame:(CGRect)frame
{
[supersetFrame:frame];
}
-(void)setOption:(ASCenterLayoutSpecCenteringOptions)option
{
_option= option;
}
-(void)setAttributedText:(NSAttributedString*)attributedText
{
_attributedText= attributedText;
_textNode.attributedText= attributedText;
}
- (ASLayoutSpec*)layoutSpecThatFits:(ASSizeRange)constrainedSize
{
return [ASCenterLayoutSpec centerLayoutSpecWithCenteringOptions:_option?_option:ASCenterLayoutSpecCenteringXY sizingOptions:ASCenterLayoutSpecSizingOptionDefault child:_textNode];
}
#pragma mark - Text Formatting
- (NSDictionary*)textAttributes
{
return @{
NSFontAttributeName: [UIFont systemFontOfSize:32],
NSForegroundColorAttributeName: [UIColor blackColor],
};
}