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

发布于 2024-08-07 21:58:52 字数 521 浏览 6 评论 0原文

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

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,其项目既有图像又有标题。

感谢您的帮助!

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

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.

I also tried initWithTitle, and then set the image property; the results are identical.

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.

Thanks for the help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

虫児飞 2024-08-14 21:58:52

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

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

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

#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

Sample usage:

UIBarButtonItem *customButtonItem = [UIBarButtonItem barButtonItemWithImage:[UIImage imageNamed:@"calendar.png"] title:@"Add to calendar" target:self action:@selector(addToCalendar)];

My background image for the button is:

alt text

翻了热茶 2024-08-14 21:58:52

Johnus 的解决方案非常有用。
但我尝试过,但出现了一个小问题:操作未发送到栏按钮项,因此我对初始化程序进行了一些修改:

- (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;
        // I've added just one line of code here
        [barButton addTarget:target
                      action:action
            forControlEvents:UIControlEventTouchUpInside];
    }

    return self;
}

Johnus's solution is pretty useful.
But I've tried it and a little problem occured: the action was not sent to the bar button item, so I've modified the initializer a little :

- (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;
        // I've added just one line of code here
        [barButton addTarget:target
                      action:action
            forControlEvents:UIControlEventTouchUpInside];
    }

    return self;
}
泪意 2024-08-14 21:58:52

这里的答案很好,但是使用 UIKit 类的组合通常比继承更容易一些。

像这样创建一个栏项:

_accountBarItem =
    [[UIBarButtonItem alloc] initWithCustomView:[CustomerBarView buttonWithImage:
    [UIImage imageNamed:@"Account.png"] text:@"Account"]];
[_accountBarItem setTarget:self];
[_accountBarItem setAction:@selector(accountButtonPressed)];

使用图像和标签实现自定义视图

@implementation CustomBarView

+ (instancetype)buttonWithImage:(UIImage*)image text:(NSString*)text
{
    CustomBarView* button = [[CustomBarView alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
    [button setText:text];
    [button setImage:image];
    return button;
}


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        _uiButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [self addSubview:_uiButton];

        _label = [[UILabel alloc] initWithFrame:CGRectZero];
        [_label setTextColor:[UIColor whiteColor]];
        [_label setFont:[UIFont boldApplicationFontOfSize:9]];
        [_label setBackgroundColor:[UIColor clearColor]];
        [_label setTextAlignment:NSTextAlignmentCenter];
        [self addSubview:_label];
    }
    return self;
}


- (void)setText:(NSString*)text
{
    [_label setText:text];
}

- (void)setImage:(UIImage*)image
{
    [_uiButton setImage:image forState:UIControlStateNormal];
}




- (void)layoutSubviews
{
    [super layoutSubviews];
    [_uiButton setFrame:self.bounds];
    [_label setFrame:CGRectMake(2, self.height - 13, self.width - 4, 15)];
}

@end

The answers here are nice, however its often a little easier to use composition rather than inheritance with UIKit classes.

Create a bar item like so:

_accountBarItem =
    [[UIBarButtonItem alloc] initWithCustomView:[CustomerBarView buttonWithImage:
    [UIImage imageNamed:@"Account.png"] text:@"Account"]];
[_accountBarItem setTarget:self];
[_accountBarItem setAction:@selector(accountButtonPressed)];

Implementation of Custom View with Image and Label

@implementation CustomBarView

+ (instancetype)buttonWithImage:(UIImage*)image text:(NSString*)text
{
    CustomBarView* button = [[CustomBarView alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
    [button setText:text];
    [button setImage:image];
    return button;
}


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        _uiButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [self addSubview:_uiButton];

        _label = [[UILabel alloc] initWithFrame:CGRectZero];
        [_label setTextColor:[UIColor whiteColor]];
        [_label setFont:[UIFont boldApplicationFontOfSize:9]];
        [_label setBackgroundColor:[UIColor clearColor]];
        [_label setTextAlignment:NSTextAlignmentCenter];
        [self addSubview:_label];
    }
    return self;
}


- (void)setText:(NSString*)text
{
    [_label setText:text];
}

- (void)setImage:(UIImage*)image
{
    [_uiButton setImage:image forState:UIControlStateNormal];
}




- (void)layoutSubviews
{
    [super layoutSubviews];
    [_uiButton setFrame:self.bounds];
    [_label setFrame:CGRectMake(2, self.height - 13, self.width - 4, 15)];
}

@end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文