label 下划线加自动换行
功能说明:给Label可以自己加下划线 并且添加点击事件,在网上看了一些Demo大多数都不能满足需求,有的是不可以换行 有的是不支持IOS6.0一下的版本。综合各位网友的Demo整理了一个Demo可以支持换行并且自动 根据Label中的字数和大小计算 Label的高度。
#import
@interface UnderLineLabel : UILabel
{
UIControl *_actionView;
UIColor *_highlightedColor;
}
@property (nonatomic, retain) UIColor *highlightedColor;
@property (nonatomic, assign) BOOL shouldUnderline;
- (void)setTextToConfigFram:(NSString *)text;
- (void)addTarget:(id)target action:(SEL)action;
@end
在.m文件中
#import "UnderLineLabel.h"
@implementation UnderLineLabel
@synthesize highlightedColor = _highlightedColor;
@synthesize shouldUnderline = _shouldUnderline;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
}
return self;
}
- (id)init
{
if (self = [super init]) {
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
if (self = [super initWithCoder:aDecoder]) {
}
return self;
}
- (void)setShouldUnderline:(BOOL)shouldUnderline
{
_shouldUnderline = shouldUnderline;
}
- (CGFloat) heightForLabel: (UILabel *)label WithText: (NSString *) strText
{
CGSize constraint = CGSizeMake(label.frame.size.width, CGFLOAT_MAX);
CGSize size = [strText sizeWithFont: label.font constrainedToSize:constraintlineBreakMode:label.lineBreakMode];
return size.height;
}
- (void)drawRect:(CGRect)rect
{
[super drawRect:rect];
if (_shouldUnderline) {
CGFloat size = self.width;
// CGSize size =[self.text sizeWithFont:self.font
// forWidth:self.frame.size.width
// lineBreakMode:NSLineBreakByWordWrapping];
CGSize allSize = [self.text sizeWithFont:self.font];
double currentNumOfLines = ceil(allSize.width/size);
CGFloat startX = 0;
switch (self.textAlignment) {
case UITextAlignmentLeft:
startX = rect.origin.x;
break;
case UITextAlignmentCenter:
startX = (rect.size.width - allSize.width)/2.0f;
break;
case UITextAlignmentRight:
startX = rect.size.width - allSize.width;
break;
default:
break;
}
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(context, self.textColor.CGColor);
CGContextBeginPath(context);
for (int i=1; i<=currentNumOfLines; i++) {
if(i == currentNumOfLines)
{
CGFloat halfWayUp = rect.size.height-1 + rect.origin.y;
CGContextMoveToPoint(context, startX, halfWayUp);
CGContextAddLineToPoint(context, startX + allSize.width - size*(currentNumOfLines-1) +2, halfWayUp);
}else{
CGFloat halfWayUp = rect.size.height*i/currentNumOfLines - 1 + rect.origin.y;
CGContextMoveToPoint(context, startX, halfWayUp);
CGContextAddLineToPoint(context, startX + size +2, halfWayUp);
}
}
CGContextStrokePath(context);
}
}
- (void)setTextToConfigFram:(NSString *)text
{
[super setText:text];
CGFloat fHeight = [LayoutHelper heightForLabel:self WithText:text];
[self setNumberOfLines:0];
[self setFrame:CGRectMake(self.originX, self.originY, self.size.width, fHeight)];
}
- (void)addTarget:(id)target action:(SEL)action
{
[self setUserInteractionEnabled:TRUE];
_actionView = [[UIControl alloc] initWithFrame:self.bounds];
[_actionView setBackgroundColor:[UIColor clearColor]];
[_actionView addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
}
- (void)appendHighlightedColor
{
self.backgroundColor = self.highlightedColor;
}
- (void)removeHighlightedColor
{
self.backgroundColor = [UIColor clearColor];
}
@end