2012-05-03 26 views
89

Tôi có nhiều Bộ điều khiển chế độ xem và tôi muốn đặt màu phông chữ là màu đỏ.Cách đặt phông chữ và màu của tiêu đề trong UINavigationBar bằng cách sử dụng API xuất hiện iOS5?

[[UINavigationBar appearance] setFont:[UIFont boldSystemFontOfSize:12.0]]; 

đang ném lỗi bộ chọn không được công nhận.

vui lòng cung cấp mã để giúp khắc phục những điều sau.

+0

Sau đó, Bạn nên đặt nhãn và xem trên thanh điều hướng ANS thiết lập màu chữ – vishiphone

Trả lời

207

Từ Ray Wenderlich:

http://www.raywenderlich.com/4344/user-interface-customization-in-ios-5

// Customize the title text for *all* UINavigationBars 
[[UINavigationBar appearance] setTitleTextAttributes: 
    [NSDictionary dictionaryWithObjectsAndKeys: 
     [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1.0], 
     UITextAttributeTextColor, 
     [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8], 
     UITextAttributeTextShadowColor, 
     [NSValue valueWithUIOffset:UIOffsetMake(0, -1)], 
     UITextAttributeTextShadowOffset, 
     [UIFont fontWithName:@"Arial-Bold" size:0.0], 
     UITextAttributeFont, 
     nil]]; 

Hoặc nếu bạn thích với đối tượng theo phong cách chữ:

[[UINavigationBar appearance] setTitleTextAttributes:@{ 
    UITextAttributeTextColor: [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1.0], 
    UITextAttributeTextShadowColor: [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8], 
    UITextAttributeTextShadowOffset: [NSValue valueWithUIOffset:UIOffsetMake(0, -1)], 
    UITextAttributeFont: [UIFont fontWithName:@"Arial-Bold" size:0.0], 
}]; 

Sửa dành cho iOS 7 và sau

UITextAttributes là d eprecate như iOS 7, bạn có thể sử dụng như sau:

NSShadow *shadow = [[NSShadow alloc] init]; 
shadow.shadowColor = [UIColor colorWithWhite:.0f alpha:1.f]; 
shadow.shadowOffset = CGSizeMake(0, -1); 

[[UINavigationBar appearance] setTitleTextAttributes:@{ 
    NSForegroundColorAttributeName: [UIColor whiteColor], 
    NSShadowAttributeName: shadow, 
    NSFontAttributeName: [UIFont fontWithName:@"Arial-Bold" size:15.0f] 
    }]; 
+1

Trong hơn 1 giờ, tôi đã đấu tranh với một phần mã từ hướng dẫn, dẫn xén tiêu đề của mục chuyển hướng của tôi sau khi đẩy đầu tiên ...Hãy cẩn thận với kích thước phông chữ, đó là vấn đề trong tình hình của tôi ... Rõ ràng kích thước 0.0 đã không làm việc cho tôi ... – Bartu

+3

từ điển mới theo nghĩa đen sẽ làm cho điều này đẹp hơn rất nhiều – tybro0103

+0

Cũng giống như một lưu ý phụ này là 5.0+ , nếu bạn đang hỗ trợ 4 thì nó đáng làm 'nếu ([navBarInstance respondsToSelector: @selector (appearance)])' đầu tiên bởi vì nó sẽ sụp đổ các phiên bản iOS dưới đây 5. –

-5

Sử dụng dòng mã này

UILabel *badge_Label=[[UILabel alloc]initWithFrame:CGRectMake(5,3, 15, 15)]; 
badge_Label.backgroundColor=[UIColor redcolor]; 
badge_Label.font=[UIFont systemFontOfSize:12]; 
[badge_Label setText:@"20"]; 
[self.navigationController.navigationBar addSubview:badgelabel]; 

Tôi nghĩ rằng đây sẽ rất hữu ích cho bạn.

+0

tôi biết điều này, tôi đã cố gắng sử dụng các tính năng proxy của sự xuất hiện API – carbonr

+0

ok sau đó xin lỗi tôi không biết rằng bởi vì đó là trong ios5 chỉ – vishiphone

22

Đối với mục tiêu triển khai lớn hơn hoặc bằng với iOS 6, bạn nên sử dụng NSShadow thay vì:

NSShadow * shadow = [[NSShadow alloc] init]; 
shadow.shadowColor = [UIColor lightGrayColor]; 
shadow.shadowOffset = CGSizeMake(0, -2); 

NSDictionary * navBarTitleTextAttributes = 
@{ NSForegroundColorAttributeName : [UIColor redColor], 
    NSShadowAttributeName   : shadow, 
    NSFontAttributeName   : [UIFont systemFontOfSize:14] }; 

[[UINavigationBar appearance] setTitleTextAttributes:navBarTitleTextAttributes]; 

enter image description here

5

tôi đã làm điều này bằng cách chỉ thêm một vài dòng mã trong phương thức AppDelegate.m class didFinishLaunchingWithOptions: Sử dụng mã này:

NSDictionary *navbarTitleTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys: 
              [UIColor colorWithRed:255.0f/255.0f green:0.0f/255.0f blue:0.0f/255.0f alpha:1.0],UITextAttributeTextColor, 
              [UIColor clearColor], UITextAttributeTextShadowColor, 
              [NSValue valueWithUIOffset:UIOffsetMake(-1, 0)], UITextAttributeTextShadowOffset, nil]; 

[[UINavigationBar appearance] setTitleTextAttributes:navbarTitleTextAttributes]; 

nó hoạt động cho tôi ...

18

Làm điều này trên iOS 8+ và trong Swift. Không có setTitleTextAttributes cho đối tượng xuất hiện. Thay vào đó, làm điều này:

UINavigationBar.appearance().titleTextAttributes = [NSFontAttributeName : AppTheme.fontWithSize(18)] 
1

Điều này có thể được sử dụng để thiết lập một cái nhìn tùy chỉnh cho một Navigationbar đơn thay vì một thế giới thiết

- (void)updateTitleWithString:(NSString *)title 
{ 
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectZero]; 
    [headerView setAutoresizingMask:UIViewAutoresizingFlexibleWidth]; 
    [headerView setAutoresizesSubviews:YES]; 

    CGFloat headFontSize = (IS_SYSTEM_DEVICE_IPAD ? 25.0f : 19.0f); 
    UIFont *headFont = [UIFont boldSystemFontOfSize: headFontSize ]; 

    NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy]; 
    [style setLineBreakMode:NSLineBreakByTruncatingTail]; 

    CGSize size = [title boundingRectWithSize:CGSizeMake(190,headFontSize + 6) options:NSStringDrawingUsesLineFragmentOrigin 
            attributes:@{NSFontAttributeName : headFont, NSParagraphStyleAttributeName : style} context:nil].size; 

    headerView.frame = CGRectMake(0, 0,size.width,self.navigationController.navigationBar.frame.size.height); 
    float labelHeight = headFontSize + 6; 
    float labelYLoc = ( self.navigationController.navigationBar.frame.size.height - labelHeight)/2; 
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0,labelYLoc, size.width,labelHeight)]; 
    label.backgroundColor = [UIColor clearColor]; 
    label.adjustsFontSizeToFitWidth = YES; 
    label.textAlignment = NSTextAlignmentCenter; 
    label.textColor = [UIColor whiteColor]; 
    label.font = headFont; 
    label.text = title; 
    label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.4]; 
    label.lineBreakMode = NSLineBreakByTruncatingTail; 
    label.shadowOffset = CGSizeMake(0,-1); 
    label.accessibilityLabel = @"<LABEL>"; 
    [headerView addSubview:label]; 

    self.navigationItem.titleView = headerView; 
} 
5

Nếu bạn cần phải làm điều này trong Swift, bạn có thể tạo một mở rộng cho UINavigationBar để cho phép bạn nhận hoặc thiết lập các cài đặt này.

extension UINavigationBar { 
var titleColor: UIColor? { 
    get { 
     if let attributes = self.titleTextAttributes { 
      return attributes[NSForegroundColorAttributeName] as? UIColor 
     } 
     return nil 
    } 
    set { 
     if let value = newValue { 
      self.titleTextAttributes = [NSForegroundColorAttributeName: value] 
     } 
    } 
} 

var titleFont: UIFont? { 
    get { 
     if let attributes = self.titleTextAttributes { 
      return attributes[NSFontAttributeName] as? UIFont 
     } 
     return nil 
    } 
    set { 
     if let value = newValue { 
      self.titleTextAttributes = [NSFontAttributeName: value] 
     } 
    } 
    } 
} 

Sau đó bạn có thể thiết lập màu và phông chữ như thế này:

navigationBar.titleColor = UIColor.redColor() 
navigationBar.titleFont = UIFont.systemFontOfSize(12) 
Các vấn đề liên quan