iOSUITextView文本输入视图的使用 -电脑资料

电脑资料 时间:2019-01-01 我要投稿
【www.unjs.com - 电脑资料】

    #import ViewController.h

    @interface ViewController ()

    {

    UIView *bgView;

    UITextView *inputView;

    CGRect keyBoardRect;

    NSMutableArray *allContent;

    }

    @end

    /*

    1.NSDate 时间格式

    2.NSTimeInterval 时间间隔 基本单位 秒

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

    时间戳:

    日期格式如下:

    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

    4.比较时间:

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

    (2)获取较早的日期 earlierDate

    (3)获取较晚的时间 lateDate

    (4)获取两个日期间隔多少秒

    */

    @implementation ViewController

    - (void)viewDidLoad {

    [super viewDidLoad];

    allContent = [NSMutableArray array];

    //通过通知检测键盘显示的状态

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

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

    //需要输入框和其上面的按钮同时上移

    bgView = [[UIView alloc]initWithFrame.:CGRectMake(0, CGRectGetHeight(self.view.frame)-40, CGRectGetWidth(self.view.frame), 40)];

    bgView.backgroundColor = [UIColor grayColor];

    [self.view addSubview:bgView];

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

    inputView.layer.cornerRadius = 5;

    inputView.delegate = self;

    [bgView addSubview:inputView];

    UIButton *sendBt = [UIButton buttonWithType:UIButtonTypeCustom];

    sendBt.frame. = CGRectMake(CGRectGetMaxX(inputView.frame)+20, 5, 80, 30);

    [bgView addSubview:sendBt];

    sendBt.backgroundColor = [UIColor cyanColor];

    [sendBt setTitle:@发送 forState:UIControlStateNormal ];

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

    sendBt.tag = 100;

    sendBt.layer.cornerRadius = 5;

    //默认不可以使用按钮,输入内容后才使用

    sendBt.enabled = YES;

    [sendBt addTarget:self action:@selector(senContent:) forControlEvents:UIControlEventTouchUpInside];

    }

    #pragma ------------键盘的状态--------------

    - (void)keyBoard:(NSNotification *)not

    {

    NSDictionary *info = not.userInfo;

    // NSLog(@%@,info);

    keyBoardRect = [info[UIKeyboardFrameEndUserInfoKey]CGRectValue];

    bgView.frame. = CGRectMake(0, CGRectGetMinY(keyBoardRect)-40, CGRectGetWidth(self.view.frame), 40);

    }

    #pragma ------------TextViewDelegate方法--------------

    - (void)textViewDidBeginEditing:(UITextView *)textView

    {

    //开始编辑的时候 允许发送按钮使用

    UIButton *button = (UIButton *)[bgView viewWithTag:100];

    button.enabled = YES;

    }

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

    {

    //通过检测textView输入的内容得到它的内容高度,把内容的高度设置成inputView的高度以及bgView的高度

    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 ------------发送输入的内容--------------

    - (void)senContent:(UIButton *)sender

    {

    [inputView resignFirstResponder];

    NSLog(@%@,inputView.text);

    inputView.text = @;

    NSDate *curDate = [NSDate date];

    NSLog(@%@,[NSString stringWithFormat:@%@,curDate]);

    //时 分 秒

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

    //设置时间格式

    formatter.dateFormat = @y/M/d E a hh: mm :ss;

    //把curDate按照时间格式 转化成字符串

    NSString *time = [formatter stringFromDate:curDate];

    //NSDateFormatter 转换的时间 是设备的时间

    NSLog(@%@,time);

    //获得从1970年到现在的时间间隔(通常是时间戳的 时间间隔)

    NSTimeInterval timeInterval = [curDate timeIntervalSince1970];

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

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

    //把时间戳 转换成 日期

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

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

    //获得较早的日期

    //NSDate *earDate = [date earlierDate:date];

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

    //NSDate dateWithTimeIntervalSinceNow: 计算当前日期到时间间隔日期

    //获得一天的时间间隔

    NSTimeInterval interval = 24*60*60;

    //设置时间格式为 年 月 日

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

    formatter.dateFormat = @yyyy-MM-dd;

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

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

    [allContent addObject:info];

    //指定根据那个key 进行分类

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

    NSMutableArray *soreDescriptorArry = [NSMutableArray arrayWithObjects:&soreDescriptor count:1];

    //根据 描述的数组进行排序

    allContent = [[allContent sortedArrayUsingDescriptors:soreDescriptorArry] mutableCopy];

    sender.enabled = NO;

    NSLog(@%@,allContent);

    }

    - (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

    }

    @end

最新文章