How to add image in UITableViewRowAction?

前端 未结 6 1002
囚心锁ツ
囚心锁ツ 2020-11-30 05:58

I\'m trying to add image in UITableView Swipe style. I tried with Emoji text & its working fine

func tableView(_ tableView: UITableView, edi         


        
6条回答
  •  臣服心动
    2020-11-30 06:38

    Here is how I do it in objective-c and should work in swift when translated.

    - (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
        NSString *deleteString = @"Delete";
        CGFloat tableViewCellHeight = [self tableView:tableView heightForRowAtIndexPath:indexPath];
        UIImage *image = [UIImage imageNamed:@"delete_icon"];
    
        CGFloat fittingMultiplier = 0.4f;
        CGFloat iOS8PlusFontSize = 18.0f;
        CGFloat underImageFontSize = 13.0f;
        CGFloat marginHorizontaliOS8Plus = 15.0f;
        CGFloat marginVerticalBetweenTextAndImage = 3.0f;
    
        float titleMultiplier = fittingMultiplier;
    
        NSString *titleSpaceString= [@"" stringByPaddingToLength:[deleteString length]*titleMultiplier withString:@"\u3000" startingAtIndex:0];
    
        UITableViewRowAction *rowAction= [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:titleSpaceString handler:^(UITableViewRowAction *action, NSIndexPath *indexPath){
            //Do Stuff
        }];
    
        CGSize frameGuess=CGSizeMake((marginHorizontaliOS8Plus*2)+[titleSpaceString boundingRectWithSize:CGSizeMake(MAXFLOAT, tableViewCellHeight) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{ NSFontAttributeName: [UIFont systemFontOfSize:iOS8PlusFontSize] } context:nil].size.width, tableViewCellHeight);
    
        CGSize tripleFrame=CGSizeMake(frameGuess.width*3.0f, frameGuess.height*3.0f);
    
        UIGraphicsBeginImageContextWithOptions(tripleFrame, YES, [[UIScreen mainScreen] scale]);
        CGContextRef context=UIGraphicsGetCurrentContext();
    
        [[UIColor blueColor] set];
        CGContextFillRect(context, CGRectMake(0, 0, tripleFrame.width, tripleFrame.height));
    
        CGSize drawnTextSize=[deleteString boundingRectWithSize:CGSizeMake(MAXFLOAT, tableViewCellHeight) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{ NSFontAttributeName: [UIFont systemFontOfSize:underImageFontSize] } context:nil].size;
    
        [image drawAtPoint:CGPointMake((frameGuess.width/2.0f)-([image size].width/2.0f), (frameGuess.height/2.0f)-[image size].height-(marginVerticalBetweenTextAndImage/2.0f)+2.0f)];
    
        [deleteString drawInRect:CGRectMake(((frameGuess.width/2.0f)-(drawnTextSize.width/2.0f))*([[UIApplication sharedApplication] userInterfaceLayoutDirection]==UIUserInterfaceLayoutDirectionRightToLeft ? -1 : 1), (frameGuess.height/2.0f)+(marginVerticalBetweenTextAndImage/2.0f)+2.0f, frameGuess.width, frameGuess.height) withAttributes:@{ NSFontAttributeName: [UIFont systemFontOfSize:underImageFontSize], NSForegroundColorAttributeName: [UIColor whiteColor] }];
    
        [rowAction setBackgroundColor:[UIColor colorWithPatternImage:UIGraphicsGetImageFromCurrentImageContext()]];
        UIGraphicsEndImageContext();
    
        return @[rowAction];
    }
    

提交回复
热议问题