ios UILabel加刪除線

來源:互聯網
上載者:User

首先產生一個繼承與UILabel的類SFPStrikeThroughAndUnderLineLabel

一,在.h檔案裡

@interface SFPStrikeThroughAndUnderLineLabel : UILabel

{

    BOOL _isWithStrikeThrough;   

}

 @property (nonatomic, assign) BOOL isWithStrikeThrough; //控制是否顯示刪除線

@property (nonatomic, assign) CGFloat strikeThroughLineWidth;//設定刪除線的寬度

@property (nonatomic, retain) NSArray *strikeThroughRGBAlphaArray;//設定刪除線的顏色,數組裡面的元素分別是RGB以及alpha值也就是說該數組有四個元素

#import "SFPStrikeThroughAndUnderLineLabel.h"

 

@implementation SFPStrikeThroughAndUnderLineLabel

@synthesize isWithStrikeThrough = _isWithStrikeThrough;

@synthesize strikeThroughRGBAlphaArray = _strikeThroughRGBAlphaArray;

@synthesize strikeThroughLineWidth = _strikeThroughLineWidth;

 

- (id)initWithFrame:(CGRect)frame

{

    self = [super initWithFrame: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)//顯示刪除線

    {

        CGContextRef c = UIGraphicsGetCurrentContext();

        

        CGFloat color[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);

        CGFloat halfWayUp = (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);

    }

    [super drawRect: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.view addSubview:oneLabel];

注意:這是給UILabel整個控制項加刪除線(一個label只會有一條刪除線),而不是給label的text加刪除線 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.