首页 技术 正文
技术 2022年11月20日
0 收藏 397 点赞 3,428 浏览 5176 个字

这篇文章我们主要来拿官方的控件来研究一下,我们来仿照官方的控件,自己来实现它提供的控件;

首先来看看基本的图片与文字的绘制,很简单。

一、imageView

所有的视图都是继承自UIView,所以我们的ImageView也是继承自UIView,我们自己写的用My开头,以便于区分。

1、对于ImageView,我们需要绘制,需要提供图片资源,所以在我们的头文件里我们这样定义:

ios基础篇(二十四)—— 文字、图片的绘制及其自定义Button

2、回到MyImageView.m文件里,找到绘制函数:

ios基础篇(二十四)—— 文字、图片的绘制及其自定义Button

嗯,没错这样就写完了。。。

接下来回到ViewController.m里试试我们自己写的控件吧!

3、在ViewController.m中引入我们刚才自定义的View,

ios基础篇(二十四)—— 文字、图片的绘制及其自定义Button

 @interface ViewController (){     MyImageView *imageView; } @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
imageView = [[MyImageView alloc] initWithFrame:(CGRect){,,,}];
imageView.image = [UIImage imageNamed:@"u=1940733206,3299197276&fm=21&gp=0"];
[self.view addSubview:imageView]; }

看看效果:

ios基础篇(二十四)—— 文字、图片的绘制及其自定义Button

还不错吧,是不是和苹果官方提供的控件一样好用。。

二、Label

Label的绘制会稍微复杂一点,

首先还是创建我们自己的View(MyLabel)。

1、打开MyLabel.h文件,

ios基础篇(二十四)—— 文字、图片的绘制及其自定义Button

设置我们在UILabel控件中常用的属性,这里只写几个举例说明一下,具体用到时候根据需要设置。

2、回到MyLabel.m文件,

 #import "MyLabel.h" @implementation MyLabel // Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineBreakMode = NSLineBreakByCharWrapping;
paragraphStyle.alignment = NSTextAlignmentLeft; NSDictionary *dic = @{NSParagraphStyleAttributeName:paragraphStyle,
NSForegroundColorAttributeName:_textColor,//设置字体颜色
NSBackgroundColorAttributeName:_backgroundColor,//设置背景色
NSFontAttributeName:_font,//设置字体
NSStrokeWidthAttributeName:@,//设置描边宽度,这样就能使文字空心
NSStrokeColorAttributeName:[UIColor greenColor],//设置文字描边颜色
};
[_text drawInRect:(CGRect){,,,} withAttributes:dic]; }

3、回到ViewController.m文件,

引入MyLabel.h文件,

ios基础篇(二十四)—— 文字、图片的绘制及其自定义Button

 @interface ViewController (){     MyImageView *imageView;
MyLabel *label;
} @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
imageView = [[MyImageView alloc] initWithFrame:(CGRect){,,,}];
imageView.image = [UIImage imageNamed:@"u=1940733206,3299197276&fm=21&gp=0"];
[self.view addSubview:imageView]; label = [[MyLabel alloc] initWithFrame:(CGRect){,,,}];
label.text = @"Hello,World!";
label.backgroundColor = [UIColor whiteColor];
label.textColor = [UIColor orangeColor];
label.font = [UIFont systemFontOfSize:];
[self.view addSubview:label];
}

效果图:

ios基础篇(二十四)—— 文字、图片的绘制及其自定义Button

三、自定义Button

首先新建View(MyButton),继承自UIView,为什么不直接继承自UIControl?因为我们要自己添加手势!

1、打开MyButton.h文件,添加方法

 #import <UIKit/UIKit.h> @interface MyButton : UIView - (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;//添加手势
- (void)setImage:(UIImage*)image forState:(UIControlState)state;//设置button图片
- (void)setTitle:(NSString*)title forState:(UIControlState)state;//设置文字 @end

2、打开MyButton.m文件,

声明自定义属性,及其初始化方法,

 //
// MyButton.m
// Draw
//
// Created by Oran Wu on 15-12-30.
// Copyright (c) 2015年 Xinxin. All rights reserved.
// #import "MyButton.h" @interface MyButton (){ UIControlEvents controlEvent; UIImage *_buttonImage;
UIControlState controlState; NSString *_buttonTitle; UIBezierPath *buttonPath;
}
@property(nonatomic,weak)id targate;
@property(nonatomic,assign)SEL buttonAction; @end @implementation MyButton - (id)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor whiteColor];
}
return self;
} - (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents{ self.targate = target;
self.buttonAction = action;
controlEvent = controlEvents; } - (void)setImage:(UIImage *)image forState:(UIControlState)state{
_buttonImage = image;
controlState = state;
[self setNeedsDisplay];
} - (void)setTitle:(NSString *)title forState:(UIControlState)state{
_buttonTitle = title;
controlState = state;
[self setNeedsDisplay];
}

3、绘制Button,添加点击事件;

 - (void)drawRect:(CGRect)rect {
// Drawing code
UIColor *color = [UIColor colorWithRed:0.3 green:0.7 blue:0.6 alpha:0.5];
[color set]; buttonPath = [UIBezierPath bezierPathWithRoundedRect:(CGRect){,,,} cornerRadius:];
buttonPath.lineWidth = ;
[buttonPath fill]; //设置图片
[_buttonImage drawInRect:(CGRect){,,,}]; //设置文字
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineBreakMode = NSLineBreakByCharWrapping;
paragraphStyle.alignment = NSTextAlignmentCenter; NSDictionary *dic = @{NSParagraphStyleAttributeName:paragraphStyle,
NSForegroundColorAttributeName:[UIColor redColor],//设置字体颜色
NSBackgroundColorAttributeName:[UIColor clearColor],//设置背景色
NSFontAttributeName:[UIFont systemFontOfSize:],//设置字体
NSStrokeWidthAttributeName:@,//设置描边宽度,这样就能使文字空心
NSStrokeColorAttributeName:[UIColor purpleColor],//设置文字描边颜色
};
[_buttonTitle drawInRect:(CGRect){,,,} withAttributes:dic]; } //开始触摸
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
//按需求设置点击状态
} //结束触摸
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ if (controlEvent==UIControlEventTouchUpInside) { [self.targate performSelector:self.buttonAction withObject:self]; } }

4、返回ViewController.m文件试试我们自定义的Button;

同样,引入头文件,

ios基础篇(二十四)—— 文字、图片的绘制及其自定义Button

在- (void)viewDidLoad;方法中写入以下代码(这里设置了Button的背景颜色,就没有设置图片,如果有需要把文字加在图片上面也是可以的);

     button = [[MyButton alloc] initWithFrame:(CGRect){,,,}];
[button setTitle:@"myButton" forState:UIControlStateNormal];
//[button setImage:[UIImage imageNamed:@"u=38807319,2604887842&fm=15&gp=0"] forState:UIControlStateNormal];
[self.view addSubview:button];
[button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];

效果图:

ios基础篇(二十四)—— 文字、图片的绘制及其自定义Button

Button点击动作响应:

 - (void)buttonAction{
//设置改变文字
[button setTitle:@"Change" forState:UIControlStateNormal]; }

效果图:

ios基础篇(二十四)—— 文字、图片的绘制及其自定义Button

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,000
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,512
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,358
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,141
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,771
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,849