给label绘制下划线
UIlabel本身没有下划线的属性,使用绘制的方法,添加下滑下,并且赋给该label一个action作为响应方法,实现DIY超链接的效果。
//调用
#import "UnderLineLabel.h" UnderLineLabel *label = [[UnderLineLabel alloc] initWithFrame:CGRectMake(50, 200, 300, 30)]; [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(200, 240)]; [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:6]; } 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(0, 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-44 lineBreakMode:NSLineBreakByTruncatingTail]; NSLog(@"%f %f", fontSize.width, fontSize.height); [self setNumberOfLines:0]; [self setFrame:CGRectMake(0, 0, 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
posted on 2014-08-18 14:28 ACM_Someone like you 阅读(528) 评论(0) 编辑 收藏 举报
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
· 上周热点回顾(2.17-2.23)