UILable响应点击事件
2015-11-12 17:47
169 查看
.h文件
复制代码 #import <Foundation/Foundation.h> @class MyLabel; @protocol MyLabelDelegate <NSObject> @required - (void)myLabel:(MyLabel *)myLabel touchesWtihTag:(NSInteger)tag; @end |
复制代码 @interface MyLabel : UILabel { id <MyLabelDelegate> delega 4000 te; } @property (nonatomic, assign) id <MyLabelDelegate> delegate; - (id)initWithFrame:(CGRect)frame; @end |
复制代码 #import "MyLabel.h" #define FONTSIZE 13 #define COLOR(R,G,B,A) [UIColor colorWithRed:R/255.0 green:G/255.0 blue:B/255.0 alpha:A] @implementation MyLabel @synthesize delegate; // 设置换行模式,字体大小,背景色,文字颜色,开启与用户交互功能,设置label行数,0为不限制 - (id)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { [self setLineBreakMode:UILineBreakModeWordWrap|UILineBreakModeTailTruncation]; [self setFont:[UIFont systemFontOfSize:FONTSIZE]]; [self setBackgroundColor:[UIColor clearColor]]; [self setTextColor:COLOR(59,136,195,1.0)]; [self setUserInteractionEnabled:YES]; [self setNumberOfLines:0]; } return self; } // 点击该label的时候, 来个高亮显示 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [self setTextColor:[UIColor whiteColor]]; } // 还原label颜色,获取手指离开屏幕时的坐标点, 在label范围内的话就可以触发自定义的操作 - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { [self setTextColor:COLOR(59,136,195,1.0)]; UITouch *touch = [touches anyObject]; CGPoint points = [touch locationInView:self]; if (points.x >= self.frame.origin.x && points.y >= self.frame.origin.x && points.x <= self.frame.size.width && points.y <= self.frame.size.height) { [delegate myLabel:self touchesWtihTag:self.tag]; } } - (void)dealloc { [super dealloc]; } @end |
复制代码 MyLabel *webSite = [[MyLabel alloc] initWithFrame:CGRectMake(10, 10, 100, 20)]; [webSite setText:@"http://www.google.com"]; [webSite setDelegate:self]; [self.view addSubview:webSite]; [webSite release]; #pragma mark MyLabel Delegate Methods // myLabel是你的MyLabel委托的实例, tag是该实例的tag值, 有点多余, 哈哈 - (void)myLabel:(MyLabel *)myLabel touchesWtihTag:(NSInteger)tag { [[UIApplication sharedApplication] openURL:[NSURL URLWithString:webSite.text]]; } |
相关文章推荐
- UILable响应点击事件
- UITableView
- 【opencl】buildprogram错误日志提示source file is not valid utf-8
- 关于iOS UITextView的一点使用心得,设置placeHolder效果
- 全面解释StringBuilder、StringBuffer和String的关系
- android 搜索框UI
- UITextView添加planceholder
- (温故而知新)iOS开发UI篇—UITableview控件基本使用
- esayui datagrid 列内容超长自动换行
- (温故而知新)iOS开发UI篇—UITableview控件简单介绍
- UIView中的clipsTobounds属性及扩展
- UIScrollView
- Maltab gui 使用函数学习记录
- UITableView分割线式样 与 颜色设置
- UICollectionView 简单使用
- [ios]如何分组具有 UICollectionReusableView (使用 JSON) 的单元格
- 当UIScrollView 遇到 autolayout
- Elasticsearch Query String Query
- UIImage旋转详解
- GUI布局学习1