阅读284 返回首页    go 阿里云 go 技术社区[云栖]


给label添加超链接等处理

//
//  HYBHyperlinkLabel.h
//  CloudShopping
//
//  Created by sixiaobo on 14-7-10.
//  Copyright (c) 2014年 com.Uni2uni. All rights reserved.
//

#import <UIKit/UIKit.h>

/*!
 * @brief 定制超链接标签,也就是上面是文字,下面是一条横线,可以指定颜色值,默认是蓝色,
 *        下划线会处在文字的正下方,宽度会根据文字自动调整,字体大小默认是13号字
 * @note  仅适用于单行超链接
 * @author huangyibiao
 */
@interface HYBHyperlinkLabel : UIView

@property (nonatomic, strong) UIColor *textColor;      // 文本颜色,默认是[UIColor blueColor]
@property (nonatomic, strong) UIColor *underlineColor; // 下划线颜色,默认是[UIColor blueColor]

- (id)initWithFrame:(CGRect)frame text:(NSString *)text;
- (id)initWithFrame:(CGRect)frame text:(NSString *)text font:(UIFont *)font;

- (id)initWithFrame:(CGRect)frame text:(NSString *)text textColor:(UIColor *)textColor;
- (id)initWithFrame:(CGRect)frame text:(NSString *)text textColor:(UIColor *)textColor font:(UIFont *)font;

- (id)initWithFrame:(CGRect)frame
               text:(NSString *)text
          textColor:(UIColor *)textColor
     underlineColor:(UIColor *)underlineColor;
- (id)initWithFrame:(CGRect)frame
               text:(NSString *)text
          textColor:(UIColor *)textColor
     underlineColor:(UIColor *)underlineColor
               font:(UIFont *)font;

// 如果需要在点击超链接的时候,可以处理响应,那么需要调用此方法来指定回调
- (void)addTarget:(id)target action:(SEL)action;

@end


//
//  HYBHyperlinkLabel.m
//  CloudShopping
//
//  Created by sixiaobo on 14-7-10.
//  Copyright (c) 2014年 com.Uni2uni. All rights reserved.
//

#import "HYBHyperlinkLabel.h"

@interface HYBHyperlinkLabel ()

@property (nonatomic, strong) UILabel *textLabel;      // 文本内容
@property (nonatomic, strong) UILabel *underlineLabel; // 下划线,默认高度为1px
@property (nonatomic, weak)   id      target;
@property (nonatomic, assign) SEL     action;

@end

@implementation HYBHyperlinkLabel

//
- (id)initWithFrame:(CGRect)frame text:(NSString *)text {
    return [self initWithFrame:frame text:text font:kFontWithSize(13)];
}

- (id)initWithFrame:(CGRect)frame text:(NSString *)text font:(UIFont *)font {
    return [self initWithFrame:frame
                          text:text
                     textColor:[UIColor blueColor]
                underlineColor:[UIColor blueColor]
                          font:font];
}

//
- (id)initWithFrame:(CGRect)frame text:(NSString *)text textColor:(UIColor *)textColor {
    return [self initWithFrame:frame text:text textColor:textColor font:kFontWithSize(13)];
}

- (id)initWithFrame:(CGRect)frame text:(NSString *)text textColor:(UIColor *)textColor font:(UIFont *)font {
    return [self initWithFrame:frame
                          text:text
                     textColor:textColor
                underlineColor:[UIColor blueColor]
                          font:font];
}

//
- (id)initWithFrame:(CGRect)frame
               text:(NSString *)text
          textColor:(UIColor *)textColor
     underlineColor:(UIColor *)underlineColor
               font:(UIFont *)font {
    // 以文字高度作为视图的高度
    CGSize size = [text sizeWithFont:font];
    frame.size.height = size.height;
    frame.size.width = size.width;
    
    if (self = [super initWithFrame:frame]) {
        self.textColor = textColor;
        self.underlineColor = underlineColor;
        
        CGRect rect = CGRectMake(0, 0, frame.size.width, frame.size.height);
        self.textLabel = [HYBUIMaker labelWithFrame:rect
                                               text:text
                                          textColor:textColor
                                               font:font];
        [self addSubview:self.textLabel];
        
        CGFloat originX = (self.textLabel.width - size.width) / 2;
        self.underlineLabel = [HYBUIMaker labelWithFrame:CGRectMake(originX, self.textLabel.bottomY,
                                                                    size.width, 0.8)];
        self.underlineLabel.backgroundColor = self.underlineColor;
        [self addSubview:self.underlineLabel];
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame
               text:(NSString *)text
          textColor:(UIColor *)textColor
     underlineColor:(UIColor *)underlineColor {
    return [self initWithFrame:frame
                          text:text
                     textColor:textColor
                underlineColor:underlineColor
                          font:kFontWithSize(13)];
}

- (void)addTarget:(id)target action:(SEL)action {
    self.target = target;
    self.action = action;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:target action:action];
    [self addGestureRecognizer:tap];
    return;
}

@end


最后更新:2017-04-03 05:39:42

  上一篇:go NSFileManager扩展
  下一篇:go NSObject扩展