自定义UILable (实现在label中间画线)
2013-04-15 22:43
176 查看
自定义UILabel 使其继承UILabel
然后重写drawTextInRect:方法
代码如下:
-(void)drawTextInRect:(CGRect)rect
{
[super drawTextInRect:rect];
if(_strikeThroughEnabled)
{
CGSize textSize=[[self text] sizeWithFont:[self font]];
CGFloat strikeWidth=textSize.width;
CGRect lineRect;
if ([self
textAlignment] == NSTextAlignmentRight)
{
lineRect = CGRectMake(rect.size.width - strikeWidth, rect.size.height/2, strikeWidth,
1);
}
else
if ([self
textAlignment] == NSTextAlignmentCenter) {
lineRect = CGRectMake(rect.size.width/2 - strikeWidth/2, rect.size.height/2,
strikeWidth, 1);
}
else
{
lineRect = CGRectMake(0, rect.size.height/2, strikeWidth,
1);
}
}
CGContextRef context=UIGraphicsGetCurrentContext();
if(self.strikelineColor!=nil)
{
CGFloat r,g,b,a;
[self.strikelineColor getRed:&r green:&g blue:&b alpha:&a];
CGContextSetRGBFillColor(context,r,g,b,a);
}
CGContextFillRect(context,lineRect);
}
}
然后重写drawTextInRect:方法
代码如下:
-(void)drawTextInRect:(CGRect)rect
{
[super drawTextInRect:rect];
if(_strikeThroughEnabled)
{
CGSize textSize=[[self text] sizeWithFont:[self font]];
CGFloat strikeWidth=textSize.width;
CGRect lineRect;
if ([self
textAlignment] == NSTextAlignmentRight)
{
lineRect = CGRectMake(rect.size.width - strikeWidth, rect.size.height/2, strikeWidth,
1);
}
else
if ([self
textAlignment] == NSTextAlignmentCenter) {
lineRect = CGRectMake(rect.size.width/2 - strikeWidth/2, rect.size.height/2,
strikeWidth, 1);
}
else
{
lineRect = CGRectMake(0, rect.size.height/2, strikeWidth,
1);
}
}
CGContextRef context=UIGraphicsGetCurrentContext();
if(self.strikelineColor!=nil)
{
CGFloat r,g,b,a;
[self.strikelineColor getRed:&r green:&g blue:&b alpha:&a];
CGContextSetRGBFillColor(context,r,g,b,a);
}
CGContextFillRect(context,lineRect);
}
}
相关文章推荐
- C#自定义Label 实现文字描边阴影
- 实现自定义label上 指定文字的颜色
- android 自定义LabelView实现各类小标签,重要功能已标注
- android 自定义LabelView实现各类小标签,重要功能已标注
- C# 自定义Label实现 指定字符串(关键词)高亮显示(字体、颜色)
- 自定义一个label 实现数字自动增加,逢十进一
- C# Winform 通过FlowLayoutPanel及自定义的编辑控件,实现快速构建C/S版的编辑表单页面
- Qt实现类似sendmessage和postmessage发送自定义消息
- 让你的页面实现自定义的 Ajax Loading加载的体验!
- Android 自定义View实现文本流布局
- Angular实现的自定义模糊查询、排序及三角箭头标注功能示例
- (C#)DataGrid实现自定义分页,鼠标移至变色,删除确认、可编辑,可删除
- Android UI设计之<五>自定义DrawView组件,实现数字签名效果
- android自定义View之仿通讯录侧边栏滑动,实现A-Z字母检索
- jQueryUI如何自定义组件实现代码
- DataGrid实现自定义分页,鼠标移至变色,删除确认、可编辑,可删除
- 自定义ViewGroup实现ArcMenu
- android圆角矩形图片的实现进而讨论view自定义的要点
- ARKit从入门到精通(3)-ARKit自定义实现
- Android自定义View: 如何实现类钟摆的动画效果?