已经是学习的第六天了,马上就要结束学习进行ios的开发,今天只进行自定义UIView的学习。
这一篇中,名字虽然叫自定义View,但是我觉得不像是自定义view。只是把已有的控件绑定到一起,并没有创造一个从未出现过的新东西。把一张图片和,一段文字绑定在一起,如果你有兴趣,可以尝试更多控件的绑定,写更复杂的逻辑去控制各各控件的显示。和以前一样,先看效果:
这个功能很简单,就是一个UIImageView和一个UILabel的组合,把它们创建的过程放在了自定义UIView的初始化函数中,并对它们的样式,大小,位置进行控制。这个页面上有3个UI控件,每个控件又包含2个UI控件。
先创建一个类,该类继承UIView在这个类中完成位置大小样式的控制即可,看代码:
//
// MyFirstView.h
// MyUIView
//
// Created by Moluth on 17/4/12.
// Copyright (c) 2017年 Moluth. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface MyFirstView : UIView{
@public UIImageView *image;
@public UILabel *name;
}
-(void)setImage : (UIImage*)pic andName: (NSString*)pname;
@end
//
// MyFirstView.m
// MyUIView
//
// Created by Moluth on 17/4/12.
// Copyright (c) 2017年 Moluth. All rights reserved.
//
#import "MyFirstView.h"
@implementation MyFirstView
- (id)initWithFrame : (CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
float imageHeight=frame.size.width/3.0f*4.0f;
//对子控件进行初始化
image=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, imageHeight)];
name=[[UILabel alloc] initWithFrame:CGRectMake(0, imageHeight/5.0*6.0, frame.size.width, imageHeight/5.0)];
//设置子控件参数
[image setContentMode:UIViewContentModeScaleAspectFit];
image.backgroundColor=[UIColor purpleColor];
image.layer.cornerRadius=5;
image.layer.masksToBounds=YES;
name.backgroundColor=[UIColor orangeColor];
name.textColor=[UIColor whiteColor];
name.textAlignment=NSTextAlignmentCenter;
name.font=[UIFont systemFontOfSize:imageHeight/20.0f*3.0f];
name.layer.cornerRadius=5;
name.layer.masksToBounds=YES;
name.text=@"欧阳疯";
//添加子控件
[self addSubview:image];
[self addSubview:name];
}
return self;
}
-(void)setImage : (UIImage*)pic andName: (NSString*)pname{
[image setImage:pic];
name.text=pname;
}
//当控件的大小改变时,子控件跟着改变
-(void)layoutSubviews{
CGRect frame=self.frame;
float imageHeight=frame.size.width/3.0f*4.0f;
image.frame=CGRectMake(0, 0, frame.size.width, imageHeight);
name.frame=CGRectMake(0, imageHeight/5.0*6.0, frame.size.width, imageHeight/5.0);
name.font=[UIFont systemFontOfSize:imageHeight/20.0f*3.0f];
}
@end
然后,这就是一个UI控件了,使用方法和我们常用的UILabel的用法并没有什么区别:
//
// ViewController.m
// MyUIView
//
// Created by Moluth on 17/4/12.
// Copyright (c) 2017年 Moluth. All rights reserved.
//
#import "ViewController.h"
#import "MyFirstView.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
MyFirstView *myFirstView=[[MyFirstView alloc] initWithFrame:CGRectMake(10, 30, 60, 100)];
[myFirstView setImage:[UIImage imageNamed:@"ZhouEnlai1.jpg"] andName:@"周恩来1"];
[self.view addSubview:myFirstView];
//myFirstView.backgroundColor=[UIColor blueColor];
MyFirstView *myFirstView1=[[MyFirstView alloc] initWithFrame:CGRectMake(80, 30, 70, 140)];
[myFirstView1 setImage:[UIImage imageNamed:@"ZhouEnlai1.jpg"] andName:@"周恩来2"];
[self.view addSubview:myFirstView1];
//myFirstView1.backgroundColor=[UIColor brownColor];
MyFirstView *myFirstView2=[[MyFirstView alloc] initWithFrame:CGRectMake(80, 30, 200, 280)];
myFirstView2.center=self.view.center;
[myFirstView2 setImage:[UIImage imageNamed:@"ZhouEnlai1.jpg"] andName:@"周恩来3"];
[self.view addSubview:myFirstView2];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end