【发布时间】: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 那么只有你才能得到准确的输出。