2011-07-20 68 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; 

}