码迷,mamicode.com
首页 > 移动开发 > 详细

ios怎么实现带删除线的label

时间:2015-04-28 17:37:09      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:

1.自己新建一个类名字为StrikeLabel,是UILabel的子类;
2.在StrikeLabel.h里@property(nonatomic)BOOL strikeThroughEnabled;
在StrikeLabel.m里
- (void)drawRect:(CGRect)rect{
[super drawTextInRect:rect];
CGSize textSize = [[self text] sizeWithFont:[self font]];
CGFloat strikeWidth = textSize.width;
CGRect lineRect;
if ([self textAlignment] == UITextAlignmentRight) {
lineRect = CGRectMake(rect.size.width - strikeWidth, rect.size.height/2, strikeWidth, 1);
} else if ([self textAlignment] == UITextAlignmentCenter) {
lineRect = CGRectMake(rect.size.width/2 - strikeWidth/2, rect.size.height/2, strikeWidth, 1);
} else {
lineRect = CGRectMake(0, rect.size.height/2, strikeWidth, 1);
}
if (_strikeThroughEnabled) {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextFillRect(context, lineRect);
}}
3.在需要声明label的时候
StrikeLabel *label=[[StrikeLabel alloc]initWithFrame:CGRectMake(100, 100, 200, 100)];
label.text=@"wo ai ni ";
label.strikeThroughEnabled=YES;
[self.view addSubview:label];
搞定了

ios怎么实现带删除线的label

标签:

原文地址:http://www.cnblogs.com/huangzs/p/4463115.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!