Skip to content

Instantly share code, notes, and snippets.

@songzhou21
Created May 8, 2017 11:17
Show Gist options
  • Save songzhou21/5e70c78c3112ee313934a18b213f382c to your computer and use it in GitHub Desktop.
Save songzhou21/5e70c78c3112ee313934a18b213f382c to your computer and use it in GitHub Desktop.
plusOne
//
// PlusOne.m
// test
//
// Created by Song Zhou on 08/05/2017.
// Copyright © 2017 Song Zhou. All rights reserved.
//
#import "PlusOne.h"
@implementation PlusOne
+ (UIImage* )plusOneFromImage:(UIImage* )origin {
CGFloat plusOneWidth = origin.size.width * 1/3;
UIView* container = [UIView new];
container.frame = CGRectMake(0, 0, origin.size.width, origin.size.height);
UILabel* plusOneLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, plusOneWidth, plusOneWidth)];
plusOneLabel.textAlignment = NSTextAlignmentCenter;
plusOneLabel.text = @"+1";
plusOneLabel.layer.masksToBounds = YES;
plusOneLabel.backgroundColor = [UIColor redColor];
plusOneLabel.font = [UIFont boldSystemFontOfSize:plusOneWidth * 2/3];
plusOneLabel.textColor = [UIColor whiteColor];
plusOneLabel.layer.cornerRadius = plusOneWidth/2;
UIImageView* imgView = [[UIImageView alloc] initWithImage:origin];
[container addSubview:imgView];
[container addSubview:plusOneLabel];
CGRect imgViewFrame = imgView.bounds;
imgViewFrame.origin.x = 0;
imgViewFrame.origin.y = 0;
imgView.frame = imgViewFrame;
plusOneLabel.center = CGPointMake(CGRectGetWidth(imgView.bounds) - plusOneWidth/2 - 8, plusOneWidth/2 + 8);
UIGraphicsBeginImageContext(container.bounds.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[container.layer renderInContext:context];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment