4

私の iPhone ビューでは、いくつかのカスタム ボタンがツールバーに追加されます。各ボタンには画像とテキスト タイトルの両方があり、次のように作成されます。

UIBarButtonItem *fooButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"foo.png"] style:UIBarButtonItemStylePlain target:self action:@selector(fooButtonPressed:)];
fooButton.title=@"Foo";

タイトルのテキストが非常に薄く表示されます。約0.5のアルファがあるようです。デフォルトの UIToolBar の barStyle を使用すると、テキストがまったく読めなくなります。UIBarStyleBlack を使用すると、テキストを読むことができますが、それでも非常に薄暗いように見えます。

また、initWithTitle を試してから、image プロパティを設定しました。結果は同じです。

テキストを明るくする方法はありますか?アイテムに画像とタイトルの両方がある UITabBar に似た外観を望んでいます。

助けてくれてありがとう!

4

3 に答える 3

7

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

ボタンの私の背景画像は次のとおりです。

代替テキスト

于 2010-10-18T01:06:45.997 に答える
2

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;
}
于 2011-12-15T12:50:45.843 に答える
0

ここでの答えは素晴らしいですが、多くの場合、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
于 2013-09-24T06:34:36.337 に答える