How to put UIlabel on UIImageView programatically - ios

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 :)

Related

set value for a label without connection using code

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"];

How to add a UILabel inside a UIScrollView programatically?

Ok i have already gone through similar questions, but none of them helped.
I want to add a UILabel inside a UIScrollView so that the Label can be scrolled if the contents are large. Here is my code:
ViewController.h:
#interface ViewController : UIViewController
{
UILabel *myLabel;
UIScrollView *myScroll;
}
ViewController.h:
myLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y+30, self.view.frame.size.width, self.view.frame.size.height)];
myScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y+30, self.view.frame.size.width, self.view.frame.size.height)];
myLabel.backgroundColor = [UIColor yellowColor];
myLabel.text = #"Large random text";
[myLabel setNumberOfLines:0];
myLabel.lineBreakMode = NSLineBreakByWordWrapping;
[myLabel sizeToFit];
myScroll.contentSize = CGSizeMake(myLabel.frame.size.width,
myLabel.frame.size.height);
[myScroll addSubview:myLabel];
[self.view addSubview:myScroll];
I searched a lot on the internet but could not find a answer, can someone let me know what the issue is ?
Thank You !
I'm not sure how to do that, but I would like to suggest an alternative that may suit your needs. Use a UITextView, but set " [textView userInteractionEnabled:NO] " and it will act as a label, since it cannot be edited. It might end up looking like how you want.
Based on your comments, you need to size the label as needed so it is big enough for the given text. Then add the label to scroll view. Then set the scroll view's contentSize so it fits the whole label. This will ensure the scroll view allows you to scroll to see the whole label.
In addition to this, make sure the label's frame is relative to the scroll view and not to self.view. In other words, setting the label's frame's origin to 0,0 will put the label in the top left corner of the scroll view regardless of the position of the scroll view relative to its parent.
Try by replacing your contentSize like below code,
myScroll.contentSize = CGSizeMake(self.view.frame.size.width,self.view.frame.size.height + 100);
Hope it will work for u.
1.You have to use View(ContentView) to place the labels.
2.Add the ContentView into ScrollVIew.
3.Assign the ContentView Size whatever You Want
Go Through This Link,You Will get a Clear idea.
https://www.youtube.com/watch?v=UnQsFlMGDsI
Remove scrollView = [[UIScrollView alloc] init]; this is not necessary.Please Upvote if it helps.

In UIKit, how can I add a UILabel as a subview to a UITextField so that is positioned exactly where the text will be?

I want to add a UILabel as a subview to a UITextField. It should to be positioned at the exact same coordinates as the normal text contents. (I am aware of UITextField.placeholder, but it does not fit my requirements.)
I have created a class that inherits from UITextField and assigned it to a text field in a storyboard. I have added the following in the initWithCoder method:
placeholderLabel = [[UILabel alloc] init];
// set font, color, text
[placeholderLabel sizeToFit];
// This rect is useless to me :(
CGRect frame = [self textRectForBounds:placeholderLabel.bounds];
[self addSubview:placeholderLabel];
I could use the initWithFrame method, instead of init, or set the UILabel's frame property but then I would need to know a GCRect. Do I really need to calculate it by hand based on the UITextField's height, paddings, font size, and possible left view etc?

Drawing line in UITableViewCell (without using drawRect)

I have a UITableViewCell in which I would like to draw some lines (say a 2pt line at 1/3rd from top of the TableCell covering the whole width). The lines would always be in the same place within the tableview cell.
One straightforward solution is to just have a -drawRect method that will use CGContextStrokePath to draw the line. But that seems to be like an overkill since it would call drawRect everytime which is inefficient.
I would think that there would be a way to be able to cache it somehow, so that all tableview cells are created with the lines by default. One way I can think of is to create 2pt*2pt png file and add it to a UIImageView on top of the tableview cell. Any other ways?
Try this -
Add this code in your cellForRowAtIndexPath, just adjust y position og this image view-
UIImageView *seperatedImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 49, 320, 1)];
seperatedImageView.backgroundColor = [UIColor colorWithRed:201.0/255.0 green:250.0/255.0 blue:152.0/255.0 alpha:1.0];
[cellBackView addSubview:seperatedImageView];
Another option is, to add a UIView and set its background color. For example:
UIView *lineView = [[[UIView alloc] initWithFrame:CGRectMake(0,0,cell.contentView.bounds.size.width, 2)] autorelease];
lineView.backgroundColor = [UIColor grayColor];
[cell.contentView addSubview:lineView];
You can just add a UIView like a subView of your UITableViewCell.

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

Resources