iosUILabel加删除线

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

首先生成一个继承与UILabel的类SFPStrikeThroughAndUnderLineLabel

一,在.h文件里

@interfaceSFPStrikeThroughAndUnderLineLabel :UILabel

{

BOOL_isWithStrikeThrough;

}

@PRoperty(nonatomic,assign)BOOLisWithStrikeThrough;//控制是否显示删除线

@property(nonatomic,assign)CGFloatstrikeThroughLineWidth;//设置删除线的宽度

@property(nonatomic,retain)NSArray*strikeThroughRGBAlphaArray;//设置删除线的颜色,数组里面的元素分别是RGB以及alpha值也就是说该数组有四个元素

@end

二,在.m文件里

#import"SFPStrikeThroughAndUnderLineLabel.h"

@implementationSFPStrikeThroughAndUnderLineLabel

@synthesizeisWithStrikeThrough =_isWithStrikeThrough;

@synthesizestrikeThroughRGBAlphaArray =_strikeThroughRGBAlphaArray;

@synthesizestrikeThroughLineWidth =_strikeThroughLineWidth;

- (id)initWithFrame:(CGRect)frame

{

self= [superinitWithFrame:frame];

if(self) {

// Initialization code

self.strikeThroughRGBAlphaArray= [[NSArrayalloc]initWithObjects:@"0",@"0",@"0",@"1",nil];//默认删除线的颜色是黑色切实不透明的,给个默认值,建立对象时该属性可以不必赋值

self.strikeThroughLineWidth=1;//默认删除线的宽度是1pix

}

returnself;

}

// Only override drawRect: if you perform custom drawing.

// An empty implementation adversely affects performance during animation.

- (void)drawRect:(CGRect)rect

{

// Drawing code

if(self.isWithStrikeThrough)//显示删除线

{

CGContextRefc =UIGraphicsGetCurrentContext();

CGFloatcolor[4] = {[[self.strikeThroughRGBAlphaArrayobjectAtIndex:0]floatValue], [[self.strikeThroughRGBAlphaArrayobjectAtIndex:1]floatValue], [[self.strikeThroughRGBAlphaArrayobjectAtIndex:2]floatValue], [[self.strikeThroughRGBAlphaArrayobjectAtIndex:3]floatValue]};

CGContextSetStrokeColor(c, color);

CGContextSetLineWidth(c,self.strikeThroughLineWidth);

CGContextBeginPath(c);

CGFloathalfWayUp = (self.bounds.size.height-self.bounds.origin.y) /2.0;

CGContextMoveToPoint(c,self.bounds.origin.x, halfWayUp );

CGContextAddLineToPoint(c,self.bounds.origin.x+self.bounds.size.width, halfWayUp);

CGContextStrokePath(c);

}

[superdrawRect:rect];

}

三,使用:添加SFPStrikeThroughAndUnderLineLabel.h文件

SFPStrikeThroughAndUnderLineLabel*oneLabel = [[SFPStrikeThroughAndUnderLineLabelalloc]initWithFrame:CGRectMake(20,20,100,200)];

oneLabel.backgroundColor= [UIColorwhiteColor];

oneLabel.numberOfLines=0;

oneLabel.text=@"朱冬玲,我爱你一万年;朱冬玲,我爱你一万年;朱冬玲,我爱你一万年;朱冬玲,我爱你一万年;朱冬玲,我爱你一万年";

oneLabel.isWithStrikeThrough=YES;

// 下面这两个属性可设可不设值,他们设的都有默认值

// oneLabel.rgbAlphaArray = [NSArray arrayWithObjects:@"255",@"255",@"0",@"1", nil];

// oneLabel.strikeThroughLineWidth = 20;

[self.viewaddSubview:oneLabel];

注意:这是给UILabel整个控件加删除线(一个label只会有一条删除线),而不是给label的text加删除线

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航