UIlabel本身没有下划线的属性,使用绘制的方法,添加下滑下,并且赋给该label一个action作为响应方法,实现DIY超链接的效果。

//调用

#import "UnderLineLabel.h"
UnderLineLabel *label = [[UnderLineLabel alloc] initWithFrame:CGRectMake(, , , )];
[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(, )];
[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:];
}
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(,
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-
lineBreakMode:NSLineBreakByTruncatingTail];
NSLog(@"%f %f", fontSize.width, fontSize.height);
[self setNumberOfLines:];
[self setFrame:CGRectMake(, , 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

给label绘制下划线的更多相关文章

  1. iOS给label加入下划线

    UILabel *myLabel = [[UILabelalloc] ,, , )]; NSMutableAttributedString *content = [[NSMutableAttribut ...

  2. Unity UGUI图文混排(七) -- 下划线

    之前更新超链接的时候,忘了搭配实现一个下划线的功能,这篇文章就是来补上这一个功能,时间有点长,一方面没有很好的思路,一方面也没多少时间. 先在网上收集了一下下划线的实现操作,一种是在文本下再创建一个文 ...

  3. wpf label下划线不显示的问题

    突然发现label设置content的值为字符串时,如果字符串中包含_的话,在展示出来时下划线就不见了,百度了一下,发现了问题根源,说的label的ContentPresenter默认将下划线处理成快 ...

  4. dephi(pascal)中修改Label字体的样式(加粗,斜体,下划线)

    不废话,直接代码: Label1.Font.style:=[fsBold,fsItalic,fsUnderline]; //加粗.斜体,下划线

  5. iOS 给UILabel文字加下划线

    摘自:http://blog.sina.com.cn/s/blog_6cd380c10101b6hn.html //带下划线的“注” NSMutableAttributedString可变的属性字符串 ...

  6. android TextView 添加下划线

    android Textview加下划线 由于新做的一个项目要求有字体带下划线效果,当时看了下其实可以通过图片伪造出那种视觉效果.但是为了体现点技术含量,于是我想用Textview带下划线的效果.方法 ...

  7. iOS-属性字符串添加下划线、删除线

    常用到的属性字符串 ///定义属性字符串NSMutableAttributedString *att = [[NSMutableAttributedString alloc]initWithStrin ...

  8. FragmentTabHostUnderLineDemo【FragmentTabHost带下划线】

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 使用FragmentTabHost实现顶部选项卡(带下划线效果)展现. 效果图 代码分析 1.该Demo中采用的是FragmentT ...

  9. iOS开发-UILabel和UIButton添加下划线

    关于UILabel和UIButton有的时候需要添加下划线,一般有两种方式通过默认的NSMutableAttributedString设置,第二种就是在drawRect中画一条下划线,本文就简单的选择 ...

随机推荐

  1. G450 CPU 升级

    T系列是正常功耗的CPU,功耗35W,发热量大些, P系列是低功耗的U,功耗25W,发热量小些. P8700的性能比T6600高15%左右,不过平常应用感觉不是很明显. p8800cpu P8600 ...

  2. TdxAlertWindowManager右下角HINT显示控件

    带爱像的右下角HINT显示,自动隐藏 function alterInfo: TdxAlertWindowManager;begin  if not Assigned(Falter) then  be ...

  3. SQLCONNECTION使用HTTP通信协议和中间件连接

    SQLCONNECTION支持TCP/IP和HTTP两种通信协议和中间件连接.一般地,默认情况下使用TCP/IP协议. HTTP 协议的一个非常重要的优势在于穿越防火墙. SQLCONNECTION使 ...

  4. HDU 4870Rating(推公式)

    有关这个题的高斯消元的方法已经在我的另一篇博客中给出http://www.cnblogs.com/gj-Acit/p/3888382.html 这里介绍一个很吊的解法,复杂度降到了O(n),以下转自h ...

  5. 在WindowsServer2008服务器上安装SQLServer2008R2

    登录服务器 使用远程桌面登录Windows Server 2008   安装前的准备工作 下载SQL Server安装程序 下载Microsoft SQL Server2008 R2 RTM - Ex ...

  6. C# 使用ping命令

    方法一:调用cmd 的ping命令 private static string CmdPing(string strIp) { Process p = new Process(); p.StartIn ...

  7. 错误"因为数据库正在使用,所以无法获得对数据库的独占访问权"的解决方案

    今天在还原数据库的时候,提示"因为数据库正在使用,所以无法获得对数据库的独占访问权",无论我是重启数据库,还是重启计算机,都不能解决问题,多番尝试后,终于解决了该问题.现将引发该问 ...

  8. 【转】2D动画:view的Matrix

    Matrix,中文里叫矩阵,高等数学里有介绍,在图像处理方面,主要是用于平面的缩放.平移.旋转等操作. 首先介绍一下矩阵运算.加法和减法就不用说了,太简单了,对应位相加就好.图像处理,主要用到的是乘法 ...

  9. 关于App.config配置文件

    今天在做复习的时候,突然发现自己无法读取配置文件中的数据库连接字符串,而且检查了半天也没找出原因,最后求助万能的度娘才得以解决—— 1.App.config配置文件在项目中添加后不要修改名称,否则会出 ...

  10. DeleteDC() 与 ReleaseDC() 的区别 [转]

    DeleteDC 该函数删除指定的设备上下文环境(DC). 原型: BOOL DeleteDC(HDC hdc): 参数: hdc:设备上下文环境的句柄. 返回值: 成功,返回非零值:失败,返回零.调 ...