給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