美文网首页
记录iOS一些通用工具方法(不定时更新)...

记录iOS一些通用工具方法(不定时更新)...

作者: please_smile | 来源:发表于2017-02-25 18:14 被阅读0次

    记录每一点一滴。

    ...

    2017年02月25日(扩展)

    hexColor

    .h文件
    
    + (UIColor *) hexColorWithString: (NSString *)str;
    
    .m文件
    
    + (UIColor *) hexColorWithString: (NSString *)str
    {
        NSString *cString = [[str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
        // String should be 6 or 8 charactersif ([cString length] < 6) return [UIColor blackColor];
        // strip 0X if it appearsif ([cString hasPrefix:@"0X"]) cString = [cString substringFromIndex:2];
        if ([cString hasPrefix:@"#"]) cString = [cString substringFromIndex:1];
        if ([cString length] != 6) return [UIColor blackColor];
        
        // Separate into r, g, b substrings
        NSRange range;
        range.location = 0;
        range.length = 2;
        NSString *rString = [cString substringWithRange:range];
        range.location = 2;
        NSString *gString = [cString substringWithRange:range];
        range.location = 4;
        NSString *bString = [cString substringWithRange:range];
        // Scan values
        unsigned int r, g, b;
        
        [[NSScanner scannerWithString:rString] scanHexInt:&r];
        [[NSScanner scannerWithString:gString] scanHexInt:&g];
        [[NSScanner scannerWithString:bString] scanHexInt:&b];
        
        return [UIColor colorWithRed:((float) r / 255.0f)
                               green:((float) g / 255.0f)
                                blue:((float) b / 255.0f)
                               alpha:1.0f];
    }
    
    

    iOS中通过color创建一张纯色的图片并且可以设置大小:

    .h文件 (通过创建UIImage的分类实现方法)
    /**
    * 通过size生成图片
    **/
    + (UIImage *)createImageWithSize:(CGSize)size color:(UIColor *)color;
    
    
    .m文件
    
    + (UIImage *)createImageWithSize:(CGSize)size color:(UIColor *)color
    {
        // 准备绘制环境
        UIGraphicsBeginImageContext(size);
        // 获取上下文
        CGContextRef context = UIGraphicsGetCurrentContext();
        // 设置颜色
        CGContextSetFillColorWithColor(context, [color CGColor]);
        // 设置渲染范围
        CGContextFillRect(context, CGRectMake(0, 0, size.width, size.height));
        // 取得图片
        UIImage *colorImg = UIGraphicsGetImageFromCurrentImageContext();
        // 结束绘制
        UIGraphicsEndImageContext();
    
        return colorImg;
    }
    
    

    通过字体大小计算NSString的size(这里未考虑其他attributes)

    .h文件 (通过NSString分类的方式实现)
    
    /**
     *返回值是该字符串所占的size(大小)
     *font:传入的字符串所用的字体(字体大小不一样,显示出来的面积也不同)
     *maxSize:CGSizeMake(MAXFLOAT,MAXFLOAT)
     */
    - (CGSize)sizeWithFont:(UIFont *)font maxSize:(CGSize)maxSize;
    
    .m文件
    
    /// 实现方式
    -(CGSize)sizeWithFont:(UIFont *)font maxSize:(CGSize)maxSize
    {
        NSDictionary *attrs = @{NSFontAttributeName : font};
        return [self boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
    }
    

    2017年02月26日更新工具类(继承自NSObject)

    获取随机数并且可以设置范围

    + (int)getRandomNumber:(int)from to:(int)to
    {
        return (int)(from + (arc4random() % (to - from + 1)));
    }
    

    通过文件名获取路径

    /// 根据文件名来获取文件路径(document路径)
    + (NSString *)dataFilePath:(NSString *)fileName
    {
        NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
        NSString *documentDirectory = [path objectAtIndex:0];
        return [documentDirectory stringByAppendingPathComponent:fileName];
    }
    

    相关文章

      网友评论

          本文标题:记录iOS一些通用工具方法(不定时更新)...

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