【问题标题】:Underline UILabel text [duplicate]下划线 UILabel 文本 [重复]
【发布时间】:2011-11-25 19:06:24
【问题描述】:

我有UILabel,它的字符串是在运行时设置的。 UILabel 中的文本居中对齐。我想在标签文本下方显示下划线。但是该行应该在文本开始的位置具有相同的 X(考虑居中对齐),并且宽度等于文本宽度(不是标签宽度)。对于下划线,我创建了一个UIView 并设置了它的背景颜色。但我无法根据需要固定下划线的长度。

下面是我用过的代码:

 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];  

我怎样才能根据我得到的文字来固定线的宽度?

【问题讨论】:

  • 如果可能的话,你可以使用 UIWebView 而不是 UILabel..
  • 会有什么不同?
  • 使用 UIWebView 你可以传递 html 字符串,你可以在网页上获得所有想要的效果..
  • 你想使用 UIwebview 吗??如果是,那么我可以发布两行代码..也许你是那个代码..
  • 我认为如果你使用 webView 那么只有你才能得到准确的输出。

标签: iphone uiview uilabel


【解决方案1】:
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];  

【讨论】:

  • 谢谢阿曼。你让我开心。
  • 但这不支持多行标签
【解决方案2】:

这就是我的做法。子类 UILabel 并画线。确保引入 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

【讨论】:

    【解决方案3】:

    我只是稍微简化了实现:

    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;
    }
    

    【讨论】:

    • 真的很简单的代码sn-p....太棒了
    猜你喜欢
    • 1970-01-01
    • 2018-01-29
    • 1970-01-01
    • 2012-09-30
    • 1970-01-01
    • 1970-01-01
    • 2014-04-17
    • 1970-01-01
    • 2013-08-22
    相关资源
    最近更新 更多