开发者

UIScroll and its nested elements

开发者 https://www.devze.com 2023-03-31 15:42 出处:网络
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).

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 u开发者_开发百科se 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.


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.


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:

UIView * targetView;

[...]

for(id subView in targetView.subViews){
    if([subView isKindOfClass:[UILabel class]]){
        [subView setBackgroundColor:clearColor];
    }
}


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

0

精彩评论

暂无评论...
验证码 换一张
取 消