iOS-常用代码片段

  • 常用宏
// 屏幕尺寸
#define kMainScreenSize [UIScreen mainScreen].bounds.size
// 屏幕宽度
#define kMainScreenWidth kMainScreenSize.width
// 屏幕高度
#define kMainScreenHeight kMainScreenSize.height
// iOS7
#define IOS_7 ([UIDevice currentDevice].systemVersion.doubleValue < 8.0)
// AppDelegate
#define kAppDelegate ((AppDelegate *)[UIApplication sharedApplication].delegate)
// RGB颜色
#define RGBA(r,g,b,a) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:a]
#define RGB(r,g,b) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1.0]
// 去除字符串前后空格
#define trim(str) ([str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]])
// KeyPath自动提示宏
#define KEY_PATH(obj,keyPath) @(((void)obj.keyPath,#keyPath))
  • 等比例缩放
CGSize scaledSize(CGSize originSize,CGFloat maxW,CGFloat maxH){
    CGSize size = CGSizeZero;
    if (originSize.width > originSize.height) {
        // 大于最大宽度
        if (originSize.width > maxW) {
            size.width = maxW;
            size.height = maxW / originSize.width * originSize.height;
        }
        else{
            size = originSize;
        }
    }
    else{
        // 大于最大高度
        if (originSize.height > maxH) {
            size.height = maxH;
            size.width = maxH /originSize.height * originSize.width;
        }
        else{
            size = originSize;
        }
    }
    return size;
}
原文地址:https://www.cnblogs.com/lancely/p/5782722.html