40

カスタムでタイトルのフォント サイズを設定する方法が見つかりませんUIBarButtonItem。これを回避する唯一の方法は、回避したい画像として設定することです。他の提案はありますか?

4

6 に答える 6

82

目的 C:

NSUInteger fontSize = 20;
UIFont *font = [UIFont boldSystemFontOfSize:fontSize];
NSDictionary *attributes = @{NSFontAttributeName: font};

UIBarButtonItem *item = [[UIBarButtonItem alloc] init];

[item setTitle:@"Some Text"];
[item setTitleTextAttributes:attributes forState:UIControlStateNormal];

self.navigationItem.rightBarButtonItem = item;

迅速:

let fontSize:CGFloat = 20;
let font:UIFont = UIFont.boldSystemFont(ofSize: fontSize);
let attributes:[String : Any] = [NSFontAttributeName: font];
    
let item = UIBarButtonItem.init();
    
item.title = "Some Text";
item.setTitleTextAttributes(attributes, for: UIControlState.normal);
    
self.navigationItem.rightBarButtonItem = item;
于 2012-07-07T21:07:54.520 に答える
3

UILabel を作成して使用します-initWithCustomView:

于 2010-04-27T13:27:10.623 に答える
3

kennytm が提案する具体的な例としてUIBarButtonItem、次のようなものを作成します。

UILabel *txtLabel = [[UILabel alloc] initWithFrame:rect];
txtLabel.backgroundColor = [UIColor clearColor];
txtLabel.textColor = [UIColor lightGrayColor];
txtLabel.text = @"This is a custom label";
UIBarButtonItem *btnText = [[[UIBarButtonItem alloc] initWithCustomView:txtLabel] autorelease];

UIToolbar次に、たとえば次のように、中央揃えのテキストとして追加できます。

UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:rect];
toolBar.barStyle = UIBarStyleBlackTranslucent;
UIBarButtonItem *flexSpace1 = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil] autorelease];
UIBarButtonItem *flexSpace2 = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil] autorelease];

[toolBar setItems:[NSArray arrayWithItems:flexSpace1, btnText, flexSpace2, nil]];

(もちろん、適切なフォーマットを取得するにはrect、初期化に使用されtxtLabeltoolBar適切なサイズである必要があります....しかし、それは別の演習です。)

于 2012-03-10T20:08:45.587 に答える
2
[[UIBarButtonItem appearance]setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                     [UIColor colorWithRed:245.0/255.0 green:245.0/255.0 blue:245.0/255.0 alpha:1.0], NSForegroundColorAttributeName,
                                                     [UIFont fontWithName:@"FONT-NAME" size:21.0], NSFontAttributeName, nil]
                                           forState:UIControlStateNormal];
于 2015-02-03T13:38:30.617 に答える