Wondering if anyone know hows to complete the following...
In a Storyboard I set my Labels with dummy text, they are not Static text. For example 'User1 Username'.
What I would to know if there is a setting to clear the value of this label when it is loaded by the view. I have some other code that runs off and collects the relevant information. However, it might take a few seconds so a HUD is shown to the user whilst it loads.
Of course in the background of the HUD you can see the example text shown. I know in viewDidLoad I could simply clear all the label texts setting them back to #"", but is there no setting in the storyboard or anything for this?
You can use User Defined Runtime Attributes to achieve this. Simply set "text" attribute for any UILabel, UITextField or UITextView to get what you want:
KVC and runtime attributes are really powerfull when working with Storyboard (e.g. did you know that you can set "layer.cornerRadius" attribute to any UIView to get rounded corners?).
Unfortunately, there is no other way to do it.
You would have to set the text of the UILabel manually in the ViewDidLoad method:
- (void)viewDidLoad
{
[super viewDidLoad];
self.myLabel.text = #"";
}
Related
I have this issue with a custom UIView where I have a UIButton subview, I want to set the button's text on initialization based on some condition like this:
- (void)awakeFromNib {
[super awakeFromNib];
//check for some conditions
self.testButton.titleLabel.text=#"Some Title";
}
Nothing happens and the button's text is the same as defined in the nib file, however if I change the implementation to:
- (void)awakeFromNib {
[super awakeFromNib];
//check for some conditions
[self.testButton setTitle:#"Some Title" forState:UIControlStateNormal];
}
It works as expected.
Can somebody please explain to me the difference between the two approaches? and when to use each?
EDIT:
the suggested answer doesn't explain my situation, I tested changing the button's text from another button's action like this:
- (IBAction)otherButtonClicked:(id)sender {
self.testButton.titleLabel.text=#"Some Title";
}
and the button's text changed. I just want to understand that behaviour.
Exact answer is
titleLabel
Do not use the label object to set the text color or the shadow color. Instead, use the setTitleColor:forState: and setTitleShadowColor:forState: methods of this class to make those changes.
The titleLabel property returns a value even if the button has not
been displayed yet. The value of the property is nil for system
buttons.
setTitle
Use this method to set the title for the button. The title you specify
derives its formatting from the button’s associated label object. If
you set both a title and an attributed title for the button, the
button prefers the use of the attributed title over this one.
At a minimum, you should set the value for the normal state. If a
title is not specified for a state, the default behavior is to use the
title associated with the UIControlStateNormal state. If the value for
UIControlStateNormal is not set, then the property defaults to a
system value.
Title label access is given to adjust other properties of the label such as font but setting titleLabel text does not work.
It is because UIButton class has inner implementation to set the text based on different states of the button like selected/highlighted etc which overrides label text.
The accepted answer is correct, I just add this here to elaborate a bit (comments are too short)
There's not much to explain here. The title is simply not meant to set the text at all. My guess is that the internal workings of UIButton make it save the text somewhere else as well (for different states). It uses a normal UILabel to eventually display that, because that's convenient and easy to understand. Setting the text of that does not change the button in all cases, which probably ultimately depends on the drawing cycle. I assume when it's drawn, laid out, or the like it "replaces" the label's text with its other saved variant at some point.
Now you might wonder why Apple did then expose the UILabel and thus seemed to make the text editable then.
Legacy is probably one aspect of this decision (IIRC you could once set the button's title that way). Although old code doesn't result in the desired behavior, it at least didn't crash immediately. Also, a lot of code simply wants to get the text and expects a label, that works perfectly fine as ever.
Second, designing it totally different seems overkill. To avoid that, they would have to use a subclass of UILabel which prevents you from setting the text or something and use that. Or skip it (and thus legacy support) completely and only offer the setTitle:forState: method. That seems like a bit much for a simple text container like a Label.
Ultimately it's a design choice made by Apple. You can't set the title text directly and there's no case in which you should do it any way other than by using setTitle:forState:
Within my app, the title for each navigation controller is set in either loadView or viewDidLoad
When the view controller is displayed, the title is always truncated - even though the content is not very long ("lessons" in this case).
I'm using a custom font - could that case this?
I'm also using PixateFreestyle though I don't know of any bugs with that.
Please tell me there's a solution to this that isn't creating a custom titleView label?
Thanks in advance
Try calling sizeToFit on the UILabel custom fonts shouldn't be an issue.
e.g.
#label = UILabel.alloc.initWithFrame(CGRectZero)
#label.text = "A very long string that shouldn't get truncated"
#label.sizeToFit
Alternatively, #label.adjustsFontSizeToFitWidth = true. That way, if the title is excessively long, the font will be resized in order to allow it to fit. I've never tried this with Pixate, but it works in plain vanilla iOS.
I normally post source code and examples of UI but I think its more related to an iOS7 change and I cant see it being a code bug (yet). I would have to post so much code and UI that it would be counter productive. So here is my best non-visual description:
Since upgrading a project to iOS7 I am finding that if I put call to change a UILabel or calling setText of a UIButton in a ViewDidAppear or ViewWillAppear it puts the new text right on top of the old text.
Since developing for iOS I have never had to do anything different. If I do this:
lblMyHours.text = #"12";
It shouldnt just throw that on top of my existing label.
This especially happens inside of a UITableView where I have created an iVar for a UILabel thats in a UITableViewCell. If a user makes an adjustment to a value after clicking on a cell (it takes them to a detail screen to edit), when I pop back I have it recalculate in ViewDidAppear. In that recalculating I am resetting a label like the above. But it doesnt clear out the old.
You might be adding a new label every time you return a new cell.
You should just replace the text of the current label instead.
There are two way to achieve this, one you are already doing and as #Guilherme rightly pointed out. The other way is to create a custom tableview cell and put the UILabel property in there. As for the viewDidAppear scenario, you can create a uilabel in ther .h file (in the #interface declaration) and then initialize it in ViewDidLoad method and simple use in ViewDidAppear method without having to declare it again.
I would suggest that you follow the way I described for ViewDidAppear issue, and for the UITableView issue, search for UILabels in subview of the cell everytime cellForRowAtIndex method is called and remove it from the subview, something like this before adding a new label
for(UIView *view in cell.subviews)
{
if([view isKindOfClass:[UILabel class]])
{
[view removeFromSuperview];
}
}
Hope this helps.
I want to make UITextview like in the Facebook comment view. How can I do it without using any external libraries?
Here is example image:
First, set the appearance of your UITextField to Alert to get the black keyboard.
Then, you only need two more views :
a black one for the background
a white one which will be the background of your text field, which you programmatically apply the following code to (don't forget to #import <QuartzCore/QuartzCore.h> first):
view.layer.cornerRadius = 10;
i have get sample code for this - here it is YIPopupTextView
What they do is use an image, that's it! They also probably expand the size of the UITextfield to make it fit the entire screen but yeah it's pretty easy. If you go into Interface Builder you can see a thing that says image. If you want to do this programmatically there is a value is textfield.background (A UIImage).
If you don't want to create an image you can create a custom UITextField subclass and use the -(void) drawInRect:(CGRect)rect function, and draw it with Quartz, and in interface builder set the UITextField class to your textfield.
I added a UILabel subview using code. When the user rotates the iPad, I want to be able to tell that UILabel to shift its position to accomodate the new screen orientation.
I think I need to put my code in the willAnimateRotationToInterfaceOrientation function.
I want to be able to get at my UILabel and I see sample code that looks like the following to retrieve a reference to a subview:
UILabel *myLabel = (UILabel *) [self.subviews objectAtIndex:0];
How do I know what index my UILabel is at? The sample code seems to know that their subview is the first one (index 0).
Is there a way for me to find out what my UILabel's index is?
Is there another way to keep a reference to this UILabel that I create in code? If I made myLabel a variable of the class, will I be able to reference
Generally, in your UIViewController, you'd have an instance variable that was a pointer to your UILabel. Then you can access it anywhere in your controller's code.
Either have it in an instance variable (I like this, because it allows you to give nice names to your views), or set its tag property so you can find it again via [self.view viewWithTag:myTag] (the tag is just a number you assign to the view for exactly the purpose of finding it later).
Yet another possibility is not to do this, but instead set an autoresizingMask so it (for example) sticks to the upper left corner.