UISegmentedControl与图像有问题。当我将带有文字的图像放在段中时,tintcolor绘制我的图像和文本。 对它有任何帮助吗?
在细分中设置UIImage
UIImage *tmp = [UIImage imageNamed:@"UISegmentedControl-active"];
UIImage *imageWithText = [UIImage imageFromImage:tmp string:@"20" color:[UIColor whiteColor]];
[self.segmentedControl setImage:imageWithText forSegmentAtIndex:0];
imageFromImage代码:
+ (id) imageFromImage:(UIImage*)image string:(NSString*)string color:(UIColor*)color
{
UIFont *font = [UIFont systemFontOfSize:16.0];
CGSize expectedTextSize = [string sizeWithAttributes:@{NSFontAttributeName: font}];
int 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(width + 5, fontTopPosition);
// Images upside down so flip them
//CGAffineTransform flipVertical = CGAffineTransformMake(1, 0, 0, -1, 0, size.height);
//CGContextConcatCTM(context, flipVertical);
CGContextDrawImage(context, (CGRect){ {0, (height - image.size.height) / 2}, {image.size.width, image.size.height} }, [image CGImage]);
[string drawAtPoint:textPoint withAttributes:@{NSFontAttributeName: font}];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
图像变为蓝色(默认tintcolor)。
无论如何,如果有人知道如何使用圆形(非圆形)段(按钮)进行分段控制,请提供解决方案。