输入框和键盘

关于时间的知识 

  1.NSData 时间间隔

  2. NSTimeInterval 时间间隔(时间戳) 基本单位 可以通过它得到我们想要的时间 日期格式如下:

    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. NSDateFormatter 时间格式器 用于时间日期的格式化或者字符串解析为日期对象

  4. 比较时间:

  1)比较两个日期是不是同一个日期 isEqualToDate

   (2)获得较早的日期

  5.给时间排序: NSSortDescriptor

代码部分:


#import "ViewController.h"


@interface ViewController ()<UITextViewDelegate>

{

    UITextView *inputView;

    UIView *bgview;

    CGRect keyBoardRect;

    NSMutableArray *allcontent;

}

@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];


    

    allcontent = [NSMutableArray array];

    

    

//    添加一个观察者 通过通知检查键盘的接收状态

    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyBoard:) name:UIKeyboardWillShowNotification object: nil];//UIKeyboardWillShowNotification可以通过这个字符串检测到通知

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

    

//    为什么要写一个bgview 输入框和上面的发送按钮 同时上移

    bgview = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetHeight([UIScreen mainScreen].bounds)-40, CGRectGetWidth([UIScreen mainScreen].bounds), 40)];

    bgview.backgroundColor = [UIColor lightGrayColor];

    [self.view addSubview:bgview];

    

    

    inputView = [[UITextView alloc]initWithFrame:CGRectMake(50, 5, 200, 30)];

    inputView.layer.cornerRadius = 30/5;

    inputView.delegate = self;

    inputView.backgroundColor = [UIColor whiteColor];

    [bgview addSubview:inputView];

    

    

    

    UIButton *sendButton = [UIButton buttonWithType:UIButtonTypeCustom];

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

    [sendButton setTitle:@"发送" forState:UIControlStateNormal];

    [sendButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal ];

    sendButton.tag = 100;

    sendButton.enabled = NO;//默认不可以使用这个按钮

    sendButton.backgroundColor = [UIColor whiteColor];

    [sendButton addTarget:self action:@selector(sendercontent:) forControlEvents:UIControlEventTouchUpInside];

    [bgview addSubview:sendButton];

  

}


#pragma mark  发送的内容

- (void)sendercontent:(UIButton *)sender

{

    [inputView resignFirstResponder];

    NSLog(@"%@",inputView);

    


    NSDate *curTime = [NSDate date];//获得发送的时间

//    NSLog(@"%@",[NSString stringWithFormat:@"%@",curTime]);//curTime时间间隔


    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];//NSDateFormatter时间间隔 会转换成设备的时间

    formatter.dateFormat = @"hh:mm:ss a 星期:e";//在这里输入你要得时间格式就会得到你想要得格式

    NSString *time = [formatter stringFromDate:curTime];//时间对象调用一下它里面的stringFromDate:curTime转化的方法

//    NSLog(@"%@",time);

    

#pragma mark   时间戳 时间的一个标记

    NSTimeInterval timeInterval = [curTime timeIntervalSince1970];//获得从1970到现在的时间间隔 (通常是时间戳的时间间隔)

    NSString *timeString = [NSString stringWithFormat:@"%f",timeInterval];

    NSLog(@"时间戳:%@",timeString);

    

//    NSDate *date = [NSDate dateWithTimeIntervalSince1970:[timeString doubleValue]];

//    NSLog(@"时间戳转化为时间:%@",[formatter stringFromDate:date]);

#pragma mark 比较时间的先后

    

//    NSDate *earDate = [curTime earlierDate:date];//返回较早的日期

//可以通过  时间间隔 可以计算 未来 或者过去的时间

//     [NSDate dateWithTimeIntervalSinceNow:<#(NSTimeInterval)#>];计算未来的时间(通过现在的时间和时间间隔)

    

//    获得一天的时间间隔

    NSTimeInterval interval = 24*60*60;

//    获得昨天的日期

    NSDate *yesterDay = [NSDate dateWithTimeIntervalSinceNow:-interval];

    formatter.dateFormat = @"yyy-MM-dd";

    

    

    

    

    NSDictionary *dic = @{@"content":inputView.text,@"time":time};

    [allcontent addObject:dic];

    NSSortDescriptor *sorDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"time" ascending:NO];//yes 是升序 no是降序

    NSMutableArray *detaile = [NSMutableArray arrayWithObjects:&sorDescriptor count:1];

    allcontent = [[allcontent sortedArrayUsingDescriptors:detaile]mutableCopy];

//    NSLog(@"%@",allcontent);

   



    inputView.text = @"";

    sender.enabled = NO;

     [sender setTitleColor:[UIColor blackColor] forState:UIControlStateNormal ];

    NSLog(@"%@",[formatter stringFromDate:yesterDay]);

}

#pragma mark 使用textview的代理的方法

- (void)textViewDidBeginEditing:(UITextView *)textView

{

//    开始编辑的时候允许按钮发送使用  在这里必须找到tag100的按钮

    UIButton *button = (UIButton *)[bgview viewWithTag:100];//找到100这个button

    button.enabled = YES;

    

}

#pragma mark 通过这个方法可以检测textview输入里面的内容得到它的高度 设置inputview的高度 以及bgview的高度

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

//    更新一下bgviewinputview的坐标

    bgview.frame  = CGRectMake(0, CGRectGetHeight([UIScreen mainScreen].bounds)-textView.contentSize.height-10-CGRectGetHeight(keyBoardRect), CGRectGetWidth([UIScreen mainScreen].bounds), textView.contentSize.height+10);

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


    

    return YES;

}

#pragma mark 检测通知里面传送的内容

- (void)keyBoard:(NSNotification *)not

{

    NSDictionary *info = not.userInfo;//用字典接收通知里面内容

    NSLog(@"%@",info);//研究一下里面的内容

   keyBoardRect = [info[UIKeyboardFrameEndUserInfoKey]CGRectValue];//研究这个的内容

    bgview.frame = CGRectMake(0, CGRectGetMinY(keyBoardRect)-40, CGRectGetWidth([UIScreen mainScreen].bounds), 40);//再次更新bgviewframe


}

@end




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值