纯代码实现人民币兑换美元

//

//  ViewController.m

//  01-exchangeRate

//

//  Created by 王军波 on 16/4/10.

//  Copyright © 2016年 王军波. All rights reserved.

//

 

 

#define kScreenW [UIScreen mainScreen].bounds.size.width

#define kScreenH [UIScreen mainScreen].bounds.size.height

 

 

#import "ViewController.h"

 

@interface ViewController ()

@property (nonatomic,weak) UITextField *number;

@property (nonatomic,weak) UILabel *result;

@end

 

@implementation ViewController

 

//view加载完后会自动执行

- (void)viewDidLoad {

    [super viewDidLoad];

    [self setupUI];

    // Do any additional setup after loading the view, typically from a nib.

}

#pragma mark - 分割线

//设置界面

- (void)setupUI{

    //1.设置背景

    UIImageView *bottomBackgroundPicture = [[UIImageView alloc]init];

    UIImageView *topBackgroundPicture = [[UIImageView alloc]init];

    //1.1上下覆盖灰色背景

    //1.1.1灰色背景的尺寸大小

    bottomBackgroundPicture.frame = CGRectMake(0, 290, self.view.frame.size.width, self.view.frame.size.height);

    topBackgroundPicture.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height*0.03);

    //1.1.2灰色背景的颜色设置

    bottomBackgroundPicture.backgroundColor = [UIColor grayColor];

    topBackgroundPicture.backgroundColor = [UIColor grayColor];

    //2.添加到根视图

    [self.view addSubview:bottomBackgroundPicture];

    [self.view addSubview:topBackgroundPicture];

    

    //3.设置国旗

    //3.1两个相框

    UIImageView *chinaPicture = [[UIImageView alloc]initWithFrame:CGRectMake(10, 30, 180, 130)];

    UIImageView *americanPicture = [[UIImageView alloc]initWithFrame:CGRectMake(10,150 , 180, 130)];

    //3.2将照片放到相框中

    chinaPicture.image = [UIImage imageNamed:@"China"];

    americanPicture.image = [UIImage imageNamed:@"USA"];

    //添加到根视图

    [self.view addSubview:chinaPicture];

    [self.view addSubview:americanPicture];

    

    //4.输入框

    UITextField *numText = [[UITextField alloc]initWithFrame:CGRectMake(250, 50, 160, 80)];

    //框内的光标在最右边

    numText.textAlignment=NSTextAlignmentRight;

    //给框设置一个纯数字键盘

    numText.keyboardType = UIKeyboardTypeNumberPad;

    

    //[numText sizeToFit];

    //4.1输入框的背景颜色

    //numText.backgroundColor = [UIColor grayColor];

    //添加到根视图

    [self.view addSubview:numText];

    

    //赋给全局变量 用以下面换算方法的计算使用

    _number = numText;

    

    //创建3个Label

    UILabel *num1Label = [[UILabel alloc]initWithFrame:CGRectMake(250, 200, 150, 30)];

    UILabel *num2Label = [[UILabel alloc]initWithFrame:CGRectMake(60, 300, 300, 30)];

    UILabel *num3Label = [[UILabel alloc]initWithFrame:CGRectMake(45, 330, 350, 30)];

    

    //没啥用 测试

    //num1Label.highlightedTextColor = [UIColor redColor];

    num1Label.text = @"这里显示结果";

    num2Label.text = @"1美元 = 6.4761人民币,更新于2016-4-6";

    num3Label.text = @"数据仅供参考,交易时以银行柜台成交价为准";

    //num1Label.backgroundColor = [UIColor grayColor];

    //num2Label.backgroundColor = [UIColor redColor];

    // 用以下面 计算使用

    _result = num1Label;

    //添加到根视图

    [self.view addSubview:num1Label];

    [self.view addSubview:num2Label];

    [self.view addSubview:num3Label];

        

    

    //5.按钮

    UIButton *calculate = [[UIButton alloc]initWithFrame:CGRectMake(80, 370, 250, 50)];

    //5.1默认状态字的颜色

    [calculate setTitle:@"计算" forState:UIControlStateNormal];

    [calculate setTitleColor:[UIColor colorWithRed:0 green:0 blue:90 alpha:5] forState:UIControlStateNormal];

    //5.2高亮状态

    [calculate setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];

    //5.3底色

    calculate.backgroundColor = [UIColor colorWithRed:0 green:30 blue:70 alpha:20];

 

    [self.view addSubview:calculate];

    //6.触发事件的实现

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

    

}

//7.汇率换算计算的实现

- (void)calculate{

    double numRate = 6.4761;

    float rmb = _number.text.floatValue;

    float dollar = rmb/numRate;

    //dollar是float类型,将dollar转成string类型,包装成一个NSNumber的对象,对象的description方法以string形式打出来

    _result.text = @(dollar).description;

    //或者格式化获取string

    //_result.text = [NSString stringWithFormat:@"%f",dollar];

}

@end

 

posted on 2016-04-10 17:36  程序员是累不死的  阅读(1203)  评论(0编辑  收藏  举报