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.textAlignment = 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];
}
Related
I'm trying to change the color of NavigationBars title while I'm on a viewcontroller (not before pushing it). by using:
[[[self navigationController] navigationBar] setTitleTextAttributes:textAttributes];
But this line of code only works before pushes or pops. I was wondering if there is a way to force this without navigating?
I would say the simplest way is to create an UILabel with the same style you want for the UINavigationController title and set the label to the navigationItem.titleView.
Try this one
UILabel * label = [[[UILabel alloc] initWithFrame:CGRectMake(0,0,45,45)] autorelease];
label.textColor = [UIColor whiteColor];
label.backgroundColor = [UIColor clearColor];
label.text = self.navigationItem.title;
self.navigationItem.titleView = label;
create custom label and set it as titleView of your navigation bar
I am trying to wrap my section header and UILineBreakModeWordWrap is not helping. Any idea what I am doing wrong?
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *rOView = [[UIView alloc] initWithFrame:CGRectMake(10,0,300,60)] ;
UILabel *sectionLabel = [[UILabel alloc] initWithFrame:CGRectZero];
sectionLabel.backgroundColor = [UIColor clearColor];
sectionLabel.font = [UIFont boldSystemFontOfSize:18];
sectionLabel.frame = CGRectMake(70,18,200,20);
sectionLabel.text = #"A really really long text A really really long text A really really long text";
sectionLabel.textColor = [UIColor blueColor];
sectionLabel.numberOfLines = 0;
sectionLabel.lineBreakMode = UILineBreakModeWordWrap;
[roView addSubview:sectionLabel];
return roView;
}
You've not given the label a meaningful frame - only CGRectZero. After setting up your label, call -sizeToFit on it, like so:
UILabel *sectionLabel = [[UILabel alloc] initWithFrame:CGRectZero];
sectionLabel.backgroundColor = [UIColor clearColor];
sectionLabel.font = [UIFont boldSystemFontOfSize:18];
sectionLabel.frame = CGRectMake(70,18,200,20);
sectionLabel.text = #"A really really long text A really really long text A really really long text";
sectionLabel.textColor = [UIColor blueColor];
sectionLabel.numberOfLines = 0;
sectionLabel.lineBreakMode = UILineBreakModeWordWrap;
[sectionLabel sizeToFit];
Edit: I see now that you did actually set the label frame. But the height is too short to display all the text.
I have the following code in my XCode project for an iOS app I'm developing:
testLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,0, 320, 480)];
UIFont *Font = [UIFont fontWithName:#"Arial" size:40];
[testLabel setFont:Font];
[testLabel setTextAlignment:UITextAlignmentCenter];
[testLabel setTextColor:[UIColor colorWithRed:(float) 55/255 green:(float) 41/255 blue:(float) 133/255 alpha:1.0]];
testLabel.text = #"Here We Go";
I am looking to put an image in that spot instead of the text. What do I need to replace this code with?
Either you make an image and put it in an UIImageView or you make a UIView subclass in which you will draw the text inside the drawRect method.
In the second case, in your drawRect you do this :
[self.yourStringProperty drawAtPoint:CGPointMake(100,150) withFont:[UIFont systemFontOfSize:14.0]];
or
[self.yourStringProperty drawInRect:rect withFont:[UIFont systemFontOfSize:14.0]];
Also, look HERE for a detailed explanation of these functions which can also take into account the available width, minimum sizes, line breaks, etc.
The answer above mine is the best with the second part: use a UIView and put either your label or a UIImageView inside it depending on what you want. Here's what it would look like with the image:
UIView *container = [[UIView alloc] initWithFrame:CGRectMake(<<your image frame here>>)];
UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"yourImage.png"]];
image.frame = CGRectMake(<<your image frame here>>);
[container addSubview:image];
[self.view addSubview:container];
I am trying to create a view in iOS to let the user know their data is loading... it should have about a 200x200 Rounded-corner box in the middle with a spinner and the words "Data Loading..." and a transparent background.
This all is working except my 200x200 rounded-corner box is also transparent.
Here is the code I am using:
UIView *loadingDataView = [[UIView alloc] initWithFrame:CGRectMake(0, 44, 320, 367)];
loadingDataView.alpha = 0.4;
loadingDataView.backgroundColor = [UIColor clearColor];
UIView *viewWithSpinner = [[UIView alloc] initWithFrame:CGRectMake(110, 106, 100, 100)];
[viewWithSpinner.layer setCornerRadius:15.0f];
viewWithSpinner.backgroundColor = [UIColor blackColor];
UILabel *msg = [[UILabel alloc] initWithFrame:CGRectMake(5, 75, 90, 20)];
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(5, 5, 90, 70)];
spinner.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhite;
[spinner startAnimating];
msg.text = #"Data Loading";
msg.font = [UIFont systemFontOfSize:14];
msg.textAlignment = UITextAlignmentCenter;
msg.textColor = [UIColor whiteColor];
msg.backgroundColor = [UIColor clearColor];
viewWithSpinner.opaque = NO;
viewWithSpinner.backgroundColor = [UIColor blackColor];
[viewWithSpinner addSubview:spinner];
[viewWithSpinner addSubview:msg];
[loadingDataView addSubview:viewWithSpinner];
[self.view addSubview:loadingDataView];
Thanks.
No exactly an answer to your question, but check out the open source MBProgressHUD. It aims to be an open source replacement for the private UIProgressHUD class, and is exactly what you're trying to do.
The solution for your problem is that you should remove the alpha attribute of 0.4, that's what's turning your round view transparent, if your view is set to clearColor (this is not really a color, it just makes the view transparent) it makes no sense to add an alpha of 0.4. If what you want is a semi-transparent view surrounding your black rounded view, you should do the following:
[loadingDataView setBackgroundColor:[UIColor colorWithRed:1 green:1 blue:1 andAlpha:0.4]];
That will give you something whiteish kinda grayish, that should work.
However, I would recommend you to use the GIDAAlertView Class I developed, you can get the source and an example app on my GitHub:
It takes about 3 lines to get it working:
GIDAAlertView *spinnerAlert=[[GIDAAlertView alloc] initAlertWithSpinnerAndMessage:#"GIDAAlertView Spinner"];
//Show it ...
[spinnerAlert presentAlertWithSpinner];
//Later in your code, hide it
[spinnerAlert hideAlertWithSpinner];
This is how it looks like.
you are telling it to be clear...
loadingDataView.backgroundColor = [UIColor clearColor];
What do you want it to be black?
I'm trying for the past several hours to figure out how to do this, but with no luck. There are several potential solutions for this when searching Google, but nothing seems to work.
I'm trying to customize the background color of the standard UILabel that goes in a UITableViewCell (since I already customized the background color of the cell itself), but nothing I do seems to work.
I'm creating my own UILabel to customize the colors in -tableView:cellForRowAtIndexPath:
UILabel* label = [[[UILabel alloc] init] autorelease];
label.textColor = [UIColor blueColor];
label.backgroundColor = [UIColor redColor];
label.opaque = YES;
[cell.contentView addSubview:label];
cell.text = #"Sample text here";
But that doesn't work, and the resulting table view still has a bunch of cells with labels with black text and white background in it.
Any clues on what I am doing wrong here?
UPDATE: If I try to do the following instead:
UILabel* label = [[[UILabel alloc] init] autorelease];
label.textColor = [UIColor blueColor];
label.backgroundColor = [UIColor redColor];
label.opaque = YES;
[cell.contentView addSubview:label];
label.text = #"Sample text here";
I get a bunch of UITableViewCells with no text at all.
It appears that you're assigning the text to the cell instead of the label. You probably want:
label.text = #"Sample text here";
Also, you'll need to set the label's frame to what you require:
label.frame = CGRectMake(10,10, 80, 40);
or directly in the constructor:
label = [[UILabel alloc] initWithFrame:myFrame];
I wouldn't do this in code. I would do it with a custom XIB file and then load it in your
- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath
delegate function. That way you can design the XIB file to your exact specs, and tweak it.
Good luck!
You have asked this question before, and received correct answers;
How to customize the background color of a UITableViewCell?