我的iPhone视图在其工具栏中添加了一些自定义按钮。每个按钮都有一个图像和文本标题,并按如下方式创建:
My iPhone view adds some custom buttons to its toolbar. Each button has both an image and textual title, and is created like this:
UIBarButtonItem *fooButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"foo.png"] style:UIBarButtonItemStylePlain target:self action:@selector(fooButtonPressed:)]; fooButton.title=@"Foo";标题的文字显得很暗;看起来它的alpha值约为0.5。如果我使用默认的UIToolBar barStyle,我根本无法读取文本。当使用UIBarStyleBlack时,我可以读取文本,但它看起来仍然很暗。
The title's text displays very dim; it looks like it has an alpha of about 0.5. If I use the default UIToolBar barStyle, I can't read the text at all. When using UIBarStyleBlack I can read the text but it still looks very dim.
我也试过initWithTitle,然后设置了image属性;结果是一样的。
I also tried initWithTitle, and then set the image property; the results are identical.
有没有办法照亮文字?我希望看起来类似于UITabBar,其项目同时具有图像和标题。
Is there a way to brighten up the text? I am hoping for a look similar to a UITabBar whose items have both an image and title.
感谢您的帮助!
推荐答案我试图使用UIBarButtonItem来显示图像和按钮,但我很确定它被锁定以显示一个或另一个。使用此主题的基本思想想出了一个使用UIButton和背景图像的解决方案。我所做的最大缺陷是,当标题文字的大小变化很大时,背景图像会拉伸,导致圆角略微偏离。
I was trying to use the UIBarButtonItem to display both an image and a button, but I'm pretty sure it's locked down to display one or the other. Using the basic idea from this thread I came up with a solution using a UIButton and a background image. The biggest flaw of what I've done is that when the title text varies in size a lot, the background image stretches causing the rounded corners to look a little off.
CustomBarButtonItem.h
CustomBarButtonItem.h
#import <UIKit/UIKit.h> @interface CustomBarButtonItem : UIBarButtonItem {} - (id) initWithImage:(UIImage *)image title:(NSString *)title target:(id)target action:(SEL)action; @end @interface UIBarButtonItem (CustomBarButtonItem) + (UIBarButtonItem *) barButtonItemWithImage:(UIImage *)image title:(NSString *)title target:(id)target action:(SEL)action; @endCustomBarButtonItem.m
CustomBarButtonItem.m
#import "CustomBarButtonItem.h" @implementation CustomBarButtonItem - (id) initWithImage:(UIImage *)image title:(NSString *)title target:(id)target action:(SEL)action { UIButton *barButton = [UIButton buttonWithType:UIButtonTypeCustom]; UIFont *font = [UIFont boldSystemFontOfSize:13]; barButton.titleLabel.font = font; barButton.titleLabel.shadowOffset = CGSizeMake(0, -1); barButton.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 5); [barButton setImage:image forState:UIControlStateNormal]; [barButton setTitle:title forState:UIControlStateNormal]; [barButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; [barButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted]; [barButton setTitleShadowColor:[[UIColor blackColor] colorWithAlphaComponent:0.5] forState:UIControlStateNormal]; [barButton setBackgroundImage:[UIImage imageNamed:@"bar-button-item-background.png"] forState:UIControlStateNormal]; barButton.frame = CGRectMake(0, 0, image.size.width + 15 + [title sizeWithFont:font].width, 30); if (self = [super initWithCustomView:barButton]) { self.target = target; self.action = action; } return self; } @end @implementation UIBarButtonItem (CustomBarButtonItem) + (UIBarButtonItem *) barButtonItemWithImage:(UIImage *)image title:(NSString *)title target:(id)target action:(SEL)action { return [[[CustomBarButtonItem alloc] initWithImage:image title:title target:target action:action] autorelease]; } @end样本用法:
UIBarButtonItem *customButtonItem = [UIBarButtonItem barButtonItemWithImage:[UIImage imageNamed:@"calendar.png"] title:@"Add to calendar" target:self action:@selector(addToCalendar)];按钮的背景图片是:
更多推荐
具有图像和标题的UIToolbar UIBarButtonItem具有非常暗淡的文本
发布评论