2011-07-20 88 views

回答

2

我的做法是對UINavigationItem進行子類化並操作標題的設置器,以便它可以創建titleView作爲可以隨意配置的UILabel。

-(void)awakeFromNib{ 
    if(self.title){ 
     [self setTitle:self.title]; 
    } 
} 

-(id)initWithTitle:(NSString *)title{ 
    self = [super init]; 
    if(self){ 
     [self setTitle:title]; 
    } 
    return self; 
} 

-(void)setTitle:(NSString *)title{ 
    [super setTitle:title]; 
    UILabel* label = [[[UILabel alloc] init] autorelease]; 
    label.text = title; 
    label.font = [UIFont fontWithName:@"Helvetica-Bold" size:18]; 
    label.shadowColor = [UIColor lightGrayColor]; 
    label.shadowOffset = CGSizeMake(0, -1); 
    label.textAlignment = UITextAlignmentCenter; 
    label.textColor = [UIColor blackColor]; 
    label.backgroundColor = [UIColor clearColor]; 
    [label sizeToFit]; 
    self.titleView = label; 

}