为 UILabel 文本添加下划线
我有 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];
如何根据我得到的文本固定线宽?
I have UILabel
whose string is being set at runtime. The text in UILabel
is centrally aligned. I want to display an underline below the label text. But the line should have X same where the text begins (consider center alignment) and width equal to text width (not label width). For underline I have created a UIView
and have set its background color. But I am not able to fix length of underline as I want.
Below is the code I have used:
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];
How can I fix the width of line according to text I get?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我是这样做的。子类化 UILabel 并画线。确保引入 QuartzCore
Here's how I did it. Subclass UILabel and draw the line. Make sure you bring in QuartzCore
我只是稍微简化了实现:
I just simplified the implementation a bit: