我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字符串,你可以有你的網頁所有所需的效果.. –