UIScroll 及其嵌套元素
我创建了一个 UIScrollView。 我设置了尺寸,然后尝试添加 UILabels。
然而,标签都是白色文本(很烦人,因为我必须更改每个标签的属性)。
有没有办法使所有标签(从IB拖动到视图的新标签)具有默认的黑色文本颜色?
编辑以匹配评论
我想尽可能多地使用 IB。因此我想将 UILabel 从 Library 调色板拖到 UIView 中。当我这样做时,UILabel 设置为白色(默认)。我希望默认颜色为黑色。我知道我可以通过编程来做到这一点,但我试图避免这种情况,除非我真的需要这样做。
I created a UIScrollView.
I set up the dimensions and then I am trying to add UILabels.
However the labels are all white text (annoying because I have to change the property per label).
Is there a way to make all labels (new ones that are dragged from IB to the view) have a default text color of black?
Edited to match comments
I want to use IB as much as I can. Therefore I want to drag UILabel from the Library palette to the UIView. When I do this, the UILabel is set to white (default). I want the default color to be Black. I know I can do this programatically but I am trying to avoid that unless I really really need to.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
没有简单的方法可以完全满足您的要求。但是您可以做的是创建一个具有所需属性的标签,将其存储在绘图板上的某个位置而不是视图中,然后每次需要新标签时复制它,而不是拖动新标签。您可以使用选项+拖动轻松复制。
There's no easy way to do exactly what you want. But what you can do is create a label with the properties you want, store it somewhere on the drawing board but not in the view, then duplicate it each time you want a new label instead of dragging on a new one. You can duplicate easily using option+drag.
我认为简短的答案是“不,没有一种简单的方法可以做到你所描述的事情。”
我能想到的最简单的方法是创建所有 UILabel(默认设置为白色文本),然后按住 Control 键单击它们并一次性设置它们的文本颜色 - 所有其他方法都不太方便,或者基本上会要求 Apple 开源 Xcode 或 UIKit,以便我们能够了解其内部结构。
I think the short answer is "no, there's not an easy way to do what you're describing."
The easiest way I can think of would be to create all your UILabels (with the default setting of white text), then control-click them all and set their text color all at once – all the other ways are less convenient, or would essentially require that Apple open-source Xcode or UIKit so that we can get at their internals.
是的,有办法。您可以循环目标视图的子视图,例如:
Yes, there is a way. You could loop the subviews of the target view such as:
为什么标签必须来自对象库?您可以通过从库中仅拖动一个 UILable 到您的视图来获得您想要的功能,将所有属性设置为您想要的默认值,然后点击复制(command+c)一次。现在,您可以根据需要多次粘贴(command+v)具有特殊属性值的 UILabel,IBActions 和出口也将保留在副本中。
如果您计划调整比字体颜色和大小更复杂的属性,那么我建议您采用一种更自定义的方法,在 IB 中进行批量拖放工作之前只需要最少的编码。
在 Xcode 中子类化 UILable,在一个简单的返回方法中仅设置一次所有属性,然后从“init”和“awakeFromNib”调用此方法。现在返回 IB 并执行所有拖/放操作,确保标签是你的子类。
然而,我认为,如果你经常这样做,特别是如果你将来再次做类似的事情,你将节省大量的时间和精力在代码中实现这个“标签工厂”。它的代码可能比您想象的要少,而且更棒的是您可以在下一个应用程序中重用它。无论如何,这就是我的 2 美分,祝你好运
why do the labels have to come from the object library? You could get the functionality that you want by dragging only one UILable from the library to your view set all the properties to the defaults that you want and hit copy(command+c) once. Now you can paste(command+v) your UILabel with the special property values as many times as you want, IBActions and outlets will also be retained in the copys.
If you plan to tweak more involved properties than font color and size, then I would suggest a more custom approach that will require only minimul coding before you do the bulk drag and drop work in IB.
Subclass a UILable in Xcode, set all of your properties just once in a simple return method and than call this method from both "init" and "awakeFromNib" Now go back to IB and do all your drag/dropping making sure that the labels are of your subclass.
However, it is my opinion that if you are doing this a lot, especially if you will be doing something similar again in the future, you will save a substantial amount of time and energy to implement this "label factory" in code. Its likely less code than you are imagining it will be and the kicker is that you can reuse it in the next app. anyway thats my 2cents, Good Luck