2012-01-08 51 views
41

Tôi có một UINavigationBar được thêm vào chế độ xem UIViewController của tôi. Tôi muốn thay đổi các thuộc tính phông chữ. Lưu ý rằng tôi muốn thay đổi một UINavigationBar không điều khiển. Trong ứng dụng của tôi, nơi tôi sử dụng UINavigationController tôi sử dụng self.navigationItem.titleView = label; để hiển thị nhãn tùy chỉnh.Thay đổi thuộc tính phông chữ UINavigationBar?

Tôi làm cách nào để có tiêu đề tùy chỉnh trong UINavigationBar của mình?

P.S tôi sử dụng điều này để đặt văn bản tiêu đề self.navBar.topItem.title = @"Information"; của UINavigationBar của tôi.

Trả lời

86

Từ iOS 5 trở đi, chúng ta phải đặt màu văn bản tiêu đề và phông chữ của thanh điều hướng bằng cách sử dụng từ điển titleTextAttribute (từ điển được xác định trước trong tham chiếu lớp điều khiển UInavigation).

[[UINavigationBar appearance] setTitleTextAttributes: 
    [NSDictionary dictionaryWithObjectsAndKeys: 
     [UIColor blackColor], NSForegroundColorAttributeName, 
      [UIFont fontWithName:@"ArialMT" size:16.0], NSFontAttributeName,nil]]; 

Hướng dẫn dưới đây là hướng dẫn tốt nhất để tùy chỉnh các UIElements như thanh UInavigation, điều khiển UIsegmented, UITabBar. Điều này có thể hữu ích cho bạn

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

+2

@rolandjitsu bạn chỉ cần sử dụng NSFontAttributeName thay vì UITextAttributeFont. – CedricSoubrie

+1

@CedricSoubrie ~ vâng, đó là sự thật, chỉ muốn thông báo cho tác giả về điều đó để chúng tôi có thể có câu trả lời cập nhật :) – Roland

+3

Và UITextAttributeTextColor nên được thay thế bằng NSForegroundColorAttributeName – bobmoff

21

(Điều này là không thể khi sử dụng API Appearance iOS 5.0 mới).

Edit:

iOS> = 5.0:

Đặt Tiêu đề thuộc tính văn bản cho Navigationbar:

// Customize the title text for *all* UINavigationBars 
NSDictionary *settings = @{ 
    UITextAttributeFont     : [UIFont fontWithName:@"YOURFONTNAME" size:20.0], 
    UITextAttributeTextColor   : [UIColor whiteColor], 
    UITextAttributeTextShadowColor  : [UIColor clearColor], 
    UITextAttributeTextShadowOffset  : [NSValue valueWithUIOffset:UIOffsetZero]}; 

[[UINavigationBar appearance] setTitleTextAttributes:settings]; 

iOS < 5,0

UINavigationItem không có một thuộc tính được gọi là nhãn hoặc một cái gì đó, chỉ có một titleView. Bạn chỉ có thể thiết lập font bằng cách thiết lập một nhãn tùy chỉnh như quan điểm này danh hiệu Bạn có thể sử dụng đoạn mã sau: (theo đề nghị here)

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 44)]; 
label.font = [UIFont fontWithName:@"YOURFONTNAME" size:20.0]; 
label.shadowColor = [UIColor clearColor]; 
label.textColor =[UIColor whiteColor]; 
label.text = self.title; 
self.navigationItem.titleView = label;  
[label release]; 
+0

Thật vậy, cảm ơn bạn đã bình luận. Tôi không thể tìm thấy nó vào thời điểm đó. – Tieme

+0

THANK YOU SO MUCH :) – Abo3atef

7

Chỉ cần đặt này trong ứng dụng của đại biểu của bạn

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 

Từ Ray Wenderlich:

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

[[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:@"STHeitiSC-Light" size:0.0], 
UITextAttributeFont, nil]]; 
+0

Không đặt kích thước 0 thành phông chữ của bạn, điều này sẽ gây ra lỗi hình ảnh khi bạn đẩy tới bộ điều khiển chế độ xem. Đầu tiên khi bạn đẩy font chữ sẽ có kích thước 0 và sau đó nó sẽ tự kích thước phù hợp nhưng nó vẫn có thể nhìn thấy và khó chịu. Đặt nó thành 18.0 –

0

Đừng quên để thêm phông chữ để mục tiêu của bạn.

Tôi đã làm mọi thứ có thể được thực hiện về phông chữ và tôi không thể tải nó, khi, trên thực tế phông chữ không phải là thành viên của mục tiêu của tôi.

Vì vậy, hãy kiểm tra Thành viên mục tiêu cũng như trên phông chữ tùy chỉnh đã thêm.

1

liên kết bên dưới sẽ giúp bạn. Nó phụ thuộc vào nơi bạn muốn thiết lập các thuộc tính font trên thanh điều hướng hiện viewController của tất cả bất cứ nơi nào trong ứng dụng

bạn có thể tìm thấy thoải mái hướng dẫn ở đây% http://www.appcoda.com/customize-navigation-status-bar-ios-7/?utm_campaign=iOS_Dev_Weekly_Issue_118&utm_medium=email&utm_source=iOS%2BDev%2BWeekly

Ý tưởng cơ bản là tạo ra dụ NSDictionary và điền nó với phông chữ mong muốn của bạn và các thuộc tính khác. tôi kết thúc với giải pháp này:

trong phương pháp điều khiển -viewDidLoad của bạn sau khi [siêu viewDidLoad] đưa dòng này:

UIColor *color = [UIColor redColor]; 
NSShadow *shadow = [NSShadow new]; 
UIFont *font = [UIFont fontWithName:@"EnterYourFontName" size:20.0] 
shadow.shadowColor = [UIColor greenColor]; 
shadow.shadowBlurRadius = 2; 
shadow.shadowOffset = CGSizeMake(1.0f, 1.0f); 

//fill this dictionary with text attributes 
NSMutableDictionary *topBarTextAttributes = [NSMutableDictionary new]; 
    //ios 7 
topBarTextAttributes[NSForegroundColorAttributeName] = color; 
    //ios 6 
topBarTextAttributes[UITextAttributeTextColor] = color; 
    //ios 6 
topBarTextAttributes[UITextAttributeTextShadowOffset] = [NSValue valueWithCGSize:shadow.shadowOffset]; 
topBarTextAttributes[UITextAttributeTextShadowColor] = shadow.shadowColor; 
    //ios 7 
topBarTextAttributes[NSShadowAttributeName] = shadow; 
    //ios 6 
topBarTextAttributes[UITextAttributeFont] = font; 
    //ios 7 
topBarTextAttributes[NSFontAttributeName] = font; 

//for all the controllers uncomment this line 
// [[UINavigationBar appearance]setTitleTextAttributes:topBarTextAttributes]; 

//for current controller uncoment this line 
// self.navigationController.navigationBar.titleTextAttributes = topBarTextAttributes; 
4

Trong iOS8 nhanh chóng, sử dụng đoạn mã sau để thiết lập font chữ:

var navigationBarAppearance = UINavigationBar.appearance() 
let font = UIFont(name: "Open Sans", size: 17) 
if let font = font { 
    navigationBarAppearance.titleTextAttributes = [NSFontAttributeName: font, NSForegroundColorAttributeName: UIColor.whiteColor()] 
} 
Các vấn đề liên quan