美文网首页
做"百思不得姐"学到的

做"百思不得姐"学到的

作者: 贼海鸥 | 来源:发表于2017-06-15 11:56 被阅读0次
    • 1.在xib中给控件设置圆角

    通过KVC设置


    设置效果.png

    两个参数:layer.cornerRadius,layer.masksToBounds

    • 2.修改textField的占位文字的颜色

    图片.png
    • 1.第一种方法,仅仅修改了placeholder
    NSMutableDictionary *atts = [NSMutableDictionary dictionary];
        atts[NSForegroundColorAttributeName] = [UIColor whiteColor];
        NSAttributedString *placeholder = [[NSAttributedString alloc] initWithString:@"手机号" attributes:atts];
        self.phoneTextField.attributedPlaceholder = placeholder;
    
    • 2.第二种方法,自定义textField,让xib中的textField继承与自定义的textField
    -(void)drawPlaceholderInRect:(CGRect)rect {
        CGFloat h = 25;
        CGFloat y = (rect.size.height - h) / 2;
        CGRect placeholderRect = CGRectMake(0, y, rect.size.width, h);
        NSMutableDictionary *atts = [NSMutableDictionary dictionary];
        atts[NSForegroundColorAttributeName] = [UIColor grayColor];
        atts[NSFontAttributeName] = self.font;
        [self.placeholder drawInRect:placeholderRect withAttributes:atts];
    }
    
    • 3.利用KVC.我们首先需要得到textField隐藏的属性
      我们需要用到运行时技术.需要导入库
    #import <objc/runtime.h>
    

    获得textField的隐藏属性代码如下

    /**
     运行时:苹果官方一套C语言库
     能做很多底层操作(比如访问隐藏的一些成员变量\成员方法...)
     */
    +(void)initialize {
        unsigned int count = 0;
        Ivar *ivars = class_copyIvarList([UITextField class], &count);
        
        for (int i = 0; i < count; i++) {
            // 取出成员变量
            Ivar ivar = *(ivars + i);
            
            // 打印成员变量的名字
            LHXLog(@"%s" , ivar_getName(ivar));
        }
        // 释放
        free(ivars);
    }
    

    那么,你会得到很多的textField的隐藏属性.我们需要的是修改placeholder文字的颜色,那么我们需要这个属性:_placeholderLabel
    那么,我们就可以直接设置如下代码:

    -(void)awakeFromNib {
        [super awakeFromNib];
        [self setValue:[UIColor grayColor] forKeyPath:@"_placeholderLabel.textColor"];
    }
    

    最后,得到的效果是:当光标在textField上闪烁的时候,光标的颜色和placeholder文字的颜色一样,都是白色的.当光标不在textField上的时候,placeholder文字的颜色是灰色的.如下图所示:

    图片.png

    整体代码如下:

    static NSString * const LHXPlaceholderColorKeyPath = @"_placeholderLabel.textColor";
    - (void)awakeFromNib {
        [super awakeFromNib];
        
        // �设置光标颜色和文字颜色一致
        self.tintColor = self.textColor;
        // 不成为第一响应者
        [self resignFirstResponder];
    }
    
    /**
     成为第一响应者
     */
    - (BOOL)becomeFirstResponder {
        [self setValue:self.textColor forKeyPath:LHXPlaceholderColorKeyPath];
        return [super becomeFirstResponder];
    }
    
    /**
     当文本框失去焦点的时候就会调用
     */
    - (BOOL)resignFirstResponder {
        [self setValue:[UIColor grayColor] forKeyPath:LHXPlaceholderColorKeyPath];
        return [super resignFirstResponder];
    }
    

    3.时间的比较

    最简单的方法获取时间的年月日,一现在为例:

    // 当前时间
    NSDate *now = [NSDate date];
    // 日历
    NSCalendar *calendar = [NSCalendar currentCalendar];
    
    // 获取日期的年,月,日
    NSInteger year = [calendar component:NSCalendarUnitYear fromDate:now];
    NSInteger month = [calendar component:NSCalendarUnitMonth fromDate:now];
    NSInteger day = [calendar component:NSCalendarUnitDay fromDate:now];
        
    NSLog(@"%ld, %ld, %ld" , year , month , day);
    

    比较时间差:

    // 当前时间
    NSDate *now = [NSDate date];
    // 你要比较的时间字符串
    // NSString -> NSDate
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    // 设置日期格式:你的时间字符串格式
    fmt.dateFormat = @"yy-MM-dd HH:mm:ss";
    NSDate *create = [fmt dateFromString:create_time];
    // 日历比较法
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    NSDateComponents *cmps = [calendar components:unit fromDate:create toDate:now options:0];
        
    NSLog(@"%ld, %ld, %ld , %ld, %ld, %ld" , cmps.year , cmps.month , cmps.day , cmps.hour , cmps.minute , cmps.second);
    

    4.保存图片到相册

    还需要在info.plist中设置一下提醒:Privacy - Photo Library Usage Description

    图片.png
    - (IBAction)savePicture:(id)sender {
        // 将图片写入相册
        UIImageWriteToSavedPhotosAlbum(self.imageView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
    }
    
    - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
        if (error) {
            [SVProgressHUD showErrorWithStatus:@"保存失败"];
        } else {
            [SVProgressHUD showSuccessWithStatus:@"保存成功"];
        }
    }
    

    5.在view中,你如果不想因为一个按钮的方法而写一个代理的话,那么,可以参考下面的做法.在view中获得view所在的控制器,然后做相应的操作

    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:showPictureVC animated:YES completion:nil];
    

    相关文章

      网友评论

          本文标题:做"百思不得姐"学到的

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