所以我试图将图像和文本都插入到UISegmentControl
. 现在,segmentControl
我希望将图像放在顶部,将文本放在底部。所以我使用 UIImage 上的一个类别将标题放在它上面,如下所示。但是我的图像和经过大量的努力,文本仍然放错了位置。有没有人正确地这样做过?任何帮助表示赞赏。
+ (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;
}