常用代码片段

  • 常用宏
// 屏幕尺寸
#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;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • iOS开发过程中,使用的一些常用宏定义 字符串是否为空#define kStringIsEmpty(str) ([...
    goyohol阅读 10,753评论 30 84
  • 1、禁止手机睡眠[UIApplication sharedApplication].idleTimerDisabl...
    DingGa阅读 4,816评论 1 6
  • 定义了一些常用的宏,写代码的时候用起来挺方便的,添加了pch文件,设置了相对路径;设置pch文件相对路径的方法:设...
    SnailLi阅读 5,669评论 0 1
  • 晓_b69e阅读 2,490评论 1 0
  • 说到趣味,让我想到的是兴趣。百度搜索含义:使人感到愉快,能引起兴趣的特性。 思考一下我的兴趣,或者让我觉得有趣味的...
    李聘2023阅读 2,532评论 0 0

友情链接更多精彩内容