iOS开发-UILabel画删除线

在游戏的开发中,一般要用到显示道具或者是筹码的价格,为了显示优惠幅度和吸引玩家付费,一般会强调原价与现价的优惠幅度,原价上面画上一条删除线。下面是在iOS开发时用到的代码,可以作为参考实现。

===========================================================================

下面是UILabelStrikeThrough.h文件

/*

 *  用于在UILabel上画删除线

 */

#import<Foundation/Foundation.h>


@interface UILabelStrikeThrough :UILabel

{

    BOOL isWithStrikeThrough;

}


@property (nonatomic,assign) BOOL isWithStrikeThrough;

@end

===========================================================================

下面是UILabelStrikeThrough.m文件

#import"UILabelStrikeThrough.h"


@implementation UILabelStrikeThrough


@synthesize isWithStrikeThrough;


- (void)drawRect:(CGRect)rect

{

   if (isWithStrikeThrough)

    {

       CGContextRef c = UIGraphicsGetCurrentContext();

        

       CGFloat red[4] = {1.0f,0.0f, 0.0f,0.8f}; //红色

       //CGFloat black[4] = {0.0f, 0.0f, 0.0f, 0.5f};//黑色

       CGContextSetStrokeColor(c, red);

       CGContextSetLineWidth(c, 2);

       CGContextBeginPath(c);

       //画直线

       //CGFloat halfWayUp = rect.size.height/2 + rect.origin.y;

       //CGContextMoveToPoint(c, rect.origin.x, halfWayUp );//开始点

       //CGContextAddLineToPoint(c, rect.origin.x + rect.size.width, halfWayUp);//结束点

       //画斜线

       CGContextMoveToPoint(c, rect.origin.x, rect.origin.y+5 );

       CGContextAddLineToPoint(c, (rect.origin.x + rect.size.width)*0.5, rect.origin.y+rect.size.height-5); //斜线

       CGContextStrokePath(c);

    }

    

    [superdrawRect:rect];

}


- (void)dealloc

{

    [superdealloc];

}


@end


===========================================================================

调用代码:


    UILabelStrikeThrough *originalPrice=[[[UILabelStrikeThroughalloc] initWithFrame:CGRectMake(coinPic.frame.origin.x+coinPic.frame.size.width+5,0, 120,15)]autorelease];

    originalPrice.isWithStrikeThrough=TRUE;

发布了18 篇原创文章 · 获赞 1 · 访问量 6万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章