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萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章