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. 实现LoadRunner多个场景的顺序执行

    应用场景假设有3个不同的测试场景,分别为并发登录.核心业务.可靠性测试,3个场景有先后执行顺序.由于白天测试机器另有用处,只能在晚上进行性能测试,这时我们的期望是能否把测试场景都设定好之后晚上自动运行 ...

  2. POJ 2828Buy Tickets

    POJ 2828 题目大意是说有n个插入操作,每次把B插入到位置A,原来A以后的全部往后移动1,球最后的序列 tree里保存的应该是这整个区间还有多扫个位置可以插入数据,那么线段树里从后往前扫描依次插 ...

  3. sql查找字符串是否包含字符

    SELECT [Fgoodsid] ,[Fgoodsname] ,[Fcinema] ,[Fprice] FROM [tenpaytest].[dbo].[goodsinfo]where Fgoods ...

  4. InvocationHandler

    ====================================================================== 代理类生成之后再调用目标方法时就会调用invoke方法 p ...

  5. C# 获取字符串中的数字

    /// <summary> /// 获取字符串中的数字(不包含小数点) /// </summary> /// <param name="str"> ...

  6. 7. 泛化(Generalization)

    什么是泛化关系?用一个例子简单的说:假设A是B和C的父类,B.C具有公共类(父类)A,说明A是B.C的一般化(概括,也称泛化),B.C是A的特殊化. 在编程上,泛化关系(Generalization) ...

  7. input标签写CSS时需要注意的几点(先收藏)

    (从已经死了一次又一次终于挂掉的百度空间人工抢救出来的,发表日期2014-05-05) 飞鱼的声纳顶部的搜索框让我头疼了很长时间,原因是总不能获得跨浏览器的统一样式.主要的问题有这么两个:一是inpu ...

  8. winform 在指定目录下已经生成资源Image图片的方式

    假设在项目目录下存在一个Image目录,注意其中图片已经都设置成为:生成方式为资源文件. /// <summary> /// 得到要绘置的图片对像 /// </summary> ...

  9. CMSIS Example - osMessageQ osMessagePut osMessageGet

    #include "cmsis_os.h" void Thread0( void * arg); void Thread1( void * arg); osThreadDef( T ...

  10. 64位ubuntu下装32位软件

    本帖最后由 wuy069 于 2013-10-25 12:28 编辑 很多软件只有32位的,有的依赖32位库还挺严重的:从ubuntu 13.10已经废弃了ia32-libs,但可以使用多架构,安装软 ...