iOS开发-UILabel画删除线

本文介绍了一种在iOS开发中为UILabel添加删除线效果的方法,通过自定义UILabel子类来实现文字上的斜线效果,适用于展示优惠价格等场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在游戏的开发中,一般要用到显示道具或者是筹码的价格,为了显示优惠幅度和吸引玩家付费,一般会强调原价与现价的优惠幅度,原价上面画上一条删除线。下面是在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;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值