From f68866e774efa283e5911c1277a879bb6abb7c08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hans=20Kn=C3=B6chel?= Date: Sat, 17 Dec 2016 18:09:21 +0100 Subject: [PATCH] [TIMOB-24237] Expose backgroundSelectedColor --- apidoc/Titanium/UI/Button.yml | 11 ++++++++++- iphone/Classes/TiUIButton.m | 14 +++++++++++--- iphone/Classes/TiUtils.h | 6 ++++++ iphone/Classes/TiUtils.m | 16 ++++++++++++++++ 4 files changed, 43 insertions(+), 4 deletions(-) diff --git a/apidoc/Titanium/UI/Button.yml b/apidoc/Titanium/UI/Button.yml index 0c5e514b8c8..ac652f82a64 100644 --- a/apidoc/Titanium/UI/Button.yml +++ b/apidoc/Titanium/UI/Button.yml @@ -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 . + 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 diff --git a/iphone/Classes/TiUIButton.m b/iphone/Classes/TiUIButton.m index ff267243733..189260c0eeb 100644 --- a/iphone/Classes/TiUIButton.m +++ b/iphone/Classes/TiUIButton.m @@ -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]; @@ -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]; @@ -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]; diff --git a/iphone/Classes/TiUtils.h b/iphone/Classes/TiUtils.h index 69469d4bfd1..5de51e9957e 100644 --- a/iphone/Classes/TiUtils.h +++ b/iphone/Classes/TiUtils.h @@ -704,4 +704,10 @@ typedef enum */ + (BOOL)livePhotoSupported; +/** + Converts a color into an image. + @return The generated image. + */ ++ (UIImage*)imageWithColor:(UIColor*)color; + @end diff --git a/iphone/Classes/TiUtils.m b/iphone/Classes/TiUtils.m index 450b63ce483..a81406596ba 100644 --- a/iphone/Classes/TiUtils.m +++ b/iphone/Classes/TiUtils.m @@ -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