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. Jquery添加移除样式

    获取与设置样式 获取class和设置class都可以使用attr()方法来完成.例如使用attr()方法来获取p元素的class,JQuery代码如下: var p_class = $("p ...

  2. spring 占位符 默认值

    问题: 今天结合spel使用占位符时,存在没有配置文件中没有配置项的情况,就想给配置一个默认值. 解决方案: public abstract class PlaceholderConfigurerSu ...

  3. hdu 5655 CA Loves Stick

    CA Loves Stick Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) ...

  4. thinkPHP 无法create,无法插入数据,提示非法数据对象

    4.thinkPHP 无法create,提示非法数据对象解决方法:不要create+add,而用 data[]= '';+add$m_r_fa_account = D('R_fa_account'); ...

  5. LOTUS 迁移到Exchange 2010 POC 之在Exchange 2007 安装Lotus Admin!

    双击Setup安装:

  6. Java垃圾回收器

    一.Java垃圾回收器要负责完成以下3个任务: 1.分配内存 2.确保被引用对象的内存不被错误回收 3.回收不再被引用的对象的内存空间 二.垃圾回收是一个复杂而又耗时的操作.如果JVM花费过多的时间在 ...

  7. 【M33】将非尾端类设计为抽象类

    1.考虑下面的需求,软件处理动物,Cat与Dog需要特殊处理,因此,设计Cat和Dog继承Animal.Animal有copy赋值(不是虚方法),Cat和Dog也有copy赋值.考虑下面的情况: Ca ...

  8. 试读《JavaScript语言精髓与编程实践》

    有幸看到iteye的活动,有幸读到<JavaScript语言精髓与编程实践_第2版>的试读版本,希望更有幸能完整的读到此书. 说来读这本书的冲动,来得很诡异,写一篇读后感,赢一本书,其实奖 ...

  9. IOS 7 Study - Manipulating a Navigation Controller’s Array of View

    ProblemYou would like to directly manipulate the array of view controllers associated with aspecific ...

  10. Codeforces Gym 100015H Hidden Code 暴力

    Hidden Code 题目连接: http://codeforces.com/gym/100015/attachments Description It's time to put your hac ...