开发者

Underline UILabel text [duplicate]

This question already has answers here: Underline text in UIlabel (21 answers) Closed 10 years ago.

I have UILabel whose string is being set at runtime. The text in UILabel is centrally aligned. I want to display an underline below the label text. But the line should have X same where the text begins (consider center alignment) and width equal to text width (not label width). For underline I have created a UIView and have set its background color. But I am not able to fix length of underline as I want.

Below is the code I have used:

 UILabel *blabel = [[UILabel alloc] initWithFrame:CGRectMake(XX, 6, 271, 26)];
    blabel.text = [m_BCListArray objectAtIndex:tagcount];
    blabel.textAlignment = UITextAlignmentCenter;
    blabel.backgroundColor = [UIColor clearColor];
    blabel.textColor = [UIColor whiteColor];
    blabel.font = [UIFont systemFontOfSize:14];
    [scrollDemo addSubview:blabel];

    //underline code
    CGSize expectedLabelSize = [[m_BCListArray objectAtIndex:tagcount] sizeWithFont:blabel.font constrainedToSize:blabel.frame.size lineBreakMode:UILineBreakModeWordWrap];
    CGRect newFrame = blabel.frame;
    newFrame.size.height = expectedLabelSize.height;
    blabel.frame = CGRectMake(blabel.frame.origin.x, blabel.frame.origin.y, 271, expectedLabelSize.height);
    blabel.numberOfLines = 1;
    /开发者_运维知识库/[blabel sizeToFit];
    int width=blabel.bounds.size.width;
    int height=blabel.bounds.size.height;

    UIView *viewUnderline=[[UIView alloc] init];
    int len = [[m_BCListArray objectAtIndex:tagcount]length];
    viewUnderline.frame=CGRectMake(XX, 26, len, 1);
    viewUnderline.backgroundColor=[UIColor whiteColor];
    [scrollDemo addSubview:viewUnderline];
    [viewUnderline release];  

How can I fix the width of line according to text I get?


UILabel *blabel = [[UILabel alloc] initWithFrame:CGRectMake(XX, 6, 271, 26)];
blabel.text = [m_BCListArray objectAtIndex:tagcount];
blabel.textAlignment = UITextAlignmentCenter;
blabel.backgroundColor = [UIColor clearColor];
blabel.textColor = [UIColor whiteColor];
blabel.font = [UIFont systemFontOfSize:14];
[scrollDemo addSubview:blabel];

//underline code
CGSize expectedLabelSize = [[m_BCListArray objectAtIndex:tagcount] sizeWithFont:blabel.font constrainedToSize:blabel.frame.size lineBreakMode:UILineBreakModeWordWrap];

UIView *viewUnderline=[[UIView alloc] init];
viewUnderline.frame=CGRectMake((blabel.frame.size.width - expectedLabelSize.width)/2,    expectedLabelSize.height + (blabel.frame.size.height - expectedLabelSize.height)/2,   expectedLabelSize.width, 1);
viewUnderline.backgroundColor=[UIColor whiteColor];
[scrollDemo addSubview:viewUnderline];
[viewUnderline release];  


Here's how I did it. Subclass UILabel and draw the line. Make sure you bring in QuartzCore

@interface MyUnderlinedLabel : UILabel
@end

#import <QuartzCore/QuartzCore.h>

@implementation MyUnderlinedLabel

-(void)drawRect:(CGRect)rect
{
    [super drawRect:rect];

    // Get bounds
    CGRect f = self.bounds;
    CGFloat yOff = f.origin.y + f.size.height - 3.0;

    // Calculate text width
    CGSize tWidth = [self.text sizeWithFont:self.font];

    // Draw underline
    CGContextRef con = UIGraphicsGetCurrentContext();
    CGContextSetStrokeColorWithColor(con, self.textColor.CGColor);
    CGContextSetLineWidth(con, 1.0);
    CGContextMoveToPoint(con, f.origin.x, yOff);
    CGContextAddLineToPoint(con, f.origin.x + tWidth.width, yOff);
    CGContextStrokePath(con);
}

@end


I just simplified the implementation a bit:

UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 100, 320, 30)];
myLabel=[self setLabelUnderline:myLabel];

-(UILabel *)setLabelUnderline:(UILabel *)label{
    CGSize expectedLabelSize = [label.text sizeWithFont:label.font constrainedToSize:label.frame.size lineBreakMode:label.lineBreakMode];
    UIView *viewUnderline=[[UIView alloc] init];
    CGFloat xOrigin=0;
    switch (label.textAlignment) {
        case NSTextAlignmentCenter:
            xOrigin=(label.frame.size.width - expectedLabelSize.width)/2;
            break;
        case NSTextAlignmentLeft:
            xOrigin=0;
            break;
        case NSTextAlignmentRight:
            xOrigin=label.frame.size.width - expectedLabelSize.width;
            break;
        default:
            break;
    }
    viewUnderline.frame=CGRectMake(xOrigin,
                                   expectedLabelSize.height-1,
                                   expectedLabelSize.width,
                                   1);
    viewUnderline.backgroundColor=label.textColor;
    [label addSubview:viewUnderline];
    [viewUnderline release];
    return label;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜