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. NLP初步

    [NLP初步] NLP是Natural Lanuage Process的缩写.搜索引擎可以通过关词匹配和完成很多的任务, 比如话题搜索(搜索包含律师, 法院, 控告等词的文档), 但是搜索引擎无法理解 ...

  2. windows下php cli模式,提示出错

    搞一下php cli,一直提示,找不到php_pdo_mssql.dll模块 原来是在php.ini加载了该模块,但ext下又没有该模块 即便下载了一个php_pdo_mssql.dll,但还是会提示 ...

  3. unigui TUniTreeView demo

    unit untTree; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, For ...

  4. class-dump-z下载地址

    支持MAC.Linux.Win和iOS各个版本 http://download.csdn.net/detail/yukang1989/8414567

  5. 接口是干爹, 继承是亲爹 ---JAVA

    接口(interface)是干爹, 因为你可以有很多很多的干爹爹... 继承(extends)是亲爹, 因为你只能有一个父类, 只有一个亲生的父亲. 单继承,多接口?./>./..

  6. thinkphp过滤html、script

    使用tp3.1版本 1.APP/common 自定义函数 function filter_default(&$value){ $value = htmlspecialchars($value) ...

  7. Linux下如何用vi编辑和保存文件

    vi是Linux终端下或控制台下常用的编辑器,基本的操作方式为:vi /路径/文件名 例如,vi /etc/fstab表示显示/etc/fstab文件的内容.使用键盘上的Page Up和Page Do ...

  8. C3P0连接池使用小结

    C3P0在最近的demo中也用了(我用的是0.9.2.1版本),因为单例很难应付大量并发. 用法详见文档:http://www.mchange.com/projects/c3p0/ 基本的用法在htt ...

  9. HTML5中script的async属性异步加载JS

    HTML5中script的async属性异步加载JS     HTML4.01为script标签定义了5个属性: charset 可选.指定src引入代码的字符集,大多数浏览器忽略该值.defer 可 ...

  10. GitHub-更新数据

    1.查看代码的修改 git status //modified 标示修改的文件 //deleted标示删除的文件 // untracked files 未处理的文件 需要执行 git add方法添加上 ...