給label繪製底線

來源:互聯網
上載者:User

標籤:style   blog   color   使用   io   for   ar   cti   

UIlabel本身沒有底線的屬性,使用繪製的方法,添加下滑下,並且賦給該label一個action作為回應程式法,實現DIY超連結的效果。

//調用

#import "UnderLineLabel.h" UnderLineLabel *label = [[UnderLineLabel alloc] initWithFrame:CGRectMake(50, 200, 300, 30)];    [label setBackgroundColor:[UIColor clearColor]];    // [label setBackgroundColor:[UIColor yellowColor]];    [label setTextColor:[UIColor blueColor]];    [label setBackgroundColor:[UIColor yellowColor]];    label.highlightedColor = [UIColor redColor];    label.shouldUnderline = YES;            [label setText:str andCenter:CGPointMake(200, 240)];    [label addTarget:self action:@selector(labelClicked)];    [self.view addSubview:label];  //  [label release];//////"超連結執行的方法"- (void)labelClicked{    NSLog(@"%@", NSStringFromSelector(_cmd));    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Clicked!" message:nil delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];    [alertView show];    [alertView release];}

 

 

UnderLineLabel.h

#import <UIKit/UIKit.h>@interface UnderLineLabel : UILabel{    UIControl *_actionView;    UIColor *_highlightedColor;    BOOL _shouldUnderline;}@property (nonatomic, retain) UIColor *highlightedColor;@property (nonatomic, assign) BOOL shouldUnderline;- (void)setText:(NSString *)text andCenter:(CGPoint)center;- (void)addTarget:(id)target action:(SEL)action;@end 

//  UnderLineLabel.m

//  UnderLineLabel.m#import "UnderLineLabel.h"@implementation UnderLineLabel@synthesize highlightedColor = _highlightedColor;@synthesize shouldUnderline = _shouldUnderline;//- (void)dealloc//{//    [_actionView release],//    _actionView = nil;//    self.highlightedColor = nil;//   [super dealloc];//}- (id)initWithFrame:(CGRect)frame{    self = [super initWithFrame:frame];    if (self) {    }    return self;}- (id)init{    if (self = [super init]) {        self.font=[UIFont systemFontOfSize:6];    }    return self;}- (id)initWithCoder:(NSCoder *)aDecoder{    if (self = [super initWithCoder:aDecoder]) {    }    return self;}- (void)setShouldUnderline:(BOOL)shouldUnderline{    _shouldUnderline = shouldUnderline;    if (_shouldUnderline) {        [self setup];    }}- (void)drawRect:(CGRect)rect{    NSLog(@"%@", NSStringFromSelector(_cmd));    [super drawRect:rect];    if (self.shouldUnderline) {        NSLog(@"XXXXX");        CGContextRef ctx = UIGraphicsGetCurrentContext();        CGSize fontSize =[self.text sizeWithFont:self.font                                        forWidth:self.frame.size.width                                   lineBreakMode:NSLineBreakByTruncatingTail];                CGContextSetStrokeColorWithColor(ctx, self.textColor.CGColor);  // set as the text‘s color        CGContextSetLineWidth(ctx, 2.0f);                CGPoint leftPoint = CGPointMake(0,                                        self.frame.size.height);        CGPoint rightPoint = CGPointMake(fontSize.width,                                         self.frame.size.height);        CGContextMoveToPoint(ctx, leftPoint.x, leftPoint.y);        CGContextAddLineToPoint(ctx, rightPoint.x, rightPoint.y);        CGContextStrokePath(ctx);    }}- (void)setText:(NSString *)text andCenter:(CGPoint)center{    [super setText:text];    CGSize fontSize =[self.text sizeWithFont:self.font                                    forWidth:Phone_Weight-44                               lineBreakMode:NSLineBreakByTruncatingTail];    NSLog(@"%f   %f", fontSize.width, fontSize.height);    [self setNumberOfLines:0];    [self setFrame:CGRectMake(0, 0, fontSize.width, fontSize.height)];    [self setCenter:center];}- (void)setup{    [self setUserInteractionEnabled:TRUE];    _actionView = [[UIControl alloc] initWithFrame:self.bounds];    [_actionView setBackgroundColor:[UIColor clearColor]];    [_actionView addTarget:self action:@selector(appendHighlightedColor) forControlEvents:UIControlEventTouchDown];    [_actionView addTarget:self                    action:@selector(removeHighlightedColor)          forControlEvents:UIControlEventTouchCancel |     UIControlEventTouchUpInside |     UIControlEventTouchDragOutside |     UIControlEventTouchUpOutside];    [self addSubview:_actionView];    [self sendSubviewToBack:_actionView];}- (void)addTarget:(id)target action:(SEL)action{    [_actionView addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];}- (void)appendHighlightedColor{    self.backgroundColor = self.highlightedColor;}- (void)removeHighlightedColor{    self.backgroundColor = [UIColor clearColor];}@end

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.