Scroll down and up in UIScrollVIew Objective C - ios

I want to scroll up and down in my page without using AutoLayout in Xcode. How do I make the scrolling work?
I want to show the second question label and text and add other labels and fields. But I can't scroll to see how they look after building the project
This is the viewDidLoad
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIImage *bg = [UIImage imageNamed:#"logo2.jpg"];
self.myImageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
self.myImageView.image = bg;
self.myImageView.contentMode = UIViewContentModeScaleAspectFit;
self.myImageView.center = self.view.center;
self.view.backgroundColor = [UIColor whiteColor];
[self.view addSubview:self.myImageView];
CGRect scrollViewRect = self.view.bounds;
self.myScrollView = [[UIScrollView alloc] initWithFrame:scrollViewRect];
self.myScrollView.pagingEnabled = YES;
self.myScrollView.scrollEnabled = YES;
self.myScrollView.contentSize = CGSizeMake(self.myScrollView.frame.size.width + 100, self.myScrollView.frame.size.height + 100);
[self.view addSubview:self.myScrollView];
float width = CGRectGetWidth(self.myScrollView.frame);
float height = CGRectGetHeight(self.myScrollView.frame);
float newPosition = self.myScrollView.contentOffset.y+height;
CGRect toVisible = CGRectMake(newPosition, 0, width, height);
[self.myScrollView scrollRectToVisible:toVisible animated:YES];
//FirstNameLabel
CGRect labelFirst = CGRectMake(10, 100, 100, 30);
self.firstnamelabel = [[UILabel alloc] initWithFrame:labelFirst];
self.firstnamelabel.font = [UIFont boldSystemFontOfSize:15.0f];
self.firstnamelabel.text = #"First Name : ";
//FirstNameField
CGRect firsttextField = CGRectMake(100, 100, 200, 30);
self.firstnametext = [[UITextField alloc] initWithFrame:firsttextField];
self.firstnametext.borderStyle = UITextBorderStyleRoundedRect;
//LastNameLabel
CGRect labelLast = CGRectMake(10, 150, 100, 30);
self.lastnamelabel = [[UILabel alloc] initWithFrame:labelLast];
self.lastnamelabel.font = [UIFont boldSystemFontOfSize:15.0f];
self.lastnamelabel.text = #"Last Name : ";
//LastNameTextField
CGRect lasttextField = CGRectMake(100, 150, 200, 30);
self.lastnametext = [[UITextField alloc] initWithFrame:lasttextField];
self.lastnametext.borderStyle = UITextBorderStyleRoundedRect;
//DateLabel
CGRect labeldate = CGRectMake(10, 190, 100, 30);
self.DateOfBirthLabel = [[UILabel alloc] initWithFrame:labeldate];
self.DateOfBirthLabel.font = [UIFont boldSystemFontOfSize:15.0f];
self.DateOfBirthLabel.text = #"Date Of Birth: ";
//DatePicker
CGRect datepickersize=CGRectMake(10, 210, 300, 150);
self.datepicker=[[UIDatePicker alloc] initWithFrame:datepickersize];
NSDate *date= self.datepicker.date;
self.datepicker.datePickerMode=UIDatePickerModeDate;
//Email
CGRect labelemail = CGRectMake(30, 370, 100, 30);
self.emaillabel = [[UILabel alloc] initWithFrame:labelemail];
self.emaillabel.font = [UIFont boldSystemFontOfSize:15.0f];
self.emaillabel.text = #"Email : ";
//EmailText
CGRect emailtextField = CGRectMake(100, 370, 200, 30);
self.emailtext = [[UITextField alloc] initWithFrame:emailtextField];
self.emailtext.borderStyle = UITextBorderStyleRoundedRect;
//PasswordLabel
CGRect labelpassword = CGRectMake(10, 410, 100, 30);
self.passwordlabel = [[UILabel alloc] initWithFrame:labelpassword];
self.passwordlabel.font = [UIFont boldSystemFontOfSize:15.0f];
self.passwordlabel.text = #"Password : ";
//PasswordText
CGRect passwordtextField = CGRectMake(100, 410, 180, 30);
self.passwordtext = [[UITextField alloc] initWithFrame:passwordtextField];
self.passwordtext.borderStyle = UITextBorderStyleRoundedRect;
self.passwordtext.secureTextEntry = true;
//eyebutton
UIImage *eye = [UIImage imageNamed:#"eye.png"];
self.eyebutton = [UIButton buttonWithType:UIButtonTypeCustom];
self.eyebutton.frame = CGRectMake(290, 415, 20, 20);
[self.eyebutton setImage:eye forState:UIControlStateNormal];
[self.eyebutton addTarget:self action:#selector(touchDown) forControlEvents:UIControlEventTouchDown];
[self.eyebutton addTarget:self action:#selector(touchUpInside) forControlEvents:UIControlEventTouchUpInside];
//FirstQuetionLabel
CGRect labelfq = CGRectMake(10, 450, 300, 20);
self.firstQuestlabel = [[UILabel alloc] initWithFrame:labelfq];
self.firstQuestlabel.font = [UIFont boldSystemFontOfSize:14.0f];
self.firstQuestlabel.text = #"Question 1: In which city you were born in?";
//FirstQuestionTextField
CGRect fqtextField = CGRectMake(10, 470, 240, 30);
self.firstQuestionText = [[UITextField alloc] initWithFrame:fqtextField];
self.firstQuestionText.borderStyle = UITextBorderStyleRoundedRect;
//SecondQuestionLabel
CGRect labelsq = CGRectMake(10, 500, 300, 30);
self.secondQuestLabel = [[UILabel alloc] initWithFrame:labelsq];
self.secondQuestLabel.font = [UIFont boldSystemFontOfSize:13.0f];
self.secondQuestLabel.text = #"Question 2: What is your mother's \rmiddle name?";
//SecondQuestionTextField
CGRect sqtextField = CGRectMake(10, 630, 240, 30);
self.secondQuestionText = [[UITextField alloc] initWithFrame:sqtextField];
self.secondQuestionText.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:self.eyebutton];
[self.view addSubview:self.firstnamelabel];
[self.view addSubview:self.firstnametext];
[self.view addSubview:self.lastnamelabel];
[self.view addSubview:self.lastnametext];
[self.view addSubview:self.DateOfBirthLabel];
[self.view addSubview:self.datepicker];
[self.view addSubview:self.emaillabel];
[self.view addSubview:self.emailtext];
[self.view addSubview:self.passwordlabel];
[self.view addSubview:self.passwordtext];
[self.view addSubview:self.firstQuestlabel];
[self.view addSubview:self.firstQuestionText];
[self.view addSubview:self.secondQuestLabel];
[self.view addSubview:self.secondQuestionText];
}

At first glance, scrolling may not be enabled because you have not set the contentSize property of the scroll view. To set allow scrolling, the contentSize needs to be set to be larger than the frame of the scroll view.
An example in objective-c would be:
self.myScrollView.contentSize = CGSizeMake(self.myScrollView.frame.size.width + 20, self.myScrollView.frame.size.height + 20);
Updated for edited question
Any views you want to be scrollable within your scroll view need to be added as a subview on the scrollView i.e
[self.myScrollView addSubview: myLabel]; //do for all views that you want to be scrollable in the scrollView
Then you need find the sum height of all the views contained in your scroll view i.e
CGFloat sumHeight = myLabel1.frame.size.height + myView2.frame.size.height; //etc do for all views you want in your scrollView
then set it to be your contentSize ie
self.myScrollView.contentSize = CGSizeMake(self.myScrollView.frame.size.width, sumHeight);

Related

UIButton Selector method not working

I am creating a UIIimageView programtically than I create the UIView and in uiview I added two uibutton programtically but button selector method not working . I tried many thing but nothing happened . Here is my code
-(void) pagingFunc{
CGRect screen = [[UIScreen mainScreen] bounds];
CGFloat width = CGRectGetWidth(screen);
CGFloat height = CGRectGetHeight(screen);
//ScrollView Size and Contents
CGSize mxSize = CGSizeMake( [imgesArry count]*width , height-114);
[scrlView setContentSize : mxSize];
self.customView.translatesAutoresizingMaskIntoConstraints =YES;
self.customView.frame = CGRectMake(0, 0, mxSize.width, mxSize.height);
int incX = 0 ;
for( int i = 0; i < [imgesArry count]; i++)
{
PFObject *imageObject = [imgesArry objectAtIndex:i];
PFFile *file;
if (height == 480) {
file = [imageObject objectForKey:#"image4s"];
}
else{
file = [imageObject objectForKey:#"image6s"];
}
NSString * imgFile = [file url];
UIImageView* imagView = [[UIImageView alloc]initWithFrame : CGRectMake(incX,0,width ,height)];
imgView.userInteractionEnabled = YES;
[imagView sd_setImageWithURL:[NSURL URLWithString:imgFile] placeholderImage:[UIImage imageNamed:#"UserUpdationImageCell.png"]];
btnView = [[UIView alloc] initWithFrame:CGRectMake(incX, imagView.frame.size.height-60, width, 50)];
btnView.backgroundColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.8];
UIButton *bckBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[bckBtn addTarget:self
action:#selector(aMethod)
forControlEvents:UIControlEventTouchUpInside];
[bckBtn setTitle:#"Back" forState:UIControlStateNormal];
bckBtn.frame = CGRectMake(0 ,0, 160.0, 40.0);
UIButton *downloadBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[downloadBtn addTarget:self
action:#selector(aMethod)
forControlEvents:UIControlEventTouchUpInside];
[downloadBtn setTitle:#"Download" forState:UIControlStateNormal];
downloadBtn.frame = CGRectMake(160 ,0, 160.0, 40.0);
[btnView addSubview:bckBtn];
[btnView addSubview:downloadBtn];
[self.customView addSubview:imagView];
[self.customView addSubview:btnView];
incX+= width;
}
[scrlView setContentOffset:CGPointMake(width*selectedIndex, 0)];
}
May be you can set : self.customView.userInteractionEnabled=YES;
I write a simple demo you describe and it works:
// imagView -> view1 -> bckBtn
UIImageView *imagView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 200, 200)];
imagView.userInteractionEnabled = YES;
imagView.backgroundColor = [UIColor grayColor];
[self.view addSubview:imagView];
UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 100, 100)];
view1.backgroundColor = [UIColor yellowColor];
[imagView addSubview:view1];
UIButton *bckBtn = [UIButton buttonWithType:UIButtonTypeCustom];
bckBtn.frame = CGRectMake(10, 10, 50, 50);
bckBtn.backgroundColor = [UIColor redColor];
[bckBtn addTarget:self
action:#selector(aMethod)
forControlEvents:UIControlEventTouchUpInside];
[bckBtn setTitle:#"Back" forState:UIControlStateNormal];
[view1 addSubview:bckBtn];
-(void)aMethod{
NSLog(#"button click");
}
Hope it helps.
Well 1st thing, make sure you don't have any overlays over buttons.
Also you might try using GestureRecognizers.
Here's a Swift snippet, I'm pretty sure it can be translated to obj-c
let xButton = UIButton(frame: CGRect(x: 0, y: 0, width: 160, height: 40))
xButton.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(aMethod)))

Why UIView size is changed by adding label on it?

I have Manager UIViewController class. Draw Chart UIView is one of the components in Manager ViewController. It is shown in the attached picture.
In Manager.m, I set the size of DrawChart UIView.
- (IBAction)viewRecordButtonisPressed:(id)sender {
//set UIView size
CGRect frame = self.drawChart.frame;
frame.origin.x = 0;
frame.origin.y = (int)((float)self.view.bounds.size.height/6.0);
frame.size.width = self.view.bounds.size.width;
frame.size.height = (int)((float)self.view.bounds.size.height*(2.0/3.0));
self.drawChart.frame = frame;
self.drawChart.hidden = NO;
[self.drawChart setNeedsDisplay];
}
Then in DrawChart.m, I added three labels. Then Draw Chart's UIView size is changed. How to write on UIView without changing the UIView's size?
- (void)drawRect:(CGRect)rect {
if(![currentNameID isEqualToString:#""]){
[self extractInformation];
UILabel *idLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 300, 20)];
[idLabel setTextColor:[UIColor whiteColor]];
[idLabel setBackgroundColor:[UIColor clearColor]];
[idLabel setFont:[UIFont fontWithName: #"Didot-Italic" size: 15.0f]];
NSString* idtext = #"ID: ";
idLabel.text = [idtext stringByAppendingString:currentNameID];
[self addSubview:idLabel];
UILabel *dobLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 30, 300, 20)];
[dobLabel setTextColor:[UIColor whiteColor]];
[dobLabel setBackgroundColor:[UIColor clearColor]];
[dobLabel setFont:[UIFont fontWithName: #"Didot-Italic" size: 15.0f]];
idtext = #"DOB: ";
dobLabel.text = [idtext stringByAppendingString:dob];
[self addSubview:dobLabel];
UILabel *genderLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 50, 300, 20)];
[genderLabel setTextColor:[UIColor whiteColor]];
[genderLabel setBackgroundColor:[UIColor clearColor]];
[genderLabel setFont:[UIFont fontWithName: #"Didot-Italic" size: 15.0f]];
idtext = #"Gender: ";
genderLabel.text = [idtext stringByAppendingString:gender];
[self addSubview:genderLabel];
}
}
Not sure what the problem is. The way I solved is I used CoreGraphic to write text. It doesn't change the UIView's size.
I replaced with
- (void)drawRect:(CGRect)rect {
if(![currentNameID isEqualToString:#""]){
[self extractInformation];
NSDictionary *textAttributes = #{NSFontAttributeName: [UIFont fontWithName: #"Didot-Italic" size: 15.0f], NSForegroundColorAttributeName:[UIColor whiteColor]};
// Create string drawing context
NSStringDrawingContext *drawingContext = [[NSStringDrawingContext alloc] init];
CGRect drawRect = CGRectMake(10, 10, 300, 20);
NSString* idtext = #"ID: ";
idtext = [idtext stringByAppendingString:currentNameID];
[idtext drawWithRect:drawRect
options:NSStringDrawingUsesLineFragmentOrigin
attributes:textAttributes
context:drawingContext];
CGRect drawRect1 = CGRectMake(10, 30, 300, 20);
idtext = #"DOB: ";
idtext = [idtext stringByAppendingString:dob];
[idtext drawWithRect:drawRect1
options:NSStringDrawingUsesLineFragmentOrigin
attributes:textAttributes
context:drawingContext];
CGRect drawRect2 = CGRectMake(10, 50, 300, 20);
idtext = #"Gender: ";
idtext = [idtext stringByAppendingString:gender];
[idtext drawWithRect:drawRect2
options:NSStringDrawingUsesLineFragmentOrigin
attributes:textAttributes
context:drawingContext];
}
}

How to increase the HMSegmentedControl count?

hello I am using HMSegmentedControl for swipe tab feature. And my code is like this.
// Tying up the segmented control to a scroll view
self.segmentedControl4 = [[HMSegmentedControl alloc] initWithFrame:CGRectMake(0, 260, viewWidth, 50)];
self.segmentedControl4.sectionTitles = #[#"Worldwide", #"Local", #"Headlines",#"Four",#"Five"];
self.segmentedControl4.selectedSegmentIndex = 1;
self.segmentedControl4.backgroundColor = [UIColor colorWithRed:0.7 green:0.7 blue:0.7 alpha:1];
self.segmentedControl4.titleTextAttributes = #{NSForegroundColorAttributeName : [UIColor whiteColor]};
self.segmentedControl4.selectedTitleTextAttributes = #{NSForegroundColorAttributeName : [UIColor colorWithRed:0.1 green:0.1 blue:0.1 alpha:1]};
self.segmentedControl4.selectionIndicatorColor = [UIColor colorWithRed:0.3 green:0.3 blue:0.3 alpha:1];
self.segmentedControl4.selectionStyle = HMSegmentedControlSelectionStyleBox;
self.segmentedControl4.selectionIndicatorLocation = HMSegmentedControlSelectionIndicatorLocationUp;
self.segmentedControl4.tag = 3;
__weak typeof(self) weakSelf = self;
[self.segmentedControl4 setIndexChangeBlock:^(NSInteger index) {
[weakSelf.scrollView scrollRectToVisible:CGRectMake(viewWidth * index, 0, viewWidth, 200) animated:YES];
}];
[self.view addSubview:self.segmentedControl4];
self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 310, viewWidth, 210)];
self.scrollView.backgroundColor = [UIColor colorWithRed:0.7 green:0.7 blue:0.7 alpha:1];
self.scrollView.pagingEnabled = YES;
self.scrollView.showsHorizontalScrollIndicator = NO;
self.scrollView.contentSize = CGSizeMake(viewWidth * 5, 200);
self.scrollView.delegate = self;
[self.scrollView scrollRectToVisible:CGRectMake(viewWidth, 0, viewWidth, 200) animated:NO];
[self.view addSubview:self.scrollView];
UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, viewWidth, 210)];
[self setApperanceForLabel:label1];
label1.text = #"Worldwide";
[self.scrollView addSubview:label1];
UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(viewWidth, 0, viewWidth, 210)];
[self setApperanceForLabel:label2];
label2.text = #"Local";
[self.scrollView addSubview:label2];
UILabel *label3 = [[UILabel alloc] initWithFrame:CGRectMake(viewWidth * 2, 0, viewWidth, 210)];
[self setApperanceForLabel:label3];
label3.text = #"Headlines";
[self.scrollView addSubview:label3];
UILabel *label4 = [[UILabel alloc] initWithFrame:CGRectMake(viewWidth * 2, 0, viewWidth, 210)];
[self setApperanceForLabel:label4];
label4.text = #"Four";
[self.scrollView addSubview:label4];
UILabel *label5 = [[UILabel alloc] initWithFrame:CGRectMake(viewWidth * 2, 0, viewWidth, 210)];
[self setApperanceForLabel:label5];
label5.text = #"Five";
[self.scrollView addSubview:label5];
My problem is it doesnt show these label4 and label 5 only show upto 3. But all 5 titles show from the section titles array. How can I solve these problem.
Please help me.
ThanksThis is the github project I used
I don't know about HMSegmentedControl but i use Page Menu for scroll pages like segment controller. you can add your view controller directly into this menu segment.
I am not sure that this is a perfect answer of this question but it might help others as well.
Set your self.segmentedControl4.tag = 5;
Give tag numbers which you want to show in segmentedControl.

Why is nothing displaying in my UIView?

I have following code that adds a UIView and then adds a UIImageView and UILabel, the UIView display correctly but nothing i have put in side the view is showing. I have looked everywhere but i cannot find an answer. The UIView is presented under certain conditions and when it is tapped it dissapears.
Here is my code.
UIView *actionView = [[UIView alloc] initWithFrame:CGRectMake(20, 180, 280, 165)];
UITapGestureRecognizer *singleFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTap:)];
[actionView addGestureRecognizer:singleFingerTap];
actionView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5f];
actionView.layer.cornerRadius = 5;
actionView.layer.masksToBounds = YES;
[self.view addSubview:actionView];
UILabel *actionText = [[UILabel alloc] initWithFrame:CGRectMake(20, 180, 280, 165)];
actionText.text = #"Hello";
actionText.textColor = [UIColor redColor];
actionText.textAlignment = NSTextAlignmentCenter;
[actionText setTextColor:[UIColor redColor]];
[actionText setBackgroundColor:[UIColor clearColor]];
[actionText setFont:[UIFont fontWithName: #"Trebuchet MS" size: 14.0f]];
actionText.Frame = CGRectMake(20, 180, 280, 165);
[actionView addSubview:actionText];
UIImage* image = [UIImage imageNamed:[NSString stringWithFormat:#"trash-icon.png"]];
UIImageView* blockView = [[UIImageView alloc] initWithImage:image];
blockView.frame = CGRectMake(109, 273, 40, 40);
[actionView addSubview:blockView];
Change your frame positions like..
UILabel *actionText = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, 280, 40)];
actionText.text = #"Hello";
actionText.textColor = [UIColor redColor];
actionText.textAlignment = NSTextAlignmentCenter;
[actionText setTextColor:[UIColor redColor]];
[actionText setBackgroundColor:[UIColor clearColor]];
[actionText setFont:[UIFont fontWithName: #"Trebuchet MS" size: 14.0f]];
[actionView addSubview:actionText];
UIImage* image = [UIImage imageNamed:[NSString stringWithFormat:#"trash-icon.png"]];
UIImageView* blockView = [[UIImageView alloc] initWithImage:image];
blockView.frame = CGRectMake(109,70, 40, 40);
[actionView addSubview:blockView];
When you set the frame of a view, it is important to remember that the origin relates to the coordinate system of the superview it is added to.
So when you set the frame of your label to:
UILabel *actionText = [[UILabel alloc] initWithFrame:CGRectMake(20, 180, 280, 165)];
the origin of x = 20, y = 180 end up being the origin WITHIN the actionView. Now this is your problem because your actionView is only 165px tall, and you are telling the actionText subView that it's y origin is 180, which is well outside the bottom of the actionView.
for your actionText, you should allocate it as follows:
UILabel *actionText = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 280, 165)];
and for your blockView, something like:
blockView.frame = CGRectMake(89,93, 40, 40);

Autoresize UIScrollView To Fit UILabel Text

I have been searching for quite a while now, and every answer I find doesn't seem to work. I have a view, code below, that holds a bunch of text that is pulled from a YouTube video description from another view. Obviously ever video's descriptions are of different length, which is what poses the problem. I want to make it that the scrollview only scrolls down to about 10 pixels after the last text line of the label.
In this code, I don't have it doing that, I have it at a fixed size.
Can ANYONE help? I have been searching for days now, and I can seem to find a proper solution.
self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
VideoDescription = [VideoDescription stringByTrimmingCharactersInSet:
[NSCharacterSet whitespaceAndNewlineCharacterSet]];
//int a = VideoDescription.length;
//NSLog([NSString stringWithFormat:#"%i", a]);
if(VideoDescription.length < 310)
{
VideoDescription = [VideoDescription stringByAppendingString:#"\n_______________________________________________"];
VideoDescription = [VideoDescription stringByAppendingString:#"\n\nFor the best viewing experience, please use WiFi."];
VideoDescription = [VideoDescription stringByAppendingString:#"\n\nWant an app like this for your YouTube Channel? Go to www.apps4tubers.com to find out how you can get one!"];
VideoDescription = [VideoDescription stringByAppendingString:#"\n\nDon't forget to rate this app! Love it? Hate it? Regardless we love to hear your comments and suggestions!"];
}
else if(VideoDescription.length < 380)
{
VideoDescription = [VideoDescription stringByAppendingString:#"\n_______________________________________________"];
VideoDescription = [VideoDescription stringByAppendingString:#"\n\nFor the best viewing experience, please use WiFi."];
VideoDescription = [VideoDescription stringByAppendingString:#"\n\nWant an app like this for your YouTube Channel? Go to www.apps4tubers.com to find out how you can get one!"];
}
else if(VideoDescription.length > 379 && VideoDescription.length < 410)
{
VideoDescription = [VideoDescription stringByAppendingString:#"\n_______________________________________________"];
VideoDescription = [VideoDescription stringByAppendingString:#"\n\nWant an app like this for your YouTube Channel? Go to www.apps4tubers.com to find out how you can get one!"];
}
else
{
VideoDescription = [VideoDescription stringByAppendingString:#"\n\nFor the best viewing experience, please use WiFi."];
}
CGRect scrollViewFrame = CGRectMake(0, 0, 320, 480);
TheView = [[UIScrollView alloc] initWithFrame:scrollViewFrame];
CGSize scrollViewContentSize = CGSizeMake(320, 650);
[TheView setContentSize:scrollViewContentSize];
[TheView setPagingEnabled:NO];
TheView.showsHorizontalScrollIndicator = NO;
TheView.showsVerticalScrollIndicator = YES;
TheView.bounces = NO;
[self.view addSubview:TheView];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.backBarButtonItem = backButton;
Background = [[UILabel alloc] initWithFrame: CGRectMake(00, 25, 1000, 1600)];
Background.center = CGPointMake(00, 00);
Background.backgroundColor = [self colorWithHexString:#"111625"];
[TheView addSubview: Background];
VideoDecrip = [[UILabel alloc] initWithFrame: CGRectMake(00, 25, 320, 400)];
VideoDecrip.center = CGPointMake(160.5, 400);
VideoDecrip.text = VideoDescription;
VideoDecrip.font = [UIFont fontWithName: #"Cochin-Bold" size: 13.5];
VideoDecrip.textColor = [self colorWithHexString:#"D1E751"];
VideoDecrip.shadowColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:.35];
VideoDecrip.layer.shadowOpacity = 0;
VideoDecrip.shadowOffset = CGSizeMake(1,1);
VideoDecrip.layer.masksToBounds = NO;
VideoDecrip.backgroundColor = [UIColor clearColor];
VideoDecrip.textAlignment = NSLineBreakByWordWrapping;
VideoDecrip.adjustsFontSizeToFitWidth = YES;
VideoDecrip.numberOfLines = 0;
[VideoDecrip sizeToFit];
[TheView addSubview: VideoDecrip];
NSLog(#"%i", VideoDecrip.text.length);
VideoTitleBorder = [[UILabel alloc] initWithFrame: CGRectMake(00, 25, 320.5, 30)];
VideoTitleBorder.center = CGPointMake(160, 182.5);
VideoTitleBorder.shadowOffset = CGSizeMake(1,1);
VideoTitleBorder.backgroundColor = [self colorWithHexString:#"0B486B"];
VideoTitleBorder.layer.borderColor = [UIColor darkGrayColor].CGColor;
VideoTitleBorder.layer.borderWidth = .4;
[TheView addSubview: VideoTitleBorder];
VideoTitle = [[UILabel alloc] initWithFrame: CGRectMake(00, 25, 320, 26)];
VideoTitle.text = VidTitle;
if([VideoTitle.text length] < 20)
{
VideoTitle.center = CGPointMake(160, 182.5);
}
else if([VideoTitle.text length] < 30)
{
VideoTitle.center = CGPointMake(160, 182);
}
else
{
VideoTitle.center = CGPointMake(160, 179);
}
VideoTitle.font = [UIFont fontWithName: #"ArialRoundedMTBold" size: 25];
VideoTitle.textColor = [self colorWithHexString:#"BEF202"];
VideoTitle.shadowColor = [UIColor blackColor];
VideoTitle.backgroundColor = [UIColor clearColor];
VideoTitle.shadowOffset = CGSizeMake(1,1);
VideoTitle.textAlignment = NSTextAlignmentCenter;
VideoTitle.adjustsFontSizeToFitWidth = YES;
[TheView addSubview: VideoTitle];
VideoTitleBorder2 = [[UILabel alloc] initWithFrame: CGRectMake(00, 25, 160, 500)];
VideoTitleBorder2.center = CGPointMake(400, 125);
VideoTitleBorder2.shadowOffset = CGSizeMake(1,1);
VideoTitleBorder2.backgroundColor = [self colorWithHexString:#"0B486B"];
VideoTitleBorder2.layer.borderColor = [UIColor darkGrayColor].CGColor;
VideoTitleBorder2.layer.borderWidth = 2.0;
[self.view addSubview: VideoTitleBorder2];
In short, you can use the NSString method -sizeWithFont:forWidth:lineBreakMode: to determine how tall the string will render in a container with the given width and line break mode. You can use that to size your UILabel and to set the scrollview's contentsize appropriately.
Try using sizeToFiton the label, then setting the scrollView's contentSize:
[label sizeToFit];
[scrollView setContentSize:CGSizeMake(CGRectGetWidth(self.view.frame), CGRectGetHeight(label.frame) + 10)];
Using sizeToFit is really nice, just make sure your label is already the right width before you call it.

Resources