控件里的字体下划线

如果要在按钮里的字体加下划线

可以新建一个类继承UIButton 再重写(void)drawRect:(CGRect)rect方法

View Code
- (void)drawRect:(CGRect)rect 
    {
            // Get the Render Context
        CGContextRef ctx = UIGraphicsGetCurrentContext();

        // Measure the font size, so the line fits the text.
        // Could be that "titleLabel" is something else in other classes like UILable, dont know.
        // So make sure you fix it here if you are enhancing UILabel or something else..
        CGSize fontSize =[self.titleLabel.text sizeWithFont:self.titleLabel.font 
                                                   forWidth:self.bounds.size.width
                                              lineBreakMode:UILineBreakModeTailTruncation];
        // Get the fonts color. 
        const float * colors = CGColorGetComponents(self.titleLabel.textColor.CGColor);
        // Sets the color to draw the line
        CGContextSetRGBStrokeColor(ctx, colors[0], colors[1], colors[2], 1.0f); // Format : RGBA

        // Line Width : make thinner or bigger if you want
        CGContextSetLineWidth(ctx, 1.0f);

        // Calculate the starting point (left) and target (right)   
        float fontLeft = self.titleLabel.center.x - fontSize.width/2.0;
        float fontRight = self.titleLabel.center.x + fontSize.width/2.0;

        // Add Move Command to point the draw cursor to the starting point
        CGContextMoveToPoint(ctx, fontLeft, self.bounds.size.height - 1);

        // Add Command to draw a Line
        CGContextAddLineToPoint(ctx, fontRight, self.bounds.size.height - 1);

        // Actually draw the line.
        CGContextStrokePath(ctx);

        // should be nothing, but who knows...
        [super drawRect:rect];   
    }

 

 

 

转载于:https://www.cnblogs.com/zhangdadi/archive/2012/07/26/2610599.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值