Ios UILabel plus strikethrough

Source: Internet
Author: User

First, generate a class that inherits from UILabel SFPStrikeThroughAndUnderLineLabel

1. In the. h file

@ Interface SFPStrikeThroughAndUnderLineLabel: UILabel

{

BOOL _ isWithStrikeThrough;

}

@ Property (nonatomic, assign) BOOL isWithStrikeThrough; // controls whether to display strikethrough.

@ Property (nonatomic, assign) CGFloat strikeThroughLineWidth; // you can specify the strikethrough width.

@ Property (nonatomic, retain) NSArray * strikeThroughRGBAlphaArray; // sets the strikethrough color. The elements in the array are RGB and alpha values, which means that the array has four elements.

# 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]; // by default, the color of the strikethrough is black and non-transparent. a default value is provided. This attribute does not need to be assigned a value when an object is created.

Self. strikeThroughLineWidth = 1; // The default strikethrough width is 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) // display strikethrough

{

CGContextRef c = UIGraphicsGetCurrentContext ();

CGFloat color [4] = {[self. strikethroughrgbalph?rayobjectatindex: 0] floatValue], [[self. strikethroughrgbalph?rayobjectatindex: 1] floatValue], [[self. strikethroughrgbalph?rayobjectatindex: 2] floatValue], [[self. strikethroughrgbalph?rayobjectatindex: 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];

}

3. Use: Add SFPStrikeThroughAndUnderLineLabel. h file

SFPStrikeThroughAndUnderLineLabel * oneLabel = [[SFPStrikeThroughAndUnderLineLabelalloc] initWithFrame: CGRectMake (20, 20,100,200)];

OneLabel. backgroundColor = [UIColorwhiteColor];

OneLabel. numberOfLines = 0;

OneLabel. text = @ "Zhu Dongling, I love you for 10 thousand years; Zhu Dongling, I love you for 10 thousand years; Zhu Dongling, I love you for 10 thousand years; Zhu Dongling, I love you for 10 thousand years; Zhu Dongling, I love you for 10 thousand years ";

OneLabel. isWithStrikeThrough = YES;

// The following two attributes can be set with no value, and all of them have default values.

// OneLabel. rgbAlphaArray = [NSArray arrayWithObjects: @ "255", @ "255", @ "0", @ "1", nil];

// OneLabel. strikeThroughLineWidth = 20;

[Self. view addSubview: oneLabel];

Note: This is to delete the entire UILabel control (a label only has one strikethrough), rather than adding strikethrough to the label text.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.