How do you add a UILabel to a UIScrollView? - ios

I'm trying to add a UILabel to a UIScrollView. I followed the steps in this tutorial. But for some reason, the label is not showing on the scrollView when I run the app. The app consists of a storyboard with two files (ViewController.h and ViewController.m). In the storyboard there's only one view controller which is a custom class of ViewController. Below is my code which is in the ViewDidLoad method of ViewController.m.
UILabel *testLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 150.0, 250.0)];
testLabel.numberOfLines = 0;
[testLabel sizeToFit];
testLabel.text = #"test";
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 568.0)];
[scrollView addSubview:testLabel];
I tried the following lines of code at the end of my current, but still doesn't help.
[testLabel release];
[scrollView release];

You are calling [testLabel sizeToFit] before setting the text. This is setting the size of the label's frame to 0, 0.
Call sizeToFit after setting the text.

Make sure the view adds the scrollView as a subview too.
And a word to your fixes. Since Objective-C supports ARC (Automatic Reference Counting) the call "release" is not needed anymore.

Related

UITableView footer view issue in iOS 10 due to autoresizingMask

I am using the following code for adding table footer view with a label.
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 60)];
view.autoresizingMask = self.view.autoresizingMask;
UILabel *label = [[UILabel alloc] initWithFrame:view.frame];
label.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
label.text = #"The End";
label.textAlignment = NSTextAlignmentCenter;
[view addSubview:label];
self.tableView.tableFooterView = view;
It was working fine with iOS version less than iOS 10.
Only on iOS 10 I footer view goes outside of the frame and is getting displayed only if we pull up the tableview out of its scrolling bounds.
And on releasing the tableview again the footer view goes out of view.
Can anyone please let me know whats happening here in iOS 10, Do we need to change the approach shown above.
But I found that by removing the following line of code it works fine.
view.autoresizingMask = self.view.autoresizingMask;
Could anyone explain why this happens ?

Adding a UILabel as a subview on top of a UITextView

I am trying to make a fake placeholder for a UITextView with a UILabel. Let's say there's a UITextView inside ViewController and this is what I do:
CGFloat frameWidth = self.postInputField.frame.size.width;
textViewPlaceholder = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, frameWidth, 200)];
textViewPlaceholder.text = POST_PLACEHOLDER;
textViewPlaceholder.textColor = [UIColor lightGrayColor];
[self.view addSubview:textViewPlaceholder];
[super viewWillAppear:animated];
}
And there's an unnecessary margin on top of the UILabel as shown below:
I could set the y value to be negative something but I want to make sure why this is happening. One thing that I do when the view controller is loaded is self.automaticallyAdjustsScrollViewInsets = NO; to get rid of the margin inside the UITextView, but it shouldn't matter.
The exact x y values that work are (4, -16) for the UILabel.
I think the problem is the height of UILabel:
textViewPlaceholder = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, frameWidth, 200)];
The height is too large. Try to reduce it. And using background color to see more.

Updating same label with new text in ios

I am trying to update label. When I tap on label the text is move to textview then when i click on Done every time new label is created with updated text. What should I do to update same label? I am using `singleton' for doing so.
Try searching about IBOutlets and properties of particular UI Elements. Like, you want to change your .text property of the UILabel in this case. There is absolutely no use for singletons here.
Try to use viewWithTag to get the existing label if you don't have the instance of label.
For example:
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(10, 60, 200, 100)];
[self.view addSubview:myView];
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, CGRectGetWidth(myView.frame), 50)];
[myLabel setTag:1001]; // We can use this tag to find the label from different place.
[myView addSubview:myLabel];
To find the label
UILabel *mLabel = (UILabel *) [myView viewWithTag:1001];
if (mLabel) {
[mLabel setText:#"Hello"];
}

Adding a UIImageView as a subview of UILabel, image not appearing

So, when I do this with a regular old view:
UIView *topBlock = [[UIView alloc] initWithFrame:CGRectMake(0,-frameSize.height, frameSize.width, frameSize.height/2)];
[viewController.view addSubview:topBlock];
topBlock.autoresizesSubviews = NO;
topBlock.clipsToBounds = YES;
UIImage *topImage = [UIImage imageNamed:#"BloktLayout"];
UIImageView *topImageView = [[UIImageView alloc] initWithImage:topImage];
topImageView.frame = viewController.view.frame;
[topBlock addSubview:topImageView];
I get the nice old image where I want it, in the top view. But the middle view is a UILabel, and when I try the same thing:
UILabel *midBar = [[UILabel alloc] initWithFrame:CGRectMake(midBarOrigin.x, midBarOrigin.y, midBarWidth, midBarHeight)];
midBar.text = #"Blokt";
midBar.textColor = [UIColor blackColor];
midBar.textAlignment = NSTextAlignmentRight;
midBar.font = [UIFont fontWithName:#"HelveticaNeue-UltraLight" size:80.0f];
[viewController.view addSubview:midBar];
midBar.autoresizesSubviews = NO;
midBar.clipsToBounds = YES;
UIImage *midImage = [UIImage imageNamed:#"BloktLayout"];
UIImageView *midImageView = [[UIImageView alloc] initWithImage:midImage];
midImageView.frame = viewController.view.frame;
[midBar addSubview:midImageView];
I don't see any image at all in the UILabel. Any help?
Seems like the issue is related to your frames.
Tough to say without additional info. Can you post viewController.view.frame, frameSize, and midBarOrigin / midBarWidth / midBarHeight?
In the second codeblock, midBar.clipsToBounds == YES, but it looks like the midImageView.frame is likely very different / outside of midBar.frame in which case it wouldn't be visible.
Edit Some screenshots would help but aren't necessary
Edit 2 Note that subviews' origin points are always relative to the coordinate system of their superview, never relative to the coordinate system of any other view in the view heierarchy. This is likely the heart of the issue here. If you do want to convert CGPoints or CGRects from one coordinate system to another there are methods on UIView such as convertRect: and convertPoint: etc.
Interface Builder doesn't even let you add a control inside of a UILabel.
Instead, if you wish to group multiple controls, you can add them both as subviews of a UIView.
In other words, your image view and label can share the same superview, but the image view cannot be a subview of the label.
If they share the same superview, you can position the image view behind the label, and it should appear "through" the label as long as the label's background is clear.
Simple Way to do.....
You can add UIImageView, UILabel as subview of cell.textLabel
UIImageView *statusImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 4, 8, 8)];<br/>statusImage.backgroundColor = [UIColor redColor];<br/>
statusImage.layer.cornerRadius = 4;<br/>
statusImage.clipsToBounds = YES;<br/>
[cell.textLabel addSubview:statusImage];<br/>
UILabel *Lbl = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, cell.textLabel.frame.size.width - 15, cell.textLabel.frame.size.height)];<br/>
Lbl.text = #"xyz";<br/>
[cell.textLabel addSubview:Lbl];<br/>
I just had this problem as well.
I found that ImageView was behind the label.
When I replaced label with UIView, it works properly.

How to create a UIScrollView?

I have 4 labels on UIViewController. Now I want to know how can i make my current view to scrollable view using UIScrollView ?
You have to create and add these labels there. Something like this (Warning: untested)
UIScrollView * content = [[UIScrollView alloc] initWithFrame:self.view.bounds];
[content setContentSize:CGSizeMake(width, height)];
//Here you can add those labels to content view
[self.view addSubView:content];
To get scrolling to work you must specify size of the content (setContentSize:) larger than main view size. And don't forget to release the scroll view.
Simplest method:
-(void)scrollview
{
UIScrollView *scrollview=[[UIScrollView alloc]initWithFrame:CGRectMake(10, 10, 250, 150)];
scrollview.showsVerticalScrollIndicator=YES;
scrollview.scrollEnabled=YES;
scrollview.userInteractionEnabled=YES;
scrollview.backgroundColor = [UIColor greenColor];
[mainScrollView addSubview:scrollview];
scrollview.contentSize = CGSizeMake(250,150);
}

Resources