6-2 老师我想实现的是这种硬币图片加数字,而不是两段文字
来源:6-2 作业题
SiuNam
2018-10-15 14:54:08

//
// ViewController.m
// 看图猜字游戏
//
// Created by on 2018/10/9.
// Copyright © 2018年 lzn. All rights reserved.
//
#import "ViewController.h"
#import "DiyCoinView.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self addUpView];
//[self addcoinView];
}
-(void)addUpView{
UIView *UpView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 375, 390)];
UpView.backgroundColor = [UIColor colorWithRed:178/255.0 green:34/255.0 blue:34/255.0 alpha:1];
[self.view addSubview:UpView];
//创建自定义的view
DiyCoinView *customView =[[DiyCoinView alloc]init];
//设置数据
CustomModel *model = [CustomModel modelWithName:@"hosea_zhou" icon:@"1"];
customView.model =model;
//设置frame
customView.frame = CGRectMake(100, 100, 100, 100);
//添加子控件
[self.view addSubview:customView];
}
/*
//硬币图片加个数
-(void)addcoinView{
DiyCoinView *coin = [[DiyCoinView alloc]initWithFrame:CGRectMake(244, 20, 131, 30)];
coin.backgroundColor = UIColor.clearColor;
coin.coinImageView.image = [UIImage imageNamed:@"coin.png"];
coin.money.text = @"20000";
[self.view addSubview:coin];
}
*/
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
//
// DiyCoinView.h
// 看图猜字游戏
//
// Created by on 2018/10/12.
// Copyright © 2018年 lzn. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "CustomModel.h"
@interface DiyCoinView : UIView
@property(nonatomic,weak) UIImageView *coinImageView;
@property(nonatomic,weak) UILabel *money;
@property(nonatomic,strong) CustomModel *model;
@end
//
// DiyCoinView.m
// 看图猜字游戏
//
// Created by on 2018/10/12.
// Copyright © 2018年 lzn. All rights reserved.
//
#import "DiyCoinView.h"
@implementation DiyCoinView
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
-(instancetype)initWithFrame:(CGRect)frame{
if(self=[super initWithFrame:frame]){
UIImageView *coinImageView = [[UIImageView alloc]init];
self.coinImageView = coinImageView;
[self addSubview:coinImageView];
UILabel *money = [[UILabel alloc]init];
money.textAlignment = NSTextAlignmentCenter;
self.money = money;
[self addSubview:money];
}
return self;
}
-(void)layoutSubviews{
[super layoutSubviews];
CGFloat coinImageViewX = self.frame.origin.x;
CGFloat coinImageViewY = self.frame.origin.y;
CGFloat coinImageViewW = 30;
CGFloat coinImageViewH = self.frame.size.height;
self.coinImageView.frame = CGRectMake(coinImageViewX, coinImageViewY, coinImageViewW, coinImageViewH);
CGFloat moneyLabelX = self.frame.origin.x+40;
CGFloat moneyLabelY = self.frame.origin.y;
CGFloat moneyLabelW = self.frame.size.width-40;
CGFloat moneyLabelH = self.frame.size.height;
self.money.frame = CGRectMake(moneyLabelX, moneyLabelY, moneyLabelW, moneyLabelH);
}
-(void)setModel:(CustomModel *)model{
_model = model;
self.coinImageView.image =[UIImage imageNamed:model.icon];
self.money.text = model.name;
}
@end
//
// CustomModel.h
// 看图猜字游戏
//
// Created by on 2018/10/15.
// Copyright © 2018年 lzn. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface CustomModel : NSObject
@property(nonatomic,copy) NSString *name;
@property(nonatomic,copy) NSString *icon;
+(instancetype)modelWithName:(NSString *)name icon:(NSString *)icon;
-(instancetype)initWithName:(NSString *)name icon:(NSString *)icon;
@end
//
// CustomModel.m
// 看图猜字游戏
//
// Created by on 2018/10/15.
// Copyright © 2018年 lzn. All rights reserved.
//
#import "CustomModel.h"
@implementation CustomModel
+(instancetype)modelWithName:(NSString *)name icon:(NSString *)icon{
return [[self alloc]initWithName:name icon:icon];
}
-(instancetype)initWithName:(NSString *)name icon:(NSString *)icon{
self = [super init];
if(self){
self.name = name;
self.icon = icon;
}
return self;
}
@end1回答
Tender10
2018-10-15
你的这个题目描述我看的不是太明白,你可以详细再描述一下嘛,之前跟你说的自定义view不是你想实现的需求吗?自定义view也不是两个控件,而是自定义的一个控件,只是里面包含了image属性和label属性,这样就可以通过调用初始化方法直接传入图片以及文字,就可以显示出来了。你表达的硬币图片加文字不是两段文字,我不太懂你的具体需求,可以再详细描述一下哈。