gpt4 book ai didi

iphone - 为 UILabel 文本添加下划线

转载 作者:行者123 更新时间:2023-12-03 19:26:21 25 4
gpt4 key购买 nike

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

如何根据我得到的文本固定线宽?

最佳答案

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

关于iphone - 为 UILabel 文本添加下划线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7565407/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com