给label绘制下划线
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绘制下划线的更多相关文章
- iOS给label加入下划线
UILabel *myLabel = [[UILabelalloc] ,, , )]; NSMutableAttributedString *content = [[NSMutableAttribut ...
- Unity UGUI图文混排(七) -- 下划线
之前更新超链接的时候,忘了搭配实现一个下划线的功能,这篇文章就是来补上这一个功能,时间有点长,一方面没有很好的思路,一方面也没多少时间. 先在网上收集了一下下划线的实现操作,一种是在文本下再创建一个文 ...
- wpf label下划线不显示的问题
突然发现label设置content的值为字符串时,如果字符串中包含_的话,在展示出来时下划线就不见了,百度了一下,发现了问题根源,说的label的ContentPresenter默认将下划线处理成快 ...
- dephi(pascal)中修改Label字体的样式(加粗,斜体,下划线)
不废话,直接代码: Label1.Font.style:=[fsBold,fsItalic,fsUnderline]; //加粗.斜体,下划线
- iOS 给UILabel文字加下划线
摘自:http://blog.sina.com.cn/s/blog_6cd380c10101b6hn.html //带下划线的“注” NSMutableAttributedString可变的属性字符串 ...
- android TextView 添加下划线
android Textview加下划线 由于新做的一个项目要求有字体带下划线效果,当时看了下其实可以通过图片伪造出那种视觉效果.但是为了体现点技术含量,于是我想用Textview带下划线的效果.方法 ...
- iOS-属性字符串添加下划线、删除线
常用到的属性字符串 ///定义属性字符串NSMutableAttributedString *att = [[NSMutableAttributedString alloc]initWithStrin ...
- FragmentTabHostUnderLineDemo【FragmentTabHost带下划线】
版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 使用FragmentTabHost实现顶部选项卡(带下划线效果)展现. 效果图 代码分析 1.该Demo中采用的是FragmentT ...
- iOS开发-UILabel和UIButton添加下划线
关于UILabel和UIButton有的时候需要添加下划线,一般有两种方式通过默认的NSMutableAttributedString设置,第二种就是在drawRect中画一条下划线,本文就简单的选择 ...
随机推荐
- nginx配置 首页不显示 index.html首页是显示域名
原状况如下: 访问:www.test.com 敲回车后浏览器中自动跳转致: www.test.com/index.html 公司新需求如下: 访问:www.test.com 敲回车后浏览器中url不变 ...
- TPARAMS和OLEVARIANT相互转换
所谓的“真3层”有时候是需要客户端上传数据集的TPARAMS到中间件的. 现在,高版本的DATASNAP的远程方法其实也是直接可以传输TPARAMS类型的变量,但是DELPHI7(七爷).六爷它们是不 ...
- 2013-2014集训之DP
第一周: 经过漫长的时间,终于有时间来写一下结题报告. 地址http://acm.hust.edu.cn/vjudge/contest/view.action?cid=36180#overview A ...
- CCF 201403-3 命令行选项 (STL模拟)
问题描述 请你写一个命令行分析程序,用以分析给定的命 令行里包含哪些选项.每个命令行由若干个字符串组成,它们之间恰好由一个空格分隔.这些字符串中的第一个为该命令行工具的名字,由小写字母组成,你的程序 ...
- ASP.NET MVC- Upload File的例子
上传文件是一项基本功能,一定要了解的.先来看一下使用ASP.NET MVC实现简单的上传. 一.简单的例子 Controller的例子 public ActionResult UploadDemo() ...
- 利用C#实现对excel的写操作
一.COM interop 首先我们要了解下何为COM Interop,它是一种服务,可以使.NET Framework对象能够与COM对象通信.Visual Studio .NET 通过引入面向公共 ...
- VC++ 中滑动条(slider控件)使用 [转+补充]
滑动控件slider是Windows中最常用的控件之一.一般而言它是由一个滑动条,一个滑块和可选的刻度组成,用户可以通过移动滑块在相应的控件中显示对应的值.通常,在滑动控件附近一定有标签控件或编辑框控 ...
- discuz问题综合
1.discuz x2搬家问题:搬家后,需要修改数据库的配置文件,包含: 主要的配置文件有三个:config目录下的config_global.php和config_ucenter.php以 ...
- MVC网站发布常见问题
直接发布的时候生成的bin会漏掉一些文件,从而导致网站无法访问: 解决方法:发布之后,再在本地运行一下网站,然后将运行后生成的bin文件夹下的文件拷贝到发布的文件夹目录下进行覆盖,就可以了
- Codeforces Gym 100803C Shopping 贪心
Shopping 题目连接: http://codeforces.com/gym/100803/attachments Description Your friend will enjoy shopp ...