UILabel,文字中间划横线
2015-11-02 11:58
465 查看
在项目中我们会有这样的需求就是在label的内容上划一个线,我们可以使用一个透明的有一条线的图片实现,但是那样那条线的颜色就是固定的了,如何灵活的画线呢?
我们可以继承系统的UILabel然后在对其添加一个划线的属性。代码如下:
在使用的时候直接调用自己写的这个label初始化 然后设置showLine这个属性,就可以实现划线的功能了,是不是很简单呢! showLineColor通过对这个属性的设置,可以改变这条横线的颜色
我们可以继承系统的UILabel然后在对其添加一个划线的属性。代码如下:
#import <UIKit/UIKit.h> /** * UILabel 在label中划线 */ @interface MySelfLabel : UILabel @property(nonatomic, assign) BOOL showLine; @property(nonatomic, assign) UIColor * showLineColor; @end =============================== #import "MySelfLabel.h" #pragma mark - UILabel @implementation MySelfLabel - (void)drawRect:(CGRect)rect { [super drawRect:rect]; if (_showLine) { if (_showLineColor) { [_showLineColor set]; //横线的颜色设置 }else { [[UIColor greenColor] set]; //默认 横线颜色 } CGContextRef c = UIGraphicsGetCurrentContext(); CGContextSetLineWidth(c, 1); CGContextBeginPath(c); CGFloat halfWayUp = rect.size.height/2 + rect.origin.y; CGContextMoveToPoint(c, rect.origin.x, halfWayUp);//起点 CGContextAddLineToPoint(c, rect.origin.x + rect.size.width, halfWayUp);//终点 CGContextStrokePath(c); } } @end
在使用的时候直接调用自己写的这个label初始化 然后设置showLine这个属性,就可以实现划线的功能了,是不是很简单呢! showLineColor通过对这个属性的设置,可以改变这条横线的颜色
相关文章推荐
- Mysql中 unique列插入重复值该怎么解决呢
- hdu5531 Rebuild
- mac os安装pyquery
- UIlabel - 富文本属性
- 网上下载源码导入出现Call requires API level错误
- 基于MVC4+EasyUI的Web开发框架经验总结(14)--自动生成图标样式文件和图标的选择操作
- Codeforces 590B Chip 'n Dale Rescue Rangers(二分)
- UOJ #142. 【UER #5】万圣节的南瓜灯 并查集
- UITextView实现 return键隐藏键盘
- 很多UI效果,做个记录
- druid
- Unique Binary Search Trees
- hdu 3397 Sequence operation(线段树:区间更新)
- hdu 5538 House Building(水)
- UITableView UITableViewStylePlain UITableViewStyleGrouped
- iOS7上TableViewCell的button和UIImageView个别未显示的bug
- hdu 5531 Rebuild(三分)
- ADT版本查看,This Android SDK requires Andr...ate ADT to the latest问题
- [js]Easyui组件扩展--fieldset
- js实现marquee头尾无缝滚动