给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


目录
相关文章
|
Swift iOS开发
iOS 用一个布局来解决嵌套问题—— UICollectionViewCompositionalLayout
iOS 用一个布局来解决嵌套问题—— UICollectionViewCompositionalLayout
iOS 用一个布局来解决嵌套问题—— UICollectionViewCompositionalLayout
|
11月前
|
存储 关系型数据库 MySQL
MySQL支持多种数据类型
MySQL支持多种数据类型
252 4
|
存储 关系型数据库 MySQL
MySQL表空间结构与页、区、段的定义
一、概念引入 1、页 InnoDB是以页为单位管理存储空间的,在InnoDB中针对不同的目的设计了各种不同类型的页面。如下(省略了FIL_PAGE或FiL_PAGE_TYPE的前缀):
|
存储 SQL 关系型数据库
MySQL意向锁是什么?
意向锁用于协调InnoDB存储引擎中的行锁与表锁,避免全表扫描判断行锁的存在,提升性能。主要包括意向共享锁(IS)与意向排他锁(IX),分别在请求行级共享(S)锁与排他(X)锁前加于表级。意向锁自动管理,无需用户干预。例如,事务A锁定一行时先加IS锁,B事务可加IX锁但不能直接加表级X锁。意向锁与行级S/X锁兼容,仅与表级S/X锁冲突。这确保了锁机制高效且减少冲突。
423 0
|
SQL 关系型数据库 MySQL
MySQL Online DDL原理解读
MySQL Online DDL原理解读
321 3
|
存储 算法 关系型数据库
【MySQL技术内幕】4.4-InnoDB数据页结构
【MySQL技术内幕】4.4-InnoDB数据页结构
239 1
|
SQL 存储 关系型数据库
【MySQL】DDL的表操作详解:创建&查询&修改&删除
【MySQL】DDL的表操作详解:创建&查询&修改&删除
|
网络协议 安全 Unix
6种查看Linux进程占用端口号的方法
6种查看Linux进程占用端口号的方法
2680 0
|
C#
WPF技术之Foreground
在WPF中,Foreground属性用于设置元素的前景颜色。与Background属性类似,Foreground属性可以接受多种类型的值,包括颜色、图像、线性渐变和径向渐变等。
1057 0