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

做"百思不得姐"学到的

作者: 贼海鸥 | 来源:发表于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];

相关文章

  • 做"百思不得姐"学到的

    1.在xib中给控件设置圆角 通过KVC设置 两个参数:layer.cornerRadius,layer.mask...

  • 源码推荐:仿《百思不得姐》项目 简易的瀑布流布局

    源码推荐:仿《百思不得姐》项目 简易的瀑布流布局 源码推荐:仿《百思不得姐》项目 简易的瀑布流布局

  • 百思不得姐

    文/华衣 有一对可爱的蚂蚁花。 文静和淘气。 它们一起玩耍,一起寻找食物,彼此很要好。甚至相信它们的友谊会长久。 ...

  • 百思不得姐

    最近几个月,几乎天天都在简书遨游。一边欣赏着各位老师和友友们的心得佳作,一边抒发一下内心的所思所想,吸收是为了更好...

  • 仿百思不得姐

    使用swift4.0 mvvm仿写的百思不得姐, 思路上按照小码哥OC版仿做, 易于理解适合swift初学者学习U...

  • 百思不得其姐!

    我偏不信邪! 1,他妈的T我又不行 2,他妈的找他私聊也不行 3,他妈的找他群更加不行不行 4,他妈的大家不聊闭嘴...

  • 项目实战 百思不得姐

    01 项目界面框架搭 https://www.jianshu.com/p/71996f9e4048 02 父子控制...

  • OC项目百思不得姐

    第一课 颜色常识 BuDeJie项目搭建 处理tabbar选中标题 UIappearance appearance...

  • 百思不得姐(高仿)

    高仿百思不得姐客户端。简单的mvc设计方式。代码中我加了很多注释,方便大家学习交流。 程序中使用到的库 AFNet...

  • 百思不得姐,其一

    最近发现奇事一件,每天午睡后,最想干的事就是多奈两分钟起床上班,可事与愿违,每到下午一点半,准确手机就滴答滴答响,...

网友评论

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

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