iOS-UITextView-文本输入视图的使用
阅读原文时间:2023年07月09日阅读:3

#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];

//须要输入框和其上面的button同一时候上移

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;

//默认不能够使用button,输入内容后才使用

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

{

//開始编辑的时候
同意发送button使用

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

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章