具有图像和标题的UISegmentedControl

编程入门 行业动态 更新时间:2024-10-09 17:25:55
本文介绍了具有图像和标题的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

本文发布于:2023-11-25 20:57:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1631313.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图像   标题   UISegmentedControl

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!