用UILabel文字加下划线

用UILabel文字加下划线

本文介绍了用UILabel文字加下划线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

//下划线码
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];

如何根据我得到的文字修正行宽?


'的div类= h2_lin>解决方案

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

//下划线码
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];


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

这篇关于用UILabel文字加下划线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 23:55