//
// ViewController.m
#import "SDAutoLayout.h"
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic, strong) UITextView *textView;
@property (nonatomic, strong) UITextView *textViewBottom;
@property (nonatomic, copy) NSString *string;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
floatmagin =10;
UITextView*text = [selfreturnTextView];
self.textView= text;
text.sd_layout.topSpaceToView(self.view,60).leftSpaceToView(self.view, magin).rightSpaceToView(self.view,100).heightRatioToView(self.view,0.4);
text = [selfreturnTextView];
self.textViewBottom = text;
text.sd_layout.topSpaceToView(self.textView, magin).leftSpaceToView(self.view, magin).rightSpaceToView(self.view, magin).bottomSpaceToView(self.view,50);
UIButton*button = [selfreturnButton:@"提交"tag:1];
button.sd_layout.rightSpaceToView(self.view, magin).topSpaceToView(self.textView, -180).widthIs(80).heightIs(36);
UIButton*button2 = [selfreturnButton:@"复制"tag:2];
button2.sd_layout.rightEqualToView(button).topSpaceToView(button,10).widthIs(80).heightIs(36);
UIButton*button3 = [selfreturnButton:@"清空"tag:3];
button3.sd_layout.rightEqualToView(button).topSpaceToView(button2,10).widthIs(80).heightIs(36);
UIButton*button4 = [selfreturnButton:@"返回"tag:4];
button4.sd_layout.rightEqualToView(button).topSpaceToView(button3,10).widthIs(80).heightIs(36);
}
- (void)touchesBegan:(NSSet *)toucheswithEvent:(UIEvent*)event
{
[self.textView resignFirstResponder];
[self.textViewBottom resignFirstResponder];
}
- (void)buttonClick:(UIButton*)sender
{
switch(sender.tag) {
case1:
{
[self.textView resignFirstResponder];
NSString*str =self.textView.text;
NSArray *array = [str componentsSeparatedByString:@"\n"];
str =@"";
for(inti =0; i < array.count-1; i++) {
floatnum1 = [array[i]floatValue];
floatnum2 = [array[i +1]floatValue];
floatvalue =fabsf(num1 - num2) / num1;
if(num1 < num2 && str.length==0) {//先升
str =@"================";
}
if(num1 >1000|| num2 >1000) {
str = [strstringByAppendingFormat:@"%.0f %.0f=%.2f",num1,num2,value];
}else{
str = [strstringByAppendingFormat:@"%.2f %.2f=%.2f",num1,num2,value];
}
if(num1 < num2) {
str = [strstringByAppendingString:@"\n"];
}else{
str = [strstringByAppendingString:@" "];
}
}
self.textViewBottom.text= str;
}
break;
case2:
{
UIPasteboard *past = [UIPasteboard generalPasteboard];
past.string=self.textViewBottom.text;
}
break;
case3:
{
self.string=self.textView.text;
self.textView.text=@"";
}
break;
case4:
{
self.textView.text=self.string;
}
break;
default:
break;
}
}
- (UITextView *)returnTextView
{
UITextView *text = [UITextView new];
text.sd_cornerRadius =@10;
text.font = [UIFont systemFontOfSize:18];
text.backgroundColor = [UIColor groupTableViewBackgroundColor];
text.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
[self.viewaddSubview:text];
returntext;
}
- (UIButton *)returnButton:(NSString *)title tag:(NSInteger)tag
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.tag= tag;
button.titleLabel.font = [UIFont systemFontOfSize:16];
button.sd_cornerRadiusFromHeightRatio =@0.5;;
button.backgroundColor = [UIColor cyanColor];
[buttonsetTitle:title forState:UIControlStateNormal];
[buttonaddTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
[self.viewaddSubview:button];
returnbutton;
}
@end