在所有导航栏中更改标题文本颜色和字体

时间:2016-02-19 01:27:57

标签: ios swift uinavigationcontroller uinavigationbar

我正在使用swift 2和最新版本的Xcode,我正在尝试更改每个导航栏中标题文本的字体大小,名称和颜色。我在import pygame import os pygame.init() os.environ['SDL_VIDEODRIVER']='windib' class game(object): def __init__(self): self._running = True self._screen = pygame.display.set_mode((800,600)) self._background = pygame.Surface(self._screen.get_size()) self._background.fill((0,0,0)) pygame.display.set_caption('The Spice is Right') self.scroller1 = pygame.image.load("scroller.png") self.scroller2 = pygame.image.load("scroller.png") self.playerCross = pygame.image.load("") self.playerRect = self.playerCross.get_rect() self.head = pygame.image.load("tyler.png") self.headRect = self.head.get_rect() self.title = pygame.image.load("spiceMain.png") self.timer = 100 self.attack = False self.spiceLevel = 0 self.headRight = False self.headLeft = False self.headUp = False self.headDown = False self.titleScreen = True self.gameMode = False self.bgy = 0 self.bgy2 = -600 def run(self): while self._running: self.handle_input() pygame.display.flip() self.bgMethod() if self.titleScreen: self.backgrounScroll() if self.gameMode: self.bgMethod() self.headMethod() self.player() pygame.quit() def handle_input(self): for evt in pygame.event.get(): if evt.type == pygame.QUIT: self._running = False elif evt.type == pygame.KEYDOWN: if evt.key == pygame.K_ESCAPE: self._running = False def bgMethod(self): self._screen.blit(self._background,(0,0)) def backgroundScroll(self): self._screen.blit(self.scroller1,(0,self.bgy)) self._screen.blit(self.scroller2,(0,self.bgy2)) self._screen.blit(self.title,(400,300)) self.bgy += 4 self.bgy += 4 if self.bgy >= 600: self.bgy == 0 if self.bgy2 >= 600: self.bgy2 == 0 def headMethod(self): self._screen.blit(self.head(self.headRect.x,self.headRect.y)) if self.headRect.x <= 800: self.headRight = True if self.headRect.x >= 800: self.headLeft = True if self.headRect.x <= 0: self.headRight = True if self.headRect.x >= 0: self.headLeft = True if self.headRight: self.headRect.x += 6 if self.headLeft: self.headRect.x -= 6 if self.headRect.y <= 600: self.headDown if self.headRect.y >= 600: self.headUp if self.headRect.y <= 0: self.headDown if self.headRect.y >= 0: self.headUp if self.headUp: self.rectHead.y -= 6 if self.headDown: self.rectHead.y += 6 def player(self): self._screen.blit(self.playerCross,(pygame.mouse.get_pos())) def attackMethod(self): if self.timer == 100: if self.attack: self.timer == 0 self.timer += 1 if self.timer == 100: self.spiceLevel += 1 g= game() g.run 区域的AppDelegate部分编写了以下代码,但它只会更改颜色,字体的大小或类型不会受到影响。当我摆脱颜色线时,标题跟在didFinishLaunchingWithOptions行之后。有没有办法让两行代码都能执行?我还没找到任何可以让你改变代码的东西......

NSFontAttributeName

1 个答案:

答案 0 :(得分:2)

你的代码很傻。您设置titleTextAttributes,然后扔掉您刚刚设置的值替换为其他值!

UINavigationBar.appearance().titleTextAttributes = 
    [NSFontAttributeName: (UIFont(name: "AvenirNext-DemiBold", size: 50.0))!]
UINavigationBar.appearance().titleTextAttributes = 
    [NSForegroundColorAttributeName : UIColor(red: 244/255, green: 234/255, blue: 166/255, alpha: 1.0)]

如果这不是您想要做的,请将titleTextAttributes设置为一个值,即包含两个设置的字典:

UINavigationBar.appearance().titleTextAttributes = [
    NSFontAttributeName: (UIFont(name: "AvenirNext-DemiBold", size: 50.0))!,
    NSForegroundColorAttributeName : UIColor(red: 244/255, green: 234/255, blue: 166/255, alpha: 1.0)
]