具有图像和标题的UISegmentedControl [英] UISegmentedControl with Image and Title

查看:133
本文介绍了具有图像和标题的UISegmentedControl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 UIToolBar 中使用 UISegmentedControl 按钮。我不能使用正常的 UIButtonBarItem ,因为我需要设置 tintColor 并支持iOS 4.3。
所以我使用下面的代码:

I am using a UISegmentedControl inside a UIToolBar as button. I can't use a normal UIButtonBarItem, because I need to set the tintColor and support iOS 4.3. So I'm using following code:

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:删除图像。必须有其他方法...

This works great, but is there a way I can have a Text / Title next to my image? The method setTitle: removes the image. There must be a other way...

感谢您的帮助。

-

编辑:
我决定将文本直接添加到 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);
    }
}


推荐答案

this:

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];

这篇关于具有图像和标题的UISegmentedControl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆