我UILabel
其字符串被设置在运行时。 UILabel
中的文本集中对齐。我想在标签文本下方显示下划线。不过,该行应具有X相同的文本开始的地方(考虑居中对齐)和宽度等于文本宽度(未标签宽度)。对于下划线我创建了一个UIView
,并设置其背景色。但我无法按照自己的意愿确定下划线的长度。下划线的UILabel文本
下面是我用的代码:
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 .. –
它又有什么不同? – Nitish
使用UIWebView的U可以通过HTML字符串,你可以有你的网页所有所需的效果.. –