7

as ボタンのUISegmentedControl内側を使用しています。を設定して iOS 4.3 をサポートする必要があるためUIToolBar、通常の は使用できません。だから私は次のコードを使用しています:UIButtonBarItemtintColor

UISegmentedControl* searchButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"", nil]];
[searchButton setImage:[UIImage imageNamed:@"search_picto"] forSegmentAtIndex:0];
searchButton.momentary = YES;
searchButton.segmentedControlStyle = UISegmentedControlStyleBar;
searchButton.tintColor = [UIColor colorWithRed:0.27 green:0.60 blue:0.20 alpha:1.00];
[searchButton addTarget:self action:@selector(actionSearch:) forControlEvents:UIControlEventValueChanged];

これはうまく機能しますが、画像の横にテキスト/タイトルを表示する方法はありますか? メソッド setTitle: は画像を削除します。他に方法があるはず...

ご協力いただきありがとうございます。

--

編集:次のようにテキストを直接追加することにしましたUIImageView:

NSString* label = @"My Label";
UIImage* image = [self addText:label toImage:[UIImage imageNamed:@"icon"]];
[_segmentedControl insertSegmentWithImage:image atIndex:0 animated:NO];
[_segmentedControl setWidth:image.size.width + 10]; // add some margin on the right



- (UIImage *)addText:(NSString *)text toImage:(UIImage *)image {
    UIFont* font = [UIFont boldSystemFontOfSize: 12];
    CGSize expectedSize = [text sizeWithFont:font];

    [self retinaAwareUIGraphicsBeginImageContext:CGSizeMake(image.size.width + expectedSize.width + 10, image.size.height)];
    [image drawAtPoint: CGPointZero];
    [[UIColor whiteColor] set];
    [text drawAtPoint: CGPointMake(30, 2) withFont:font];
    image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;
}

- (void)retinaAwareUIGraphicsBeginImageContext:(CGSize) size {
    CGFloat scale = -1.0;
    if (scale<0.0) {
        UIScreen *screen = [UIScreen mainScreen];
        if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 4.0) {
            scale = [screen scale];
        }
        else {
            scale = 0.0;
        }
    }
    if (scale>0.0) {
        UIGraphicsBeginImageContextWithOptions(size, NO, scale);
    }
    else {
        UIGraphicsBeginImageContext(size);
    }
}
4

7 に答える 7

23

あなたはそれを行うことができます:

UIImage のカテゴリを追加します。

UIImage+UISegmentIconAndText.h 内

@interface UIImage (UISegmentIconAndText)

+ (id) imageFromImage:(UIImage*)image string:(NSString*)string color:(UIColor*)color;
+ (id) imageFromImage:(UIImage*)image string:(NSString*)string color:(UIColor*)color position:(NSString*)position;

@end

UIImage+UISegmentIconAndText.m

#import "UIImage+UISegmentIconAndText.h"

@implementation UIImage (UISegmentIconAndText)
+ (id) imageFromImage:(UIImage*)image string:(NSString*)string color:(UIColor*)color
{
    UIFont *font = [UIFont systemFontOfSize:16.0];
    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 + 5, 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){ {0, (height - image.size.height) / 2}, {image.size.width, image.size.height} }, [image CGImage]);
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}
@end

コードでセグメントを実装します。

UIImage *imageWithText = [UIImage imageFromImage:[UIImage imageNamed:@"images/jobaids_tab_icon"] string:@"Hello", nil) color:[UIColor whiteColor]];

[_segmentedController setImage:imageWithText forSegmentAtIndex:0];
于 2014-03-26T06:51:24.430 に答える
1

スイフト用。

segFontは、埋め込みテキストのフォントです。

imageAlignmentは、テキストの左または右に表示されるイメージの配置です。

左揃え(テキストの左側に画像が表示される) の場合はimageAlignment0に設定し、右揃え (テキストの右側に画像が表示される) の場合は1に設定します。

class func textEmbededImage(image: UIImage, string: String, color:UIColor, imageAlignment: Int = 0, segFont: UIFont? = nil) -> UIImage {

let font = segFont ?? UIFont.systemFontOfSize(16.0)

let expectedTextSize: CGSize = (string as NSString).sizeWithAttributes([NSFontAttributeName: font])

let width: CGFloat = expectedTextSize.width + image.size.width + 5.0

let height: CGFloat = max(expectedTextSize.height, image.size.width)

let size: CGSize = CGSizeMake(width, height)

UIGraphicsBeginImageContextWithOptions(size, false, 0)

let context: CGContextRef = UIGraphicsGetCurrentContext()!

CGContextSetFillColorWithColor(context, color.CGColor)

let fontTopPosition: CGFloat = (height - expectedTextSize.height) / 2.0

let textOrigin: CGFloat = (imageAlignment == 0) ? image.size.width + 5 : 0
let textPoint: CGPoint = CGPointMake(textOrigin, fontTopPosition)


string.drawAtPoint(textPoint, withAttributes: [NSFontAttributeName: font])
let flipVertical: CGAffineTransform = CGAffineTransformMake(1, 0, 0, -1, 0, size.height)
CGContextConcatCTM(context, flipVertical)

let alignment: CGFloat =  (imageAlignment == 0) ? 0.0 : expectedTextSize.width + 5.0
CGContextDrawImage(context, CGRectMake(alignment, ((height - image.size.height) / 2.0), image.size.width, image.size.height), image.CGImage)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

return newImage
}
于 2016-09-16T03:50:35.200 に答える
-4

これを試して:

UISegmentedControl* searchButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"", nil]];
[searchButton insertSegmentWithImage:[UIImage imageNamed:@"up_button.png"] atIndex:0 animated:YES];
[searchButton insertSegmentWithImage:[UIImage imageNamed:@"down_button.png"] atIndex:1 animated:YES];
searchButton.segmentedControlStyle = UISegmentedControlStyleBar;
searchButton.tintColor = [UIColor colorWithRed:0.27 green:0.60 blue:0.20 alpha:1.00];
[searchButton setMomentary:YES];
于 2012-11-19T18:58:27.870 に答える