swift 第三課 宏定義 宏方法


swift 與oc 不同,沒有宏的定義就像 oc 可以這樣寫,直接調用:

/*
 默認顏色
 */
#define RGBCOLOR_HEX(h) RGBCOLOR((((h)>>16)&0xFF), (((h)>>8)&0xFF), ((h)&0xFF))
#define RGBCOLOR(r,g,b) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:1]
#define userGreenColor [UIColor colorWithRed:23.0/255 green:146.0/255 blue:137.0/255 alpha:1] #define userBackColor RGBCOLOR_HEX(0xe6e6e6) #define userReportButtonColor RGBCOLOR_HEX(0xF3F3F3)

#define kStatusBarFrame [UIApplication sharedApplication].statusBarFrame

 

swift 就要這樣寫了:

/*
 默認顏色
 */
func RGBColor(r :CGFloat ,g:CGFloat,b:CGFloat) ->UIColor{
    return UIColor.init(colorLiteralRed: Float(r / 255.0), green:Float(g / 255.0) , blue:Float (b / 255.0), alpha: 1)
}

func RGBCOLOR_HEX(h:Int) ->UIColor {
    return RGBColor(r: CGFloat(((h)>>16) & 0xFF), g:   CGFloat(((h)>>8) & 0xFF), b:  CGFloat((h) & 0xFF))
}

let userBackColor = RGBCOLOR_HEX(h: 0xe6e6e6)
let userGreenColor  =  RGBColor(r: 23.0, g: 146.0, b: 137.0)
let  userReportButtonColor = RGBCOLOR_HEX(h: 0xF3F3F3)

let statusBarFrame = UIApplication.shared.statusBarFrame

 

swift 的調用方法:

class ConstController: UIViewController {
    class func customSetting()  {

        self.tabBar.tintColor = userGreenColor //這里是調用的顏色
        UINavigationBar.appearance().tintColor = UIColor.white
        UINavigationBar.appearance().barTintColor = userGreenColor
        UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName:UIColor.white,NSFontAttributeName: UIFont.systemFont(ofSize: 25.0)] //為導航欄設置字體顏色等
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM