Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TIMOB-24237] iOS: Expose backgroundSelectedColor (Parity) #8698

Merged
merged 1 commit into from Dec 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 10 additions & 1 deletion apidoc/Titanium/UI/Button.yml
Expand Up @@ -184,7 +184,16 @@ properties:
state only. For iOS, since there is not a trackball, this does nothing.
type: String
platforms: [android, iphone, ipad, mobileweb]


- name: backgroundSelectedColor
summary: Selected background color of the view, as a color name or hex triplet.
description: |
For information about color values, see the "Colors" section of <Titanium.UI>.
type: String
platforms: [iphone, ipad, android, mobileweb]
default: Background color of this view.
since: {iphone: "6.1.0", ipad: "6.1.0", android: 0.9.0, mobileweb: 1.8.0}

- name: backgroundSelectedImage
summary: |
Background image for the button in its selected state, specified as a local file
Expand Down
14 changes: 11 additions & 3 deletions iphone/Classes/TiUIButton.m
Expand Up @@ -167,9 +167,7 @@ -(UIButton*)button
{
if (button==nil)
{
BOOL hasImage = [self.proxy valueForKey:@"backgroundImage"]!=nil;

UIButtonType defaultType = (hasImage==YES) ? UIButtonTypeCustom : UIButtonTypeRoundedRect;
UIButtonType defaultType = [self hasImageProperties] ? UIButtonTypeCustom : UIButtonTypeRoundedRect;
style = [TiUtils intValue:[self.proxy valueForKey:@"style"] def:defaultType];
UIView *btn = [TiButtonUtil buttonWithType:style];
button = (UIButton*)[btn retain];
Expand All @@ -190,6 +188,11 @@ -(UIButton*)button
return button;
}

- (BOOL)hasImageProperties
{
return [self.proxy valueForKey:@"backgroundImage"] || [self.proxy valueForKey:@"backgroundSelectedImage"] || [self.proxy valueForKey:@"backgroundSelectedColor"];
}

- (id)accessibilityElement
{
return [self button];
Expand Down Expand Up @@ -268,6 +271,11 @@ -(void)setBackgroundImage_:(id)value
[self updateBackgroundImage];
}

-(void)setBackgroundSelectedColor_:(id)value
{
[[self button] setBackgroundImage:[TiUtils imageWithColor:[[TiUtils colorValue:value] _color]] forState:UIControlStateHighlighted];
}

-(void)setBackgroundSelectedImage_:(id)value
{
[[self button] setBackgroundImage:[self loadImage:value] forState:UIControlStateHighlighted];
Expand Down
6 changes: 6 additions & 0 deletions iphone/Classes/TiUtils.h
Expand Up @@ -704,4 +704,10 @@ typedef enum
*/
+ (BOOL)livePhotoSupported;

/**
Converts a color into an image.
@return The generated image.
*/
+ (UIImage*)imageWithColor:(UIColor*)color;

@end
16 changes: 16 additions & 0 deletions iphone/Classes/TiUtils.m
Expand Up @@ -2117,4 +2117,20 @@ +(BOOL)validatePencilWithTouch:(UITouch*)touch
}
}

// Credits: http://stackoverflow.com/a/14525049/5537752
+ (UIImage*)imageWithColor:(UIColor*)color
{
CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();

CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);

UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return image;
}

@end