mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-12-10 08:20:16 +00:00
Merge pull request #1025 from 1nput0utput/ASButtonNodeImprovements
[ASButtonNode] Add support for backgroundImage. Extend & simplify API with separate title and color.
This commit is contained in:
commit
ea6c7ae938
@ -11,8 +11,9 @@
|
||||
|
||||
@interface ASButtonNode : ASControlNode
|
||||
|
||||
@property (nonatomic, readonly) ASTextNode *titleNode;
|
||||
@property (nonatomic, readonly) ASImageNode *imageNode;
|
||||
@property (nonatomic, readonly) ASTextNode * _Nonnull titleNode;
|
||||
@property (nonatomic, readonly) ASImageNode * _Nonnull imageNode;
|
||||
@property (nonatomic, readonly) ASImageNode * _Nonnull backgroundImageNode;
|
||||
|
||||
/**
|
||||
Spacing between image and title. Defaults to 8.0.
|
||||
@ -36,11 +37,66 @@
|
||||
*/
|
||||
@property (nonatomic, assign) ASVerticalAlignment contentVerticalAlignment;
|
||||
|
||||
/**
|
||||
* Returns the styled title associated with the specified state.
|
||||
*
|
||||
* @param state The state that uses the styled title. The possible values are described in ASControlState.
|
||||
*
|
||||
* @return The title for the specified state.
|
||||
*/
|
||||
- (NSAttributedString * _Nullable)attributedTitleForState:(ASControlState)state;
|
||||
|
||||
- (NSAttributedString *)attributedTitleForState:(ASControlState)state;
|
||||
- (void)setAttributedTitle:(NSAttributedString *)title forState:(ASControlState)state;
|
||||
/**
|
||||
* Sets the styled title to use for the specified state. This will reset styled title previously set with -setTitle:withFont:withColor:forState.
|
||||
*
|
||||
* @param title The styled text string to use for the title.
|
||||
* @param state The state that uses the specified title. The possible values are described in ASControlState.
|
||||
*/
|
||||
- (void)setAttributedTitle:(nullable NSAttributedString *)title forState:(ASControlState)state;
|
||||
|
||||
- (UIImage *)imageForState:(ASControlState)state;
|
||||
- (void)setImage:(UIImage *)image forState:(ASControlState)state;
|
||||
/**
|
||||
* Sets the title to use for the specified state. This will reset styled title previously set with -setAttributedTitle:forState.
|
||||
*
|
||||
* @param title The styled text string to use for the title.
|
||||
* @param font The font to use for the title.
|
||||
* @param color The color to use for the title.
|
||||
* @param state The state that uses the specified title. The possible values are described in ASControlState.
|
||||
*/
|
||||
- (void)setTitle:(nonnull NSString *)title withFont:(nullable UIFont *)font withColor:(nullable UIColor *)color forState:(ASControlState)state;
|
||||
|
||||
/**
|
||||
* Returns the image used for a button state.
|
||||
*
|
||||
* @param state The state that uses the image. Possible values are described in ASControlState.
|
||||
*
|
||||
* @return The image used for the specified state.
|
||||
*/
|
||||
- (UIImage * _Nullable)imageForState:(ASControlState)state;
|
||||
|
||||
/**
|
||||
* Sets the image to use for the specified state.
|
||||
*
|
||||
* @param image The image to use for the specified state.
|
||||
* @param state The state that uses the specified title. The values are described in ASControlState.
|
||||
*/
|
||||
- (void)setImage:(nullable UIImage *)image forState:(ASControlState)state;
|
||||
|
||||
/**
|
||||
* Sets the background image to use for the specified state.
|
||||
*
|
||||
* @param image The image to use for the specified state.
|
||||
* @param state The state that uses the specified title. The values are described in ASControlState.
|
||||
*/
|
||||
- (void)setBackgroundImage:(nullable UIImage *)image forState:(ASControlState)state;
|
||||
|
||||
|
||||
/**
|
||||
* Returns the background image used for a button state.
|
||||
*
|
||||
* @param state The state that uses the image. Possible values are described in ASControlState.
|
||||
*
|
||||
* @return The background image used for the specified state.
|
||||
*/
|
||||
- (UIImage * _Nullable)backgroundImageForState:(ASControlState)state;
|
||||
|
||||
@end
|
||||
|
||||
@ -10,6 +10,7 @@
|
||||
#import "ASStackLayoutSpec.h"
|
||||
#import "ASThread.h"
|
||||
#import "ASDisplayNode+Subclasses.h"
|
||||
#import "ASBackgroundLayoutSpec.h"
|
||||
|
||||
@interface ASButtonNode ()
|
||||
{
|
||||
@ -24,6 +25,11 @@
|
||||
UIImage *_highlightedImage;
|
||||
UIImage *_selectedImage;
|
||||
UIImage *_disabledImage;
|
||||
|
||||
UIImage *_normalBackgroundImage;
|
||||
UIImage *_highlightedBackgroundImage;
|
||||
UIImage *_selectedBackgroundImage;
|
||||
UIImage *_disabledBackgroundImage;
|
||||
}
|
||||
|
||||
@end
|
||||
@ -41,33 +47,50 @@
|
||||
|
||||
_titleNode = [[ASTextNode alloc] init];
|
||||
_imageNode = [[ASImageNode alloc] init];
|
||||
_backgroundImageNode = [[ASImageNode alloc] init];
|
||||
[_backgroundImageNode setContentMode:UIViewContentModeScaleToFill];
|
||||
|
||||
[_titleNode setLayerBacked:YES];
|
||||
[_imageNode setLayerBacked:YES];
|
||||
[_backgroundImageNode setLayerBacked:YES];
|
||||
|
||||
_contentHorizontalAlignment = ASAlignmentMiddle;
|
||||
_contentVerticalAlignment = ASAlignmentCenter;
|
||||
|
||||
[self addSubnode:_backgroundImageNode];
|
||||
[self addSubnode:_titleNode];
|
||||
[self addSubnode:_imageNode];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)setLayerBacked:(BOOL)layerBacked
|
||||
{
|
||||
ASDisplayNodeAssert(!layerBacked, @"ASButtonNode must not be layer backed!");
|
||||
[super setLayerBacked:layerBacked];
|
||||
}
|
||||
|
||||
- (void)setEnabled:(BOOL)enabled
|
||||
{
|
||||
[super setEnabled:enabled];
|
||||
[self updateImage];
|
||||
[self updateTitle];
|
||||
[self updateButtonContent];
|
||||
}
|
||||
|
||||
- (void)setHighlighted:(BOOL)highlighted
|
||||
{
|
||||
[super setHighlighted:highlighted];
|
||||
[self updateImage];
|
||||
[self updateTitle];
|
||||
[self updateButtonContent];
|
||||
}
|
||||
|
||||
- (void)setSelected:(BOOL)selected
|
||||
{
|
||||
[super setSelected:selected];
|
||||
[self updateButtonContent];
|
||||
}
|
||||
|
||||
- (void)updateButtonContent
|
||||
{
|
||||
[self updateBackgroundImage];
|
||||
[self updateImage];
|
||||
[self updateTitle];
|
||||
}
|
||||
@ -120,6 +143,27 @@
|
||||
}
|
||||
}
|
||||
|
||||
- (void)updateBackgroundImage
|
||||
{
|
||||
ASDN::MutexLocker l(_propertyLock);
|
||||
|
||||
UIImage *newImage;
|
||||
if (self.enabled == NO && _disabledBackgroundImage) {
|
||||
newImage = _disabledBackgroundImage;
|
||||
} else if (self.highlighted && _highlightedBackgroundImage) {
|
||||
newImage = _highlightedBackgroundImage;
|
||||
} else if (self.selected && _selectedBackgroundImage) {
|
||||
newImage = _selectedBackgroundImage;
|
||||
} else {
|
||||
newImage = _normalBackgroundImage;
|
||||
}
|
||||
|
||||
if (newImage != self.backgroundImageNode.image) {
|
||||
self.backgroundImageNode.image = newImage;
|
||||
[self setNeedsLayout];
|
||||
}
|
||||
}
|
||||
|
||||
- (CGFloat)contentSpacing
|
||||
{
|
||||
ASDN::MutexLocker l(_propertyLock);
|
||||
@ -152,6 +196,18 @@
|
||||
[self setNeedsLayout];
|
||||
}
|
||||
|
||||
- (void)setTitle:(NSString *)title withFont:(UIFont *)font withColor:(UIColor *)color forState:(ASControlState)state
|
||||
{
|
||||
NSDictionary *attributes = @{
|
||||
NSFontAttributeName: font ? font :[UIFont systemFontOfSize:[UIFont buttonFontSize]],
|
||||
NSForegroundColorAttributeName : color ? color : [UIColor blackColor]
|
||||
};
|
||||
|
||||
NSAttributedString *string = [[NSAttributedString alloc] initWithString:title
|
||||
attributes:attributes];
|
||||
[self setAttributedTitle:string forState:state];
|
||||
}
|
||||
|
||||
- (NSAttributedString *)attributedTitleForState:(ASControlState)state
|
||||
{
|
||||
ASDN::MutexLocker l(_propertyLock);
|
||||
@ -246,6 +302,54 @@
|
||||
[self updateImage];
|
||||
}
|
||||
|
||||
- (void)setBackgroundImage:(UIImage *)image forState:(ASControlState)state
|
||||
{
|
||||
ASDN::MutexLocker l(_propertyLock);
|
||||
switch (state) {
|
||||
case ASControlStateNormal:
|
||||
_normalBackgroundImage = image;
|
||||
break;
|
||||
|
||||
case ASControlStateHighlighted:
|
||||
_highlightedBackgroundImage = image;
|
||||
break;
|
||||
|
||||
case ASControlStateSelected:
|
||||
_selectedBackgroundImage = image;
|
||||
break;
|
||||
|
||||
case ASControlStateDisabled:
|
||||
_disabledBackgroundImage = image;
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
[self updateBackgroundImage];
|
||||
}
|
||||
|
||||
- (UIImage *)backgroundImageForState:(ASControlState)state
|
||||
{
|
||||
ASDN::MutexLocker l(_propertyLock);
|
||||
switch (state) {
|
||||
case ASControlStateNormal:
|
||||
return _normalBackgroundImage;
|
||||
|
||||
case ASControlStateHighlighted:
|
||||
return _highlightedBackgroundImage;
|
||||
|
||||
case ASControlStateSelected:
|
||||
return _selectedBackgroundImage;
|
||||
|
||||
case ASControlStateDisabled:
|
||||
return _disabledBackgroundImage;
|
||||
|
||||
default:
|
||||
return _normalBackgroundImage;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
- (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize
|
||||
{
|
||||
ASStackLayoutSpec *stack = [[ASStackLayoutSpec alloc] init];
|
||||
@ -265,12 +369,18 @@
|
||||
|
||||
stack.children = children;
|
||||
|
||||
return stack;
|
||||
if (self.backgroundImageNode.image) {
|
||||
return [ASBackgroundLayoutSpec backgroundLayoutSpecWithChild:stack
|
||||
background:self.backgroundImageNode];
|
||||
} else {
|
||||
return stack;
|
||||
}
|
||||
}
|
||||
|
||||
- (void)layout
|
||||
{
|
||||
[super layout];
|
||||
self.backgroundImageNode.hidden = self.backgroundImageNode.image == nil;
|
||||
self.imageNode.hidden = self.imageNode.image == nil;
|
||||
self.titleNode.hidden = self.titleNode.attributedString.length > 0 == NO;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user