2014资料整理--User Defined Runtime Attributes

来源:互联网 发布:如何将数据导入matlab 编辑:程序博客网 时间:2024/05/18 13:45

最近在xocde中发现了一个“User Defined Runtime Attributes”属性,之前没有用过,写下做个记录。如图:



在网上了一些资料感觉也没有说清,先说下我自己的理解:如果想扩展一个类方法,就要创建一个继承类。在需要引用它的xib中只需要设Custom Class,也就是新建的这个类,在“User Defined Runtime Attributes”下增加相应的key 和value就可以实现。之前的写法是在类文件中引用新建的类,在调用它的实现方法,传递相关的参数。现在这个属性可以省去了这一步。

下面是写的一个小例子,多谢朋友们的无私分享。

1、首先要建立一个类:MyImgView继承UIButton

MyImgView.h 内容如下:

#import <UIKit/UIKit.h>

#import <Foundation/Foundation.h>

@interface MyImgView : UIButton

@property (nonatomic,retain)NSNumber *cornerRadius;

@end

MyImgView.m 内容如下:

#import "MyImgView.h"

#import <QuartzCore/CALayer.h> 

@implementation MyImgView

@synthesize cornerRadius;


- (id)initWithFrame:(CGRect)frame

{

    self = [super initWithFrame:frame];

    if (self) {

        // Initialization code

    }

    return self;

}

-(void)drawRect:(CGRect)rect

{

    [[selflayer]setCornerRadius:[cornerRadiusfloatValue]];

    [[selflayer]setMasksToBounds:YES];

    [[selflayer]setBackgroundColor:[selfgetColorFromRed:168.0Green:255.0Blue:168.0Alpha:1]];

    NSLog(@"成功");

}


-(CGColorRef) getColorFromRed:(int)red Green:(int)green Blue:(int)blue Alpha:(int)alpha

{

    CGFloat r = (CGFloat) red/255.0;

    CGFloat g = (CGFloat) green/255.0;

    CGFloat b = (CGFloat) blue/255.0;

    CGFloat a = (CGFloat) alpha/255.0;

    CGFloat components[4] = {r,g,b,a};

    CGColorSpaceRef colorSpace =CGColorSpaceCreateDeviceRGB();

    

CGColorRef color = (CGColorRef)[(id)CGColorCreate(colorSpace, components)autorelease];

    CGColorSpaceRelease(colorSpace);

    return color;

}


@end


2.在ViewController.xib中拖放一个uibutton,并设置一个图片。

3、设置此按钮的“Custom Class”和“User Defined Runtime Attributes”,如下图:


运行就可以可以看到更改后的效果了。

使用时应当注意定义的变量应和xib中设置的key一样

有不对地方,希望大家帮忙指出来,3Q。



0 0
原创粉丝点击