iphone - 具有图像和标题的 UIToolbar UIBarButtonItem 的文本非常暗淡

标签 iphone uibarbuttonitem uitoolbar

我的 iPhone View 在其工具栏中添加了一些自定义按钮。每个按钮都有图像和文本标题,创建方式如下:

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 时,我可以阅读文本,但它看起来仍然很暗淡。

我也尝试过initWithTitle,然后设置image属性;结果是相同的。

有没有办法让文字变亮?我希望外观类似于 UITabBar,其项目既有图像又有标题。

感谢您的帮助!

最佳答案

我试图使用 UIBarButtonItem 来显示图像和按钮,但我很确定它已锁定以显示其中一个或另一个。使用this thread中的基本思想我想出了一个使用 UIButton 和背景图像的解决方案。我所做的最大缺陷是,当标题文本大小变化很大时,背景图像会拉伸(stretch),导致圆角看起来有点偏离。

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;

@end

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

我的按钮背景图片是:

alt text

关于iphone - 具有图像和标题的 UIToolbar UIBarButtonItem 的文本非常暗淡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1533238/

相关文章:

ios - UIBarButtonItem 不显示在 iOS 7 的 UIToolbar 上

iphone - 如何在 UIToolBar 上添加 UIActivityView 图标?

objective-c - 自定义 QLPreviewController 的操作表

iphone - 我可以将 UIToolbar 项目居中吗?

iphone - UIToolbar渐变修复

iphone - 如何更改导航工具栏的颜色

ios - 如何在 UITextField 返回键上添加操作?

iphone - NSArray与SQLite在iPhone上进行复杂查询

iphone - 构建自己的 "SDK"放入其他第三个应用程序

uiview - 如何在导航栏中添加后退按钮?