如何显示 UILabels 数组?
我有一个想要在 UITableViewCell 中显示的项目列表。现在,我只是使用 cell.textLabel 用逗号分隔每个值,但我想做一些更动态的事情。
我怎样才能实现这样的目标?
它是一个带有边框和半径的 UILabel 数组吗?
感谢您的想法。
I have a list of items I'd like to show in a UITableViewCell. Right now, I'm simply using a cell.textLabel with commas separating each value, but I'd like to do something a little more dynamic.
How would I achieve something like this?
Would it be an array of UILabels with borders and radius on those borders?
Thanks for your ideas.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一种可能的快速且简单的方法来做到这一点。它基于您可以获得的代码 此处。
请注意,您必须将 QuartzCore 框架添加到您的项目中,并将其包含在您编写此代码的文件中!
每个 UIView 都由 CALayer 支持。您可以使用 .layer 属性获取 UIView 的 CALayer。由于 UILabel 是 UIView,因此您可以通过这种方式获取其支持层。获得背景图层后,您可以设置其backgroundColor、cornerRadius、borderColor 和borderWidth 属性。这应该可以让你创建圆形效果。
要获得居中效果,请尝试将 UILabel 的 textAlignment 设置为 UITextAlignmentCenter。然后,您可以尝试根据 sizeThatFits 设置 UILabel 的框架,或者基于对要放入标签的字符串调用 sizeWithFont 。
这里有一些快速、完全未经测试的代码可以帮助您入门。
假设您已在某处初始化了 UIFont,如下所示(输入您想要的字体大小)。
然后,对于每个标签,按如下方式进行设置。我假设您已从数组中取出文本并将其放入名为“text”的变量中。 X_PADDING 和 Y_PADDING 是标签文本周围所需的间距。 xLoc 和 yLoc 是用于跟踪要放置标签的 x 和 y 位置的变量。您可能会根据 textSize + X_PADDING + LABEL_SPACING 或其他内容(定义 LABEL_SPACING 的位置)增加 xLoc:
我希望这可以帮助您入门。
Here's a possible quick and easy way to do this. It's based on the code that you can get here.
Note that you have to add the QuartzCore framework to your project and include in the file where you write this code!
Every UIView is backed by a CALayer. You can get at the UIView's CALayer with the .layer property. Since a UILabel is a UIView, you can get its backing layer this way. Once you have the backing layer, you can set its backgroundColor, cornerRadius, borderColor, and borderWidth properties. That should let you create the rounded effect.
To get the centered effect, try setting the UILabel's textAlignment to UITextAlignmentCenter. Then, you could try setting the UILabel's frame based on sizeThatFits, or maybe based on calling sizeWithFont on the string you're putting into the label.
Here's some quick, totally untested code to get you started.
Assume that somewhere you've initialized a UIFont as follows (put in whatever size you want for the font).
Then, for each label, set it up as follows.I'm assuming you've pulled the text out of an array and put into a variable called "text". X_PADDING and Y_PADDING are how much spacing you want around the label's text. xLoc and yLoc are variables you're using to keep track of the x and y position you want to put the labels at. You'll probably increase xLoc based on textSize + X_PADDING + LABEL_SPACING or something (where you define LABEL_SPACING):
I hope this helps get you started.