簡體   English   中英

在UITableViewController中設置標題欄文本的字體和字體大小

[英]Setting font and font-size of title bar text in a UITableViewController

我有一個簡單的導航應用程序的iphone / objective-c

在推入視圖的各種UIViewControllers中,我可以使用類似的東西在標題欄中設置文本

self.title = @"blah blah blah"

有沒有辦法控制標題欄文本中標題的字體字體大小

謝謝!

調整navcontroller標題文本大小的正確方法是設置navigationItem的titleView屬性

像這樣(在viewDidLoad中)

   UILabel* tlabel=[[UILabel alloc] initWithFrame:CGRectMake(0,0, 300, 40)];
        tlabel.text=self.navigationItem.title;
    tlabel.textColor=[UIColor whiteColor];
    tlabel.backgroundColor =[UIColor clearColor];
    tlabel.adjustsFontSizeToFitWidth=YES;
    self.navigationItem.titleView=tlabel;

您可能需要對標簽進行壓花,使其看起來不模糊和扁平:

- (void)viewDidLoad
{
    [super viewDidLoad];

    CGRect frame = CGRectMake(0, 0, 400, 44);
    UILabel *label = [[[UILabel alloc] initWithFrame:frame] autorelease];
    label.backgroundColor = [UIColor clearColor];
    label.font = [UIFont boldSystemFontOfSize:18.0];
    label.textAlignment = UITextAlignmentCenter;
    label.textColor = [UIColor whiteColor];
    label.text = self.navigationItem.title;
    // emboss so that the label looks OK
    [label setShadowColor:[UIColor darkGrayColor]];
    [label setShadowOffset:CGSizeMake(0, -0.5)];
    self.navigationItem.titleView = label;
}

您可以將任何UIView分配給navcontroller的標題區域。

創建一個UILabel並根據需要設置其字體和大小,然后將其分配給UIViewController的navigationItem.titleView屬性。 確保UILabel的backgroundColor設置為clearColor。

這僅適用於頂級導航視圖。 當用戶向下鑽入視圖控制器層次結構並顯示“后退”按鈕時,將忽略備用titleView並顯示常規文本標簽。

如果你想讓它在iphone和ipad上工作,並且想要使標題居中,那么使用下面的代碼。

 - (void)viewDidLoad
{
    [super viewDidLoad];

    UILabel* label=[[UILabel alloc] initWithFrame:CGRectMake(0,0, self.navigationItem.titleView.frame.size.width, 40)];
    label.text=self.navigationItem.title;
    label.textColor=[UIColor whiteColor];
    label.backgroundColor =[UIColor clearColor];
    label.adjustsFontSizeToFitWidth=YES;
    label.font = [AppHelper titleFont];
    label.textAlignment = NSTextAlignmentCenter;
    self.navigationItem.titleView=label;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM