タイトル通りなのですが
文字とImageを組み合わせてそれをまるっとUIImageで返すメソッドを
作って、それをsectionImagesに入れたところ
ボタン下部にグレーの横線が入ってしまいます。
ObjC
1#import "UIImage+UISegmentIconAndText.h" 2 3@implementation UIImage (UISegmentIconAndText) 4+ (id) imageFromImage:(UIImage*)image string:(NSString*)string color:(UIColor*)color 5{ 6 UIFont *font = [UIFont systemFontOfSize:16.0]; 7 CGSize expectedTextSize = [string sizeWithAttributes:@{NSFontAttributeName: font}]; 8 int width = expectedTextSize.width + image.size.width + 5; 9 int height = MAX(expectedTextSize.height, image.size.width); 10 CGSize size = CGSizeMake((float)width, (float)height); 11 UIGraphicsBeginImageContextWithOptions(size, NO, 0); 12 CGContextRef context = UIGraphicsGetCurrentContext(); 13 CGContextSetFillColorWithColor(context, color.CGColor); 14 int fontTopPosition = (height - expectedTextSize.height) / 2; 15 CGPoint textPoint = CGPointMake(image.size.width + 5, fontTopPosition); 16 17 [string drawAtPoint:textPoint withAttributes:@{NSFontAttributeName: font}]; 18 // Images upside down so flip them 19 CGAffineTransform flipVertical = CGAffineTransformMake(1, 0, 0, -1, 0, size.height); 20 CGContextConcatCTM(context, flipVertical); 21 CGContextDrawImage(context, (CGRect){ {0, (height - image.size.height) / 2}, {image.size.width, image.size.height} }, [image CGImage]); 22 UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); 23 UIGraphicsEndImageContext(); 24 return newImage; 25} 26@end 27
segmentedControlのsectionimageに設定できる
大きさに制約等はありますか?
(追記:サンプルコードはObjCになっていますが,
swiftでの実装が望ましいです。)
あなたの回答
tips
プレビュー