您的位置:首页 > 产品设计 > UI/UE

自定义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);

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  iOS label uilabel