Swift   发布时间:2022-03-31  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了在Swift for iOS中将TabBarItem标题字体更改为粗体大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

我正在尝试将所选标签栏项的字体粗细设置为粗体字体.似乎它没有任何效果.知道什么是错的. forState:.Normal按预期工作,对于State:.SELEcted无效. let tabBarItem0 = tabBar.items![0] as! UITabBarItem var SELEctedImage0 : UIImage = UIImage(named:"ic_tabbar_item_
我正在尝试将所选标签栏项的字体粗细设置为@L_696_1@体.似乎它没有任何效果.知道什么是错的. forState:.Normal按预期工作,对于State:.SELEcted无效.
let tabBarItem0 = tabBar.items![0] as! UITabBarItem
var SELEctedImage0 : UIImage = UIImage(named:"ic_tabbar_item_one")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
var fontLight:UIFont = UIFont(name: "HelveticaNeue-UltraLight",size: 12)!
var fontBold:UIFont = UIFont(name: "HelveticaNeue-Bold",size: 12)!

tabBarItem0.image = unSELEctedImage0
tabBarItem0.SELEctedImage = SELEctedImage0
tabBarItem0.title = "Overview"
tabBarItem0.settitleTextAttributes(
    [
        NSForegroundColorAttributename: UIColor.whiteColor(),NSFontAttributename: fontLight
    ],forState: .Normal)

tabBarItem0.settitleTextAttributes(
    [
        NSForegroundColorAttributename: UIColor.whiteColor(),NSFontAttributename: fontBold
    ],forState: UIControlState.SELEcted)
发现解决方案(Swift 3,XCode 8.1)

>在Storyboard中,为每个UITabBarItem提供一个唯一的标记:对于每个标签 – >选择它并转到它的“属性检查器” – >在“标签”字段中为每个人提供一个唯一的编号,但不应使用零(我使用1到4).

这为我们稍后设置,以确定按下哪个选项卡.

>创建一个新的UITabBarController子类,然后分配它:FILE – >新文件 – > iOS Cocoa Touch – >创建一个UITabBarController的子类.将新的.swift文件分配给您的.swift文件
“Identity Inspector”下的UITabBarController.

我们需要在UITabBarController中使用自定义逻辑.

>创建UITabBarItem的新子类,将相同的文件分配给所有UITabBarItem:FILE – >新文件 – > iOS Cocoa Touch – >创建一个UITabBarItem的子类,并为所有选项卡分配相同的子类.

我们的标签项目中需要共享的自定义逻辑.

>将此代码添加到UITabBarItem子类,它设置初始状态(主选项卡粗体,其余未选中),并允许编程选项卡更改:

class MyUITabBarItemSubclass: UITabBarItem {

    //choose initial state fonts and weights here
    let normaltitleFont = UIFont.systemFont(ofSize: 12,weight: UIFontWeightRegular) 
    let SELEctedtitleFont = UIFont.systemFont(ofSize: 12,weight: UIFontWeightBold)

    //choose initial state colors here
    let normaltitleColor = UIColor.gray 
    let SELEctedtitleColor = UIColor.black

    //assigns the proper initial state logic when each tab instantiates 
    override func awakeFromNib() {
        super.awakeFromNib()

        //this tag # should be your priMary tab's Tag* 
        if self.tag == 1 { 
            self.settitleTextAttributes([NSFontAttributename: SELEctedtitleFont,NSForegroundColorAttributename: SELEctedtitleColor],for: UIControlState.normal)
        } else {
            self.settitleTextAttributes([NSFontAttributename: normaltitleFont,NSForegroundColorAttributename: normaltitleColor],for: UIControlState.normal)
        }

    }

}

这里我们设置初始状态,以便在应用程序打开时正确设置选项卡,我们将在下一个子类中处理物理选项卡.

>将此代码添加到UITabBarController子类中,这是在按下选项卡时分配正确状态的逻辑.

class MyUITabBarControllerSubclass: UITabBarController {

    //choose normal and SELEcted fonts here
    let normaltitleFont = UIFont.systemFont(ofSize: 12,weight: UIFontWeightRegular)
    let SELEctedtitleFont = UIFont.systemFont(ofSize: 12,weight: UIFontWeightBold)

    //choose normal and SELEcted colors here
    let normaltitleColor = UIColor.gray
    let SELEctedtitleColor = UIColor.black


    //the following is a delegate method from the UITabBar protocol that's available 
    //to UITabBarController automatically. It sends us information every
    //time a tab is pressed. Since we Tagged our tabs earlier,we'll kNow which one was pressed,//and pass that identifier into a function to set our button states for us

    override func tabBar(_ tabBar: UITabBar,didSELEct item: UITabBarItem) {
        setButtonStates(itemTag: item.tag)
    }


    //the function takes the tabBar.tag as an Int
    func setButtonStates (itemTag: int) {

        //making an array of all the tabs
        let tabs = self.tabBar.items

        //looping through and setTing the states
        var x = 0
        while x < (tabs?.count)! {

            if tabs?[x].tag == itemTag {
                tabs?[x].settitleTextAttributes([NSFontAttributename: SELEctedtitleFont,for: UIControlState.normal)
            } else {
                tabs?[x].settitleTextAttributes([NSFontAttributename: normaltitleFont,for: UIControlState.normal)
            }

            x += 1

        }

    }

}

看起来这很痛苦,因为由于某种原因,标签不能将状态变化识别为“.SELEcted”.我们必须通过仅使用.Normal状态来做所有事情 – 基本上自己检测状态变化.

>您可以通过编程方式更改选项卡并仍然通过…检测状态更改.如果有人有兴趣,我会稍后更新,只需询问.

希望这有帮助!

大佬总结

以上是大佬教程为你收集整理的在Swift for iOS中将TabBarItem标题字体更改为粗体全部内容,希望文章能够帮你解决在Swift for iOS中将TabBarItem标题字体更改为粗体所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。