zoukankan      html  css  js  c++  java
  • 给label绘制下划线

    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
  • 相关阅读:
    维特比(Viterbi)算法解最优状态序列
    c#重要知识点复习1---程序流程控制
    学C# Hook原理及EasyHook简易教程
    EmguCV 绘画图形
    EmguCV创建/保存图片
    EmguCV中图像类型进行转换
    basler 相机拍照简单类综合Emgu.CV---得到图档--原创
    RotatedRect 类的用法
    EmguCv“线段” 结构类型学习
    aforge 学习-基本图像处理要用的类库
  • 原文地址:https://www.cnblogs.com/someonelikeyou/p/3919466.html
Copyright © 2011-2022 走看看