Underline UILabel text [duplicate]
Esta pergunta já tem uma resposta aqui:
Sublinhar texto em UIlabel 19 respostasEu tenhoUILabel
cuja string está sendo definida em tempo de execução. O texto emUILabel
está alinhado centralmente. Quero exibir um sublinhado abaixo do texto do rótulo. Mas a linha deve ter X igual ao início do texto (considere o alinhamento central) e largura igual à largura do texto (não à largura da etiqueta). Para sublinhar, criei umUIView
e definiu a cor de fundo. Mas não consigo fixar o comprimento do sublinhado como quer
Abaixo é o código que eu usei:
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];
Como posso corrigir a largura da linha de acordo com o texto que recebo?