タイトルテキストの属性を設定してみました。ボタンは、アイコンを使用してからtitleプロパティを設定することで設定されます。アイコンは適切に着色されています。タイトルは「意図されていない」ままです
スクリーンショットを添付。
タイトルテキストの属性を設定してみました。ボタンは、アイコンを使用してからtitleプロパティを設定することで設定されます。アイコンは適切に着色されています。タイトルは「意図されていない」ままです
スクリーンショットを添付。
tintColorがテキストの色に影響を与えない場合、私が知っている唯一の解決策は、基本的にアイコンとテキストの合成画像を作成し、それをボタン画像として使用することです。
UIGraphicsBeginImageContextWithOptions(someSize, NO, 0);
CGContextRef context = UIGraphicsGetCurrentContext();
// draw the image
// Use UIKit NSString additions to draw the text
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
デビッドの提案は機能します。サンプルコードは次のとおりです。
+ (UIImage *) image:(NSString *) imageName withTitle:(NSString *)title {
UIFont *titleFont = [UIFont boldSystemFontOfSize:10];
CGSize textSize = [title sizeWithFont:titleFont];
CGFloat width = MAX(35, textSize.width);
UIGraphicsBeginImageContextWithOptions(CGSizeMake(width, 35), NO, 0);
CGContextRef context = UIGraphicsGetCurrentContext();
UIImage *toolbarImage = [UIImage imageNamed:imageName];
CGContextSaveGState(context); {
CGContextTranslateCTM(context, 0, 35);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextDrawImage(context, CGRectMake((width - toolbarImage.size.width) / 2, 14, toolbarImage.size.width, toolbarImage.size.height), toolbarImage.CGImage);
}
CGContextRestoreGState(context);
[title drawInRect:CGRectMake((width - textSize.width) / 2, 22, textSize.width, textSize.height) withFont:titleFont];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
画像に合うようにいくつかのフレームパラメータを微調整します。