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

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

一、imageView

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

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

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

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

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

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

 @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]; }

看看效果:

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

二、Label

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

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

1、打开MyLabel.h文件,

设置我们在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文件,

 @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];
}

效果图:

三、自定义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;

同样,引入头文件,

在- (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];

效果图:

Button点击动作响应:

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

效果图:

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

  1. ios基础篇(十四)——UITableView(二)属性及基本用法

    上一篇说了UITableView的重用机制,让我们对UITableView有了简单了解,下面说说UITableView的属性及常见方法. 一.属性 1.frame:设置控件的尺寸和大小 2.backg ...

  2. ios基础篇(十二)——UINavgationController的使用(三)ToolBar

    UIToolBar存在于UINavigationController导航栏控制器中,而且默认被隐藏:设置UINavigationController的toolbarHidden属性可显示UIToolB ...

  3. ios基础篇(十八)——Delegate 、NSNotification 和 KVO用法及其区别

    一.Delegate Delegate本质是一种程序设计模型,iOS中使用Delegate主要用于两个页面之间的数据传递.iphone中常用@protocol和delegate的机制来实现接口的功能. ...

  4. ios基础篇(十六)——UIWebView的基本使用

    UIWebView是内置的浏览器控件,可以用它来浏览网页.打开文档等.UIWebView是一个混合体,具体的功能控件内置的,实现一些基本的功能.UIWebView可以查看Html网页,pdf文件,do ...

  5. iOS基础篇(十五)——UIScrollView的基本用法

    滚动视图(UIScrollView)通常用于显示内容尺寸大于屏幕尺寸的视图. 一.基本属性 1.CGSize contentSize :设置UIScrollView的滚动范围 2.CGPoint co ...

  6. Py修行路 python基础 (二十四)socket编程

    socket编程 一.客户端/服务端架构 客户端/服务端架构 即C/S架构,包括:1.硬件C/S架构,2.软件C/S架构. 互联网中处处都是C/S架构,学习socket 就是为了完成C/S架构的开发. ...

  7. ioS基础篇(十九)——UIResponder简析

    UIResponder类定义了对象相应和控制事件的接口,他是UIApplication.UIView的超类,这类的实例通常被称为应答对象. 一.Responder对象 在iOS系统中,能够响应并处理事 ...

  8. JavaEE基础(二十四)/多线程

    1.多线程(多线程的引入) 1.什么是线程 线程是程序执行的一条路径, 一个进程中可以包含多条线程 多线程并发执行可以提高程序的效率, 可以同时完成多项工作 2.多线程的应用场景 红蜘蛛同时共享屏幕给 ...

  9. ios基础篇(十)——UINavgationController的使用(一)UIBarButtonItem的添加

    UINavigationController又被成为导航控制器,继承自UIViewController,以栈的方式管理所控制的视图控制器,下面就详细说一下UINavigationController的 ...

随机推荐

  1. FileItem类的常用方法

    FileItem类的常用方法: 1.  boolean isFormField() isFormField方法用于判断FileItem类对象封装的数据是一个普通文本表单字段,还是一个文件表单字段,如果 ...

  2. 20161011001 treeView 递归

    protected void FillTree()        {            H_data H_data = new H_data(); H_data.sql_text1 = " ...

  3. 修改bootstrap modal模态框的宽度

    原文链接:http://blog.csdn.net/wuhawang/article/details/52252912 修改模态框的宽度很简单,修改width属性就可以了 但是要注意的一点是,修改的不 ...

  4. shell技巧

    如何实现Shell脚本以DEAMON的方式运行,即实现Shell版的Fork if [ "$1" != 'background' ] ; then scriptdir=$(cd & ...

  5. win7和u盘redhat7.1双系统安装总结

    最近win7系统越用越卡,又没钱买mac只能想办法装以下linux系统,听说redhat服务器用的比较多,就想尝试一下装一个redhat.当然,和所有人一样,搜索了很多资料.我选择装双系统,因为要抛弃 ...

  6. jquery.validate使用 - 常用验证脚本

    一些常用的验证脚本 不会写js了,只能从网上找一些常用的验证脚本. // 手机号码验证jQuery.validator.addMethod("mobile", function(v ...

  7. 双模蓝牙CC2564调试笔记

    1.CC256X Testing Guide  官方文档WIKI地址:http://processors.wiki.ti.com/index.php/CC256x_Testing_Guide#Devi ...

  8. js设计模式总结-迭代器模式

    迭代器模式 要解决的问题 迭代器要解决的问题很简单很单纯,就是进行遍历操作. 实现原理 基本所有语言都实现了迭代器,javascript也不例外,如Array.prototype.forEach,fo ...

  9. T-Sql 递归查询(给定节点查所有父节点、所有子节点的方法)

    -- 查找所有父节点with tab as( select Type_Id,ParentId,Type_Name from Sys_ParamType_V2_0 where Type_Id=316-- ...

  10. WCF入门简单教程(图文) VS2010版

    在这个例子中我们将使用VS 2010 创建一个WCF服务,其中会了解 [DataContract] [ServiceContract] 等特性.  内置的 WCFSVCHost ,并使用“WCF测试客 ...