给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


目录
相关文章
|
4月前
label控件
label控件
24 1
|
4月前
|
小程序 Python
Tkinter 中的标签(Label)
Tkinter 中的标签(Label)组件是一种用于显示文本或图像的控件。它可以通过 tk.Label() 函数创建,常用的属性包括: - text:设置标签显示的文本内容。
33 1
|
5月前
HTML <input> 标签的 autocomplete 属性
HTML <input> 标签的 autocomplete 属性
29 0
|
8月前
|
前端开发
使用 div 实现 input、textarea 输入框,并支持 placeholder 属性(解决浏览器兼容问题)
使用 div 实现 input、textarea 输入框,并支持 placeholder 属性(解决浏览器兼容问题)
110 0
form表单 +input的属性值+textarea文本域+select下拉菜单
form表单 +input的属性值+textarea文本域+select下拉菜单
05 - 超链接 a 标签+ 图片标签img
05 - 超链接 a 标签+ 图片标签img
114 0
|
Shell Python 容器
html+css实战40-label标签
html+css实战40-label标签
83 0
html+css实战40-label标签
【layui】多图上传在input框的显示
【layui】多图上传在input框的显示
214 1