美文网首页
iOS 提升开发效率(工具类别)

iOS 提升开发效率(工具类别)

作者: 高产的白猫 | 来源:发表于2016-11-04 12:00 被阅读0次

    1.自定义UITextField.placeholder颜色

    新建UITextField类别

    .h

    @interface UITextField (LXExtension)
    /** 占位文字颜色 */
    @property (nonatomic, strong) UIColor *lx_placeholderColor;
    @end
    

    .m

    @implementation UITextField (LXExtension)
    /** 通过这个属性名,就可以修改textField内部的占位文字颜色 */
    static NSString * const LXPlaceholderColorKeyPath = @"placeholderLabel.textColor";
    /**
     *  设置占位文字颜色
     */
    - (void)setLx_placeholderColor:(UIColor *)lx_placeholderColor
    {
        // 这3行代码的作用:1> 保证创建出placeholderLabel,2> 保留曾经设置过的占位文字
        NSString *placeholder = self.placeholder;
        self.placeholder = @" ";
        self.placeholder = placeholder;
        // 处理xmg_placeholderColor为nil的情况:如果是nil,恢复成默认的占位文字颜色
        if (lx_placeholderColor == nil) {
            lx_placeholderColor = [UIColor colorWithRed:0 green:0 blue:0.0980392 alpha:0.22];
        }
        // 设置占位文字颜色
        [self setValue:lx_placeholderColor forKeyPath:LXPlaceholderColorKeyPath];
    }
    /**
     *  获得占位文字颜色
     */
    - (UIColor *)lx_placeholderColor
    {
        return [self valueForKeyPath:LXPlaceholderColorKeyPath];
    }
    @end
    

    2.快速自定义导航栏按钮

    新建UIBarButtonItem类别
    .h

    @interface UIBarButtonItem (LXExtension)
    + (instancetype)lx_itemWithImage:(NSString *)image highImage:(NSString *)highImage target:(id)target action:(SEL)action;
    @end
    

    .m

    @implementation UIBarButtonItem (XMGExtension)
    + (instancetype)lx_itemWithImage:(NSString *)image highImage:(NSString *)highImage target:(id)target action:(SEL)action
    {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:highImage] forState:UIControlStateHighlighted];
        [button sizeToFit];
        [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
        return [[self alloc] initWithCustomView:button];
    }
    @end
    
    

    3.计算文件大小,生成缓存,计算文本尺寸
    新建NSString类别
    .h

    @interface NSString (LXExtension)
    /**
     *  md5加密
     */
    + (NSString*)md5HexDigest:(NSString*)input;
    /**
     *  根据文件名计算出文件大小
     */
    - (unsigned long long)lx_fileSize;
    /**
     *  生成缓存目录全路径
     */
    - (instancetype)cacheDir;
    /**
     *  生成文档目录全路径
     */
    - (instancetype)docDir;
    /**
     *  生成临时目录全路径
     */
    - (instancetype)tmpDir;
    
    /**
     *  @brief 根据字数的不同,返回UILabel中的text文字需要占用多少Size
     *  @param size 约束的尺寸
     *  @param font 文本字体
     *  @return 文本的实际尺寸
     */
    - (CGSize)textSizeWithContentSize:(CGSize)size font:(UIFont *)font;
    
    /**
     *  @brief  根据文本字数/文本宽度约束/文本字体 求得text的Size
     *  @param width 宽度约束
     *  @param font  文本字体
     *  @return 文本的实际高度
     */
    - (CGFloat)textHeightWithContentWidth:(CGFloat)width font:(UIFont *)font;
    
    /**
     *  @brief  根据文本字数/文本宽度约束/文本字体 求得text的Size
     *  @param height 宽度约束
     *  @param font  文本字体
     *  @return 文本的实际长度
     */
    - (CGFloat)textWidthWithContentHeight:(CGFloat)height font:(UIFont *)font;
    @end
    

    .m

    #import "NSString+LXExtension.h"
    #import <objc/runtime.h>
    #import <CommonCrypto/CommonDigest.h>
    
    /*CC_MD5_DIGEST_LENGTH*/
    
    #define  MD5_LENGTH   32
    @implementation NSString (LXExtension)
    
    + (NSString*)md5HexDigest:(NSString*)input {
        const char* str = [input UTF8String];
        unsigned char result[CC_MD5_DIGEST_LENGTH];
        CC_MD5(str, strlen(str), result);
        
        NSMutableString *ret = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH*2];
        for(int i = 0; i<CC_MD5_DIGEST_LENGTH; i++) {
            [ret appendFormat:@"%02x",result[i]];
        }
        return ret;
    }
    
    - (unsigned long long)lx_fileSize
    {
        // 计算self这个文件夹\文件的大小
        
        // 文件管理者
        NSFileManager *mgr = [NSFileManager defaultManager];
        
        // 文件类型
        NSDictionary *attrs = [mgr attributesOfItemAtPath:self error:nil];
        NSString *fileType = attrs.fileType;
        
        if ([fileType isEqualToString:NSFileTypeDirectory]) { // 文件夹
            // 获得文件夹的遍历器
            NSDirectoryEnumerator *enumerator = [mgr enumeratorAtPath:self];
            
            // 总大小
            unsigned long long fileSize = 0;
            
            // 遍历所有子路径
            for (NSString *subpath in enumerator) {
                // 获得子路径的全路径
                NSString *fullSubpath = [self stringByAppendingPathComponent:subpath];
                fileSize += [mgr attributesOfItemAtPath:fullSubpath error:nil].fileSize;
            }
            
            return fileSize;
        }
        
        // 文件
        return attrs.fileSize;
    }
    
    - (instancetype)cacheDir
    {
        NSString *dir = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
        return [dir stringByAppendingPathComponent:[self lastPathComponent]];
    }
    - (instancetype)docDir
    {
        NSString *dir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
        return [dir stringByAppendingPathComponent:[self lastPathComponent]];
    }
    
    - (instancetype)tmpDir
    {
        NSString *dir = NSTemporaryDirectory();
        return [dir stringByAppendingPathComponent:[self lastPathComponent]];
    }
    
    - (CGSize)textSizeWithContentSize:(CGSize)size font:(UIFont *)font {
        return [self boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : font} context:nil].size;
    }
    
    - (CGFloat)textHeightWithContentWidth:(CGFloat)width font:(UIFont *)font {
        CGSize size = CGSizeMake(width, MAXFLOAT);
        return [self textSizeWithContentSize:size font:font].height;
    }
    
    - (CGFloat)textWidthWithContentHeight:(CGFloat)height font:(UIFont *)font {
        CGSize size = CGSizeMake(MAXFLOAT, height);
        return [self textSizeWithContentSize:size font:font].width;
    }
    
    @end
    

    相关文章

      网友评论

          本文标题:iOS 提升开发效率(工具类别)

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