So I am trying to insert both image and text into a UISegmentControl
.Now in this segmentControl
I am looking to place image on top and text at its bottom.So I am using a category on UIImage to put title on it like below.However my image and text are still misplaced after a lot of efforts .Has anybody done this before correctly?Any help is appreciated.
+ (id)imageFromImage:(UIImage*)image string:(NSString*)string color:(UIColor*)color
{
UIFont *font = [UIFont fontWithName:@"Helvetica" size:16];
CGSize expectedTextSize = [string sizeWithAttributes:@{NSFontAttributeName: font}];
int width = expectedTextSize.width + image.size.width + 5;
int height = MAX(expectedTextSize.height, image.size.width);
CGSize size = CGSizeMake((float)width, (float)height);
UIGraphicsBeginImageContextWithOptions(size, NO, 0);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, color.CGColor);
int fontTopPosition = (height + expectedTextSize.height) / 2;
CGPoint textPoint = CGPointMake(image.size.width/2, fontTopPosition);
[string drawAtPoint:textPoint withAttributes:@{NSFontAttributeName: font}];
// Images upside down so flip them
CGAffineTransform flipVertical = CGAffineTransformMake(1, 0, 0, -1, 0, size.height);
CGContextConcatCTM(context, flipVertical);
CGContextDrawImage(context, (CGRect){ {50,0},{image.size.width-10,image.size.height-10}}, [image CGImage]);
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}