Skip to content

Instantly share code, notes, and snippets.

@helloworld116
Created March 11, 2013 02:57
Show Gist options
  • Save helloworld116/5131607 to your computer and use it in GitHub Desktop.
Save helloworld116/5131607 to your computer and use it in GitHub Desktop.
ios:customcontrols:文字中间带条线条
//
// StrikeLabel.h
// UCBook
//
// Created by apple on 13-1-23.
// Copyright (c) 2013年 __MyCompanyName__. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface StrikeLabel : UILabel
@property (nonatomic) BOOL strikeThroughEnabled;
@end
//
// StrikeLabel.m
// UCBook
//
// Created by apple on 13-1-23.
// Copyright (c) 2013年 __MyCompanyName__. All rights reserved.
//
#import "StrikeLabel.h"
@implementation StrikeLabel
@synthesize strikeThroughEnabled=_strikeThroughEnabled;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
[super drawTextInRect:rect];
CGSize textSize = [[self text] sizeWithFont:[self font]];
CGFloat strikeWidth = textSize.width;
CGRect lineRect;
if ([self textAlignment] == UITextAlignmentRight) {
lineRect = CGRectMake(rect.size.width - strikeWidth, rect.size.height/2, strikeWidth, 1);
} else if ([self textAlignment] == UITextAlignmentCenter) {
lineRect = CGRectMake(rect.size.width/2 - strikeWidth/2, rect.size.height/2, strikeWidth, 1);
} else {
lineRect = CGRectMake(0, rect.size.height/2, strikeWidth, 1);
}
if (_strikeThroughEnabled) {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextFillRect(context, lineRect);
}
}
- (void)setStrikeThroughEnabled:(BOOL)strikeThroughEnabled {
_strikeThroughEnabled = strikeThroughEnabled;
NSString *tempText = [self.text copy];
self.text = @"";
self.text = tempText;
}
@end
//范例
//[strikeLabel_instance setStrikeThroughEnabled:YES];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment