美文网首页
swift 3.0+ 常见宏书写

swift 3.0+ 常见宏书写

作者: maxZhang | 来源:发表于2017-05-08 17:48 被阅读45次

swift是全局编译,没有OC中的头文件概念,所以宏文件也就是常量和函数,常用的宏如下,以后发现有需要新的再补充

//
//  HHConstant.swift
//  HHOffer
//
//  Created by Max on 2017/5/8.
//  Copyright © 2017年 maxzhang. All rights reserved.
//

import Foundation
import UIKit

//屏幕高度
let kScreen_Height = UIScreen.main.bounds.size.height;

//屏幕宽度
let kScreen_Width = UIScreen.main.bounds.size.width;

//判断iPhone4
let IPHONE4_DEV:Bool! = (UIScreen.main.bounds.size.height == 960) ? true : false

//判断iPhone5/5c/5s
let IPHONE5_DEV:Bool! = (UIScreen.main.bounds.size.height == 1136) ? true : false

//判断iPhone6/6s
let IPHONE6s_DEV:Bool! = (UIScreen.main.bounds.size.height == 1334) ? true : false

//判断iPhone6p
let IPHONE6p_DEV:Bool! = (UIScreen.main.bounds.size.height == 2208) ? true : false

//其它屏幕尺寸相对iphone6的宽度
func kWithRelIPhone6(width: CGFloat) -> CGFloat {
    return width * kScreen_Width / 750.0;
}

//其它屏幕尺寸相对iphone6的高度
func kHeightRelIPhone6(width: CGFloat) -> CGFloat {
    return width * kScreen_Height / 1334.0;
}

//RGB 16进制转换
func UIColorFromRGB(rgbValue: UInt) -> UIColor {
    return UIColor(
        red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0,
        green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0,
        blue: CGFloat(rgbValue & 0x0000FF) / 255.0,
        alpha: CGFloat(1.0)
    )
}

//通过颜色获取图片
func imageWithColor(color:UIColor, size:CGSize) -> UIImage {
    
    let rect = CGRect.init(x: 0, y: 0, width: size.width, height: size.height);
    UIGraphicsBeginImageContext(rect.size);
    let context = UIGraphicsGetCurrentContext();
    context?.setFillColor(color.cgColor);
    context?.addRect(rect);
    let img = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext();
    return img!;
}

相关文章

网友评论

      本文标题:swift 3.0+ 常见宏书写

      本文链接:https://www.haomeiwen.com/subject/brcvtxtx.html