Skip to content

Commit

Permalink
feat(ios): allow images in ListItem editActions (#13719)
Browse files Browse the repository at this point in the history
* feat(ios): allow images in ListItem editActions

* docs: add docs

* docs: add note

* docs: add version number via attribute

Co-authored-by: Hans Knöchel <h.knoechel@lambus.com>
Co-authored-by: Hans Knöchel <hansemannn@users.noreply.github.com>
  • Loading branch information
3 people committed Jan 16, 2023
1 parent d8e3f78 commit 68aa61f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
10 changes: 10 additions & 0 deletions apidoc/Titanium/UI/ListItem.yml
Original file line number Diff line number Diff line change
Expand Up @@ -708,3 +708,13 @@ properties:
By default the background color of the row action is defined by the style applied. Use this property to
override the default background color of the row action.
optional: true

- name: image
type: String
summary: The image/icon of the row action.
description: |
You can combine this property with the `color` property to apply an icon + background color in combination.
Note: When using this property, the `title` property is ignored.
optional: true
since: 12.1.0
platforms: [iphone, ipad, macos]
27 changes: 27 additions & 0 deletions iphone/Classes/TiUIListView.m
Original file line number Diff line number Diff line change
Expand Up @@ -1154,6 +1154,7 @@ - (NSArray *)editActionsFromValue:(id)value
NSString *identifier = [TiUtils stringValue:@"identifier" properties:prop];
int actionStyle = [TiUtils intValue:@"style" properties:prop def:UITableViewRowActionStyleDefault];
TiColor *color = [TiUtils colorValue:@"color" properties:prop];
id image = [prop objectForKey:@"image"];

UITableViewRowAction *theAction = [UITableViewRowAction rowActionWithStyle:actionStyle
title:title
Expand Down Expand Up @@ -1192,6 +1193,14 @@ - (NSArray *)editActionsFromValue:(id)value
if (color) {
theAction.backgroundColor = [color color];
}
if (image) {
NSURL *url = [TiUtils toURL:image proxy:(TiProxy *)self.proxy];
UIImage *nativeImage = [[ImageLoader sharedLoader] loadImmediateImage:url];
if (color) {
nativeImage = [self generateImage:nativeImage withBackgroundColor:[color color]];
}
theAction.backgroundColor = [UIColor colorWithPatternImage:nativeImage];
}
if (!returnArray) {
returnArray = [NSMutableArray arrayWithObject:theAction];
} else {
Expand All @@ -1202,6 +1211,24 @@ - (NSArray *)editActionsFromValue:(id)value
return returnArray;
}

- (UIImage *)generateImage:(UIImage *)image withBackgroundColor:(UIColor *)bgColor
{
CGRect imageRect = CGRectMake(0, 0, image.size.width, image.size.height);
UIGraphicsBeginImageContextWithOptions(imageRect.size, false, [UIScreen mainScreen].scale);

CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);

[bgColor setFill];
CGContextFillRect(context, imageRect);
[image drawInRect:imageRect];

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

return result;
}

#pragma mark - Editing Support Datasource methods.

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
Expand Down

0 comments on commit 68aa61f

Please sign in to comment.