开发者

A More Concise Way to do This in Objective C?

So I have something like this in my code:

    leftMostLabel = [[UILabel alloc] initWithFrame:CGRectMake(60.0f, 2.0f, 80.0f, 40.0f)];
    leftMostLabel.text = @"Some text";
    leftMostLabel.textColor = [UIColor colorWithWhite:1.0 alpha:1.0];
    leftMostLabel.backgroundColor = [UIColor clearColor];
    leftMostLabel.te开发者_StackOverflow社区xtAlignment = UITextAlignmentCenter;
    leftMostLabel.font = [UIFont boldSystemFontOfSize:13.0];
    leftMostLabel.userInteractionEnabled = NO;

    centerLeftLabel = [[UILabel alloc] initWithFrame:CGRectMake(115.0f, 2.0f, 80.0f, 40.0f)];
    centerLeftLabel.text = currentDate;
    centerLeftLabel.textColor = [UIColor colorWithWhite:1.0 alpha:1.0];
    centerLeftLabel.backgroundColor = [UIColor clearColor];
    centerLeftLabel.textAlignment = UITextAlignmentCenter;
    centerLeftLabel.font = [UIFont systemFontOfSize:12.0];
    centerLeftLabel.userInteractionEnabled = NO;

Which is working fine and dandy, but I repeat that format many many times. I would ideally like to pass in the variable(leftMostLabel, centerLeftLabel, etc.) to a function which creates the labels for me behind the scenes based on the name of the variable that I pass to it, as well as whichever additional parameters I feed it. This possible? If so, how would I go about this?

Thanks in advance!


Objective-C offers the possibility to extend existing classes via categories, so you could easily add a method to UILabel such as: +labelWithFrame:text:textColor:backgroundColor:alignment:font:interaction:. That'd shorten things a bit, so your code would look like:

UILabel *leftLabel = [UILabel labelWithFrame:CGRectMake(...)
                                        text:@"Yoo hoo!"
                                   textColor:[UIColor blueColor]
                             backgroundColor:[UIColor clearColor]
                               textAlignment:UITextAlignmentLeft
                                        font:[UIFont systemFontOfSize:12];
                      userInteractionEnabled:NO];

That's somewhat less cluttered looking and might be worthwhile.

If most of your labels are similar, another option would be to add a method that creates a label using a prototype. UIView's don't implement NSCopying, so you can't just make a copy of a label using the -copy method, but you can write a method that creates a new label and configures it based on an existing label. Calling it might look like:

UILabel *centerLabel = [UILabel labelWithFrame:CGRectMake(...)
                                     likeLabel:leftLabel];
centerLabel.text = @"Hey there!";

Finally, consider using Interface Builder to create all these labels. Cocoa and Cocoa Touch code would be chock full of calls to view configuration methods like -initWithFrame: and -addSubview: if it weren't for IB. It may or may not be appropriate in your case, but sometimes people avoid IB because they think it's more work or makes their code more difficult to manage; I think the opposite is true.


Concise, there's a word you don't see too often in conjunction with Objective-C. Coming from another language I feel your pain with snippets like these.

I've resorted to writing the necessary label variations once, and then repeat them many times over. The other posted answers are all viable options, though I'm not so sure about the concise argument. @Caleb's 'likeLabel' answer is the most versatile, but you still need to be able to reference that other label.

I make a project-specific category on UILabel, and place the various labels in there my self. The only thing I'm comfortable repeating for every label is the frame and the text. There's not really a neccessity to put +methods in a category, but UILabel does sum up what you want quite nicely. After having the categories in place, this is how you'd use it:

UILabel *someLabel = [UILabel headerLabelWithFrame:CGRectMake(10, 10, 256, 32) andText:@"Conciseness"];

And this is what the category would look like with just one label type. You'd add more if needed:

@interface UILabel (ProjectSpecificLabels)
+(UILabel *)headerLabelWithFrame:(CGRect)frame andText:(NSString *)text;
@end

@implementation UILabel (ProjectSpecificLabels)
+(UILabel *)headerLabelWithFrame:(CGRect)frame andText:(NSString *)text {
  UILabel *label = [[[self alloc] initWithFrame:frame] autorelease];
  label.text = text;
  label.textColor = [UIColor colorWithWhite:1.0 alpha:1.0];
  label.backgroundColor = [UIColor clearColor];
  label.textAlignment = UITextAlignmentCenter;
  label.font = [UIFont boldSystemFontOfSize:13.0];
  label.userInteractionEnabled = NO;
  return label;
}
@end

Hope it helps.
Cheers, EP.


I would break it up into a function that sets my values.

leftMostLabel = [[UILabel alloc] initWithFrame:CGRectMake(60.0f, 2.0f, 80.0f, 40.0f)];
leftMostLabel.text = @"Some text";
[self initLabelValues:leftMostLabel withFont:[UIFont boldSystemFontOfSize:13.0]];

centerLeftLabel = [[UILabel alloc] initWithFrame:CGRectMake(115.0f, 2.0f, 80.0f, 40.0f)];
centerLeftLabel.text = currentDate;
[self initLabelValues:centerLeftLabel withFont:[UIFont boldSystemFontOfSize:12.0]];

-(void) initLabelValues:(UILabel*)inLabel withFont:(UIFont*)font {

    [inLabel setTextColor:[UIColor colorWithWhite:1.0 alpha:1.0]];
    [inLabel setBackgroundColor:[UIColor clearColor]];
    [inLabel setTextAlignment:UITextAlignmentCenter];
    [inLabel setUserInteractionEnabled:NO];

    [inLabel setFont:font];
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜