set value for a label without connection using code - ios

how to set value for uilabel using objective-c without any connection from storyboard to source-file.
In a storyboard, suppose a label is dropped into uiview and we don't want to bound connection to .h file , so how is it possible to set value using objective-c.

You can create UILabel Programatically like,
UILabel *myLabel = [[UILabel alloc]initWithFrame:CGRectMake(91, 15, 0, 0)];
myLabel.text = #"hello";
[self.view addSybview:myLabel];
Second thing you can set tag from storyboard and by this tag you can access label.
Third thing you can get NSArray of subviews of self.view means your main view and from that array you can access that label.
these are the possible ways. :)
hope this will help :)

You can assign a tag value to your UILabel from the storyboard, then you can use that value to access the view from your code:
UILabel *label = (UILabel *)[self viewWithTag:1];
[label setText:#"setting some value"];

Related

Xcode / iOS / iPhone How to set event to UILabel

Please give me an advice.
I create UILabels programmatically (dynamic).
Is there is a chance to add Event to them?
What I want by steps:
I create UILabel;
I set Event to it; (NSNotification?)
When I do some action (rotate, for example) I want that Label is changed or removed. An extended example: I create Labels and when I rotate device I want that part of them (which with attached Events) disappear in animation.
I create a lot of Labels, so I can't just set them global variables. And I can't set them tags unlimited. So UILabel *label = (UILabel*)[self.view viewWithTag:labelCount not a solution. Getting element by 'viewWithTag' has one more trouble - when set animation to that element and that element already in animation happens collision - they plays one over other...
I create Labels like this:
CGRect *labelFrame = CGRectMake(left, top, width, height);
UILabel *label = [[UILabel alloc] initWithFrame:labelFrame];
label.text = #"Hi, I'm one of these army of labels";
[self.view addSubview:label];
PS: Sorry for English.
I assume you have a UIViewController that has a bunch of labels. I would recommend an IBOutletConnection for storing a reference to all of your labels (assuming storyboard).
//You will need to connect all of these labels through Interface Builder.
#property (nonatomic, strong) IBOutletCollection(UILabel) NSArray *labels;
In one of the following rotation methods (Detect rotation changes in iOS) do your rearranging.
//Called whenever orientation changes
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
for (UILabel *label in self.labels) {
//Make each label disappear here.
}
}

UIImage on UILabel

I have a UILabel on a UITableViewCell which I am reusing at multiple places. It will hold text/statements of type NSString.
Question: For just one cell, I want an image before the text starts. Is it possible to place/display UIImage before text in iOS on UILabel. I can definitely place an image directly on the cell before the label, but I wish to avoid playing around with the constraints if it affects the other reusing cells.
Yes. UILabel is a subclass of UIView. Therefore, you can add anything derived from UIView to UILabel.
Below code is an example I show you.
UILabel *label = [UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 20)];
UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"bear.png"]];
imgView.frame = CGRectMake(0, 0, 10, 10);
[label addSubview:imgView];
I think that making a custom view which has any features you want is better than using UILabel.
You can do this with NSTextAttachment class available from iOS 7.Here is the easy answer for this
Not in the UILabel but you can definitively do it in the Cell, you need to create a custom class that inherits from UITableViewCell.
In there define your cell at your better convenience

How to create UITextField with placeholder to the left and edited text to the right?

How to create UITextField with placeholder to the left and edited text to the right?
UITextField itself does not allow using its properties to put placeholder and edited text to different sides. Try to set it in the storyboard -> attributes inspector -> alignment. They are both to the left or both the right. There is no special alignment only for placeholder or only for the text.
To do this you should add a UILabel to the UITextField which is not editable and doesn't allow touches. Then set the text alignment on the UITextField to NSTextAlignmentRight.
First, set the text alignment to the right:
[textField setTextAlignment:NSTextAlignmentRight];
Fir the placeholder, you can add it as a subview, or use the leftView property:
[textField setLeftView:<your label here>];
I just created a custom UITextField subclass that does this. To use it simply create your UITextField on the Storyboard or Xib... put a placeholder text in there, set the text fields class as OBSTextFieldWithLabel and then you can use the following code:
#interface OBSTextFieldWithLabel : UITextField
#end
#implementation OBSTextFieldWithLabel
- (void)awakeFromNib
{
[super awakeFromNib];
NSString *labelText = [self placeholder];
self.placeholder = nil;
CGRect labelFrame = CGRectMake(20, 10, 150, 15);
UILabel *placeholderLabel = [[UILabel alloc] initWithFrame:labelFrame];
[placeholderLabel setFont:[UIFont systemFontOfSize:12]];
[placeholderLabel setTextColor:[UIColor lightGrayColor]];
[placeholderLabel setText:labelText];
[self addSubview:placeholderLabel];
[self setTextAlignment:NSTextAlignmentRight];
}
#end
This is probably not 100% perfect as the label size is not dynamically set and all...but it should get you up and running and it works :-)
I've written a control that might help you. Essentially it's intended for highlighting a UITextField while it is being edited, but it also has a mechanism for a placeholder on the left, as you can see in the screenshots.
https://www.cocoacontrols.com/controls/sahighlightedtextfield
Hope this points you in the right direction.

How to put UIlabel on UIImageView programatically

I'm trying to put a UIlabel on a UIImageView which is in a UIScrollView. How can I accomplish this?
imageView is a UIView, so you can add subviews to it.
also make sure you set the label with an appropriate frame
You can do:
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,0,100,20)]; //or whatever size you need
[imageView addSubview:myLabel];
You can add it directly by dragging Label from to your storyboard/xib if you want it to e static. But if you want it to dynamically generate at runtime then you should add it by using
UILabel *yourlabel = [[UILabel alloc]initWithFrame:CGRectMake(YOUR COORDINATES)];
then you can add it to any of your view.For your imageview you can add it as
[imageview addSubview:yourlabel];
Often you may require to remove it from that view if not needed. You can similarly call removeFromSuperView also.
Hope this works :)

property frameOrigin not found on object of type UILabel

I'm new into ios programming so this is probably a common misuse of techniques.
Inside my view controller I use the following code:
[timeLabel setTextColor:textColor];
[timeLabel setText:[NSDateFormatter localizedStringFromDate:_reservation.date dateStyle:NSDateFormatterNoStyle timeStyle:NSDateFormatterShortStyle]];
[timeLabel sizeToFit];
[timeLabel setFrameOrigin:CGPointMake(61, 23)];
where timeLabel is declared in header file as
UILabel *timeLabel;
along with other constants (textColor etc)
I get the following error:
Property frameOrigin not found on object of type UILabel
What am I missing? Do I need to append another frame onto each of labels (I am going to use the labels and pictograms in an UIButton view inside a table cell).
Thank you for help in advance!
You can do like this:
[timeLabel setFrame:CGRectMake(61,23,timeLabel.frame.size.width,timeLabel.frame.size.height)];
Or
CGRect frame=timeLabel.frame;
frame.origin=CGPointMake(61,23);
timeLabel.frame=frame;

Resources