iOS开发之 textView与键盘关系及 时间显示

效果展示:  这个适合做评论的时候使用






1、在创建senderButton 的时候,应该设置默认不可点击,即:senderButton。enable = no; 在调用senderButton 方法的尾部,也得设置senderButton。enable = no;在textView开始编辑的时候 ,senderButton。enable = yes;


2、通过通知 监测 键盘显示的状态

1⃣️、已经显示键盘的时候 UIKeyboardDidShowNotification

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyBoard:) name:UIKeyboardDidShowNotification object:nil];


2⃣️、键盘即将隐藏的时候 UIKeyboardWillHideNotification


[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyBoard:) name:UIKeyboardWillHideNotification object:nil];


3⃣️、键盘显示状态的调用方法


- (void)keyBoard:(NSNotification *)not

{

    NSDictionary *info = not.userInfo;//字典接收通知的信息

    

    NSLog(@"%@",info);

    //keyBoardRect 键盘的位置 全局变量

    keyBoardRect = [info[UIKeyboardFrameEndUserInfoKey]CGRectValue];

    

    bgView.frame = CGRectMake(0, CGRectGetMinY(keyBoardRect)-40, S_w, 40);

    

}




3、UITextView 发生改变的时候 调用的方法


  • (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text

//    通过监测UITextView 输入的内容 得到它的内容高度 把内容的高度设置成 inputView 的高度 以及bgView的高度 ,并与键盘的高度关联起来


    bgView.frame = CGRectMake(0, S_h-textView.contentSize.height-10-CGRectGetHeight(keyBoardRect), S_w, textView.contentSize.height+10);

    

    inputView.frame = CGRectMake(50, 5, 200, textView.contentSize.height);

    

    sendButton.frame = CGRectMake(CGRectGetMaxX(inputView.frame)+10, CGRectGetHeight(bgView.frame)-30-5, 80, 30);

    

    return YES;//因返回值是一个BOOL值,所以得return一个BOOL值


4、显示时间


1⃣️、在点击button的同时,退出键盘

[inputView resignFirstResponder];


2⃣️、初始化时间并显示出来 NSDate


NSDate *curDate = [NSDate date];


NSLog(@"%@",[NSString stringWithFormat:@“%@",curDate]); //显示的是系统自带的时间格式


3⃣️、自定义显示时间  即 设置时间格式  NSDateFormatter 

 

    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];

//    设置 时间格式

    formatter.dateFormat = @"yyyy-MM-dd hhmmss E ";

    

    NSString *time = [formatter stringFromDate:curDate];// curDate 按照时间格式的样式转换成 字符串

    

    NSLog(@"%@",time);//NSDateFormatter 转换的时间 是转换成设备的时间



4⃣️指定根据那个key 进行分类显示  ascending: YES代表升序 NO 代表降序  NSSortDescriptor


NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"time" ascending:NO];

    

    NSMutableArray *sortDescriptorArr = [NSMutableArray arrayWithObjects:&sortDescriptor count:1];

    

    [[allCotent sortedArrayUsingDescriptors:sortDescriptorArr]mutableCopy];

    

    NSLog(@“%@",allCotent);




时间格式 基本了解


 1NSDate 时间格式

2NSTimeInterval 时间间隔 基本单位 S

3NSDateFormatter 时间格式器 用于日期对象的格式化 或者字符串解析为日期对象

 

 时间格式 

 

 日期格式如下:

 y 

 M  年中的月份

 D  当天是今年的第多少天

 d  月份中的天数

 F  月份中的周数

 E  星期几

 a  Am/pm

 H  一天中的小时数(0-23

 k  一天中的小时数(1-24

 K  am/pm 中的小时数(0-11  Number  0

 h  am/pm 中的小时数(1-12  Number  12

 m  小时中的分钟数  Number  30

 s  分钟中的秒数  Number  55

 S  毫秒数  Number  978

 z  时区  General time zone  Pacific Standard Time; PST; GMT-08:00

 Z  时区  RFC 822 time zone  -0800

 

 时间戳

 

 3、比较时间 

 1)比较两个日期是不是相同的时间 isEqualToDate:

  (2)获得较早 较晚的日期 earlierDate  laterDate

 (3)获得两个日期相隔多少秒

     NSTimeInterval timeIntervalSinceDate:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值