UIscrollview frame Changes to super view frame? - ios

I have three label and based on the text contentsize of UIScrollview is increasing.While I removing the Scrollview from super view and creating again this time frame height is increasing bottom control.
Before update:
After creating again:
Please let me know if you unclear to question.
-(void)setDataDynamicallyonScroll
{
if (self.infoScroll) {
[self.infoScroll removeFromSuperview];
self.infoScroll = nil;
}
self.infoScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(10.0f, 55.0f, 300, 450)];
self.infoScroll.autoresizingMask = UIViewAutoresizingFlexibleHeight;
UILabel *mainTitleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
UILabel *subTitleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
UILabel *detailsTitleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
if ([[self.dataDic valueForKey:#"subData"] valueForKey:#"FirstText"]!=nil) {
NSString *labelText = [[self.dataDic valueForKey:#"subData"] valueForKey:#"FirstText"];
[mainTitleLabel setText:labelText];
// Tell the label to use an unlimited number of lines
[mainTitleLabel setNumberOfLines:0];
[mainTitleLabel setBackgroundColor:[UIColor clearColor]];
[mainTitleLabel setFont:[UIFont boldSystemFontOfSize:12]];
mainTitleLabel.textColor = [UIColor colorWithRed:13.0/255.0 green:52.0/255.0 blue:112.0/255.0 alpha:1.0];
//[infoLabel sizeToFit];
CGSize infoLabelSize = [mainTitleLabel.text sizeWithFont:mainTitleLabel.font
constrainedToSize:CGSizeMake(self.infoScroll.frame.size.width, 9000)
lineBreakMode:NSLineBreakByWordWrapping];
mainTitleLabel.frame = CGRectMake(0, 0, infoLabelSize.width, infoLabelSize.height);
self.infoScroll.contentSize = infoLabelSize;
NSLog(#"%f,%f,%f,%f ",mainTitleLabel.frame.origin.x,mainTitleLabel.frame.origin.y,mainTitleLabel.frame.size.width,mainTitleLabel.frame.size.height);
NSLog(#"for First %f",self.infoScroll.contentSize.height);
[self.infoScroll addSubview:mainTitleLabel];
}
if ([[self.dataDic valueForKey:#"subData"] valueForKey:#"MiddleText"]!=nil) {
AppDelegate *delegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
switch ([delegate langType]) {
case 0:
[subTitleLabel setText:[[[self.dataDic valueForKey:#"subData"] valueForKey:#"MiddleText"] valueForKey:#"English"]];
break;
case 1:
[subTitleLabel setText:[#"\u200F" stringByAppendingString:[[[self.dataDic valueForKey:#"subData"] valueForKey:#"MiddleText"] valueForKey:#"Hebrew"]]];
break;
case 2:
[subTitleLabel setText:[[[self.dataDic valueForKey:#"subData"] valueForKey:#"MiddleText"] valueForKey:#"Trans"]];
break;
default:
break;
}
// Tell the label to use an unlimited number of lines
[subTitleLabel setNumberOfLines:0];
[subTitleLabel setBackgroundColor:[UIColor clearColor]];
[subTitleLabel setFont:[UIFont boldSystemFontOfSize:13]];
[subTitleLabel setTextAlignment:NSTextAlignmentRight];
subTitleLabel.textColor = [UIColor colorWithRed:155.0/255.0 green:0/255.0 blue:16.0/255.0 alpha:1.0];
//[infoLabel sizeToFit];
CGSize infoLabelSize = [subTitleLabel.text sizeWithFont:subTitleLabel.font
constrainedToSize:CGSizeMake(self.infoScroll.frame.size.width, 9000)
lineBreakMode:NSLineBreakByWordWrapping];
subTitleLabel.frame = CGRectMake(0, self.infoScroll.contentSize.height+20, infoLabelSize.width, infoLabelSize.height);
CGSize updatedScrollViewContentSize = CGSizeMake(300,self.infoScroll.contentSize.height+infoLabelSize.height+20 );
self.infoScroll.contentSize = updatedScrollViewContentSize;
[self.infoScroll addSubview:subTitleLabel];
}
if ([[self.dataDic valueForKey:#"subData"] valueForKey:#"LastText"]!=nil) {
[detailsTitleLabel setText:[[self.dataDic valueForKey:#"subData"] valueForKey:#"LastText"]];
// Tell the label to use an unlimited number of lines
[detailsTitleLabel setNumberOfLines:0];
[detailsTitleLabel setBackgroundColor:[UIColor clearColor]];
[detailsTitleLabel setFont:[UIFont systemFontOfSize:13]];
[detailsTitleLabel setTextAlignment:NSTextAlignmentLeft];
detailsTitleLabel.textColor = [UIColor blackColor];
[detailsTitleLabel sizeToFit];
CGSize infoLabelSize = [detailsTitleLabel.text sizeWithFont:detailsTitleLabel.font
constrainedToSize:CGSizeMake(self.infoScroll.frame.size.width, 9000)
lineBreakMode:NSLineBreakByWordWrapping];
UIImageView *bgView = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:#"detailsTextbg.png"]
resizableImageWithCapInsets:UIEdgeInsetsMake(12, 12, 12, 12)]];
bgView.frame = CGRectMake(0,self.infoScroll.contentSize.height+20, infoLabelSize.width, infoLabelSize.height+20);
detailsTitleLabel.frame = CGRectMake(5,10, infoLabelSize.width, infoLabelSize.height);
CGSize updatedScrollViewContentSize = CGSizeMake(300,self.infoScroll.contentSize.height+detailsTitleLabel.frame.size.height+50);
self.infoScroll.contentSize = updatedScrollViewContentSize;
[bgView addSubview:detailsTitleLabel];
[self.infoScroll addSubview:bgView];
}
[self.view addSubview:self.infoScroll];
}

It could be because of this line: self.infoScroll.autoresizingMask = UIViewAutoresizingFlexibleHeight; If you are manually setting the frame, I do not believe you want it to autoResize.

Related

how to set dynamic height for all iphone screen

In my application dn't use autolayout and size classes. Create all the elements programmatically.
Calculate view width and set frame for each element. Here my code.
UILabel* TitleLbl = [[UILabel alloc] initWithFrame:CGRectMake((self.view.frame.size.width/2)-25,(self.view.frame.size.height/2)-235,100, 50)];
TitleLbl.text=#"Login";
TitleLbl.textColor=[UIColor whiteColor];
TitleLbl.font = [UIFont boldSystemFontOfSize:14.0];
[self.view addSubview:TitleLbl];
//NSLog(#"ttttt %#",(self.view.frame.size.height/10));
UILabel* AccountLbl = [[UILabel alloc] initWithFrame:CGRectMake((self.view.frame.size.width/2)-130,(self.view.frame.size.height/2)-210,350, 50)];
AccountLbl.text=#"Login or Create Your New Account";
AccountLbl.textColor=[UIColor whiteColor];
AccountLbl.font = [UIFont boldSystemFontOfSize:16.0];
[self.view addSubview:AccountLbl];
UITextField* UserNameFld = [[UITextField alloc] initWithFrame:CGRectMake(self.view.frame.origin.x+40, (self.view.frame.size.height/2)-175,self.view.frame.size.width-80, 40)];
UserNameFld.font = [UIFont systemFontOfSize:15];
UserNameFld.placeholder = #"Username";
UserNameFld.keyboardType = UIKeyboardTypeDefault;
NSAttributedString *str = [[NSAttributedString alloc] initWithString:#"Username" attributes:#{ NSForegroundColorAttributeName : [UIColor colorWithRed:244.0f/255.0f green:244.0f/255.0f blue:244.0f/255.0f alpha:1.0] }];
UserNameFld.attributedPlaceholder = str;
UserNameFld.delegate = self;
[self DrawLine: UserNameFld];
[self.view addSubview:UserNameFld];
UITextField* PassWorldFld = [[UITextField alloc] initWithFrame:CGRectMake(self.view.frame.origin.x+40, (self.view.frame.size.height/2)-135,self.view.frame.size.width-80, 40)];
PassWorldFld.font = [UIFont systemFontOfSize:15];
PassWorldFld.placeholder = #"Password";
PassWorldFld.keyboardType = UIKeyboardTypeDefault;
PassWorldFld.secureTextEntry = YES;
PassWorldFld.delegate = self;
NSAttributedString *strPassword = [[NSAttributedString alloc] initWithString:#"Password" attributes:#{ NSForegroundColorAttributeName : [UIColor colorWithRed:244.0f/255.0f green:244.0f/255.0f blue:244.0f/255.0f alpha:1.0] }];
PassWorldFld.attributedPlaceholder = strPassword;
[self DrawLine: PassWorldFld];
[self.view addSubview:PassWorldFld];
UIButton *ForPassBtn = [UIButton buttonWithType:UIButtonTypeCustom];
// [ForPassBtn addTarget:self action:#selector(aMethod:)
// forControlEvents:UIControlEventTouchUpInside];
[ForPassBtn setTitle:#"Forgot Password?" forState:UIControlStateNormal];
ForPassBtn.frame = CGRectMake((self.view.frame.size.width/2), (self.view.frame.size.height/2)-90, (self.view.frame.size.width/2)-40, 20.0);
ForPassBtn.layer.borderColor = [UIColor whiteColor].CGColor;
ForPassBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
ForPassBtn.titleLabel.font = [UIFont systemFontOfSize:13.0];
ForPassBtn.backgroundColor=[UIColor clearColor];
[self.view addSubview:ForPassBtn];
UIButton *LoginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
// [LoginBtn addTarget:self action:#selector(aMethod:)
// forControlEvents:UIControlEventTouchUpInside];
[LoginBtn setTitle:#"Login" forState:UIControlStateNormal];
LoginBtn.frame = CGRectMake(self.view.frame.origin.x+40,(self.view.frame.size.height/2)-65, self.view.frame.size.width-80, 30.0);
LoginBtn.layer.cornerRadius = 10;
LoginBtn.layer.borderWidth=1.0f;
LoginBtn.layer.borderColor = [UIColor whiteColor].CGColor;
LoginBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
LoginBtn.titleLabel.font = [UIFont systemFontOfSize:16.0];
[self.view addSubview:LoginBtn];
How can i calulate the dynamic heigth for this code.
Any one can help me. how to calulate dynamic heigth. give any solution for me. Thanks advance.
Write below in .pch file
#define iPhoneFactorX ([[UIScreen mainScreen] bounds].size.width*1.00/1080.0)
Now write a code as per screen size of 1080x1920
E.g. you want to set a UIImage of size 600*400
UIImageView *m1 = [[UIImageView alloc] initWithFrame:CGRectMake(240*iPhoneFactorX, 100*iPhoneFactorX, 600*iPhoneFactorX, 400*iPhoneFactorX)];
Below is how I calculate starting point as 240
(1080-600)/2
^^^ width of image
This make 1 code for all screens....
UILabel* TitleLbl = [[UILabel alloc] initWithFrame:CGRectMake((self.view.frame.size.width/2)-25,(self.view.frame.size.height/2)-235,100, 50)];
TitleLbl.text=#"Login";
TitleLbl.textColor=[UIColor whiteColor];
TitleLbl.font = [UIFont boldSystemFontOfSize:14.0];
[self.view addSubview:TitleLbl];
//NSLog(#"ttttt %#",(self.view.frame.size.height/10));
UILabel* AccountLbl = [[UILabel alloc] initWithFrame:CGRectMake((self.view.frame.size.width/2)-130,TitleLbl.frame.origin.y + TitleLbl.frame.size.height + 10,350, 50)];
AccountLbl.text=#"Login or Create Your New Account";
AccountLbl.textColor=[UIColor whiteColor];
AccountLbl.font = [UIFont boldSystemFontOfSize:16.0];
[self.view addSubview:AccountLbl];
UITextField* UserNameFld = [[UITextField alloc] initWithFrame:CGRectMake(self.view.frame.origin.x+40, AccountLbl.frame.origin.y + AccountLbl.frame.size.height + 10,self.view.frame.size.width-80, 40)];
UserNameFld.font = [UIFont systemFontOfSize:15];
UserNameFld.placeholder = #"Username";
UserNameFld.keyboardType = UIKeyboardTypeDefault;
NSAttributedString *str = [[NSAttributedString alloc] initWithString:#"Username" attributes:#{ NSForegroundColorAttributeName : [UIColor colorWithRed:244.0f/255.0f green:244.0f/255.0f blue:244.0f/255.0f alpha:1.0] }];
UserNameFld.attributedPlaceholder = str;
UserNameFld.delegate = self;
// [self DrawLine: UserNameFld];
[self.view addSubview:UserNameFld];
UITextField* PassWorldFld = [[UITextField alloc] initWithFrame:CGRectMake(UserNameFld.frame.origin.x, UserNameFld.frame.origin.y + UserNameFld.frame.size.height + 10,UserNameFld.frame.origin.x + UserNameFld.frame.size.width, 40)];
PassWorldFld.font = [UIFont systemFontOfSize:15];
PassWorldFld.placeholder = #"Password";
PassWorldFld.keyboardType = UIKeyboardTypeDefault;
PassWorldFld.secureTextEntry = YES;
PassWorldFld.delegate = self;
NSAttributedString *strPassword = [[NSAttributedString alloc] initWithString:#"Password" attributes:#{ NSForegroundColorAttributeName : [UIColor colorWithRed:244.0f/255.0f green:244.0f/255.0f blue:244.0f/255.0f alpha:1.0] }];
PassWorldFld.attributedPlaceholder = strPassword;
// [self DrawLine: PassWorldFld];
[self.view addSubview:PassWorldFld];
UIButton *ForPassBtn = [UIButton buttonWithType:UIButtonTypeCustom];
// [ForPassBtn addTarget:self action:#selector(aMethod:)
// forControlEvents:UIControlEventTouchUpInside];
[ForPassBtn setTitle:#"Forgot Password?" forState:UIControlStateNormal];
ForPassBtn.frame = CGRectMake((self.view.frame.size.width/2), PassWorldFld.frame.origin.y + PassWorldFld.frame.size.height + 50, (self.view.frame.size.width/2)-40, 20.0);
ForPassBtn.layer.borderColor = [UIColor whiteColor].CGColor;
ForPassBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
ForPassBtn.titleLabel.font = [UIFont systemFontOfSize:13.0];
ForPassBtn.backgroundColor=[UIColor clearColor];
[self.view addSubview:ForPassBtn];
UIButton *LoginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
// [LoginBtn addTarget:self action:#selector(aMethod:)
// forControlEvents:UIControlEventTouchUpInside];
[LoginBtn setTitle:#"Login" forState:UIControlStateNormal];
LoginBtn.frame = CGRectMake(self.view.frame.origin.x+40,ForPassBtn.frame.origin.y + ForPassBtn.frame.size.height + 50, self.view.frame.size.width-80, 30.0);
LoginBtn.layer.cornerRadius = 10;
LoginBtn.layer.borderWidth=1.0f;
LoginBtn.layer.borderColor = [UIColor whiteColor].CGColor;
LoginBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
LoginBtn.titleLabel.font = [UIFont systemFontOfSize:16.0];
[self.view addSubview:LoginBtn];

How can we insert multiple titles on UInavigationBar in ios

Hi i am beginner in iOS and in my project i need to insert "titles" at "left side center" and "right side center" on UInavigationBar as like below image please help me how should i do below my requirement
See below code and try :
- (void)viewDidLoad
{
[super viewDidLoad];
CGRect frame = [[UIScreen mainScreen] bounds];
UILabel *lbl1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, frame.size.width/2, 44)];
lbl1.backgroundColor = [UIColor redColor];
lbl1.textAlignment = NSTextAlignmentCenter;
lbl1.text = #"Dashboard";
[self.navigationController.navigationBar addSubview:lbl1];
UILabel *lbl2 = [[UILabel alloc] initWithFrame:CGRectMake(frame.size.width/2, 0, frame.size.width/2, 44)];
lbl2.backgroundColor = [UIColor redColor];
lbl2.textAlignment = NSTextAlignmentCenter;
lbl2.text = #"Profile";
[self.navigationController.navigationBar addSubview:lbl2];
}
You can customize the titleView of navigation bar. Read the official documentation here.
As per your question, I tried the below working code:-
-(UIView *)getTitleView
{
CGRect frame = [[UIScreen mainScreen] bounds];
UIView *viewTitle = [[UIView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, self.navigationController.navigationBar.frame.size.height)];
UILabel *lbl1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, frame.size.width/2, self.navigationController.navigationBar.frame.size.height)];
lbl1.backgroundColor = [UIColor colorWithRed:130/255.0f green:13/255.0f blue:23/255.0f alpha:1.0f];
lbl1.textAlignment = NSTextAlignmentCenter;
[lbl1 setTextColor:[UIColor whiteColor]];
lbl1.text = #"Dashboard";
[viewTitle addSubview:lbl1];
UILabel *lbl2 = [[UILabel alloc] initWithFrame:CGRectMake(frame.size.width/2, 0, frame.size.width/2, viewTitle.frame.size.height)];
lbl2.backgroundColor = [UIColor colorWithRed:130/255.0f green:13/255.0f blue:23/255.0f alpha:1.0f];
[lbl2 setTextColor:[UIColor whiteColor]];
lbl2.textAlignment = NSTextAlignmentCenter;
lbl2.text = #"Profile";
[viewTitle addSubview:lbl2];
return viewTitle;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.titleView = [self getTitleView];
}
I divide the code in separate method to follow modular approach. For any part of code, which is of separate task, write that piece of code in different method. This will lead to more readability.

Unable to center a imageView in a tableView Footer

I am trying to center a label and an image in a tableView Footer
I can't seem to make it work for both device iPhone and iPad
Right now it is centered on the iPhone but that's because I hardcoded it.
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 50)];
[view setBackgroundColor:[UIColor clearColor]];
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 15, self.view.bounds.size.width, 20)];
lbl.lineBreakMode = NSLineBreakByWordWrapping;
lbl.numberOfLines = 0;
[lbl setText:#"Powered By"];
[lbl setFont:[UIFont systemFontOfSize:10]];
[lbl setTextAlignment:NSTextAlignmentCenter];
[lbl setTextColor:[UIColor blackColor]];
District *school = [District new];
UIImageView * logoView = [[UIImageView alloc]initWithFrame:CGRectMake(50, 25, 150, 37.5)];
logoView.image = [UIImage imageNamed:#"Logo.png"];
[logoView autoresizingMask];
[view addSubview:logoView];
[view addSubview:lbl];
return view;
}
I would like to center this view, without hardcoding it. I tried taking the screen size and divide by 2.
It did not center, what is the right approach, please advise.
You need to set the view's autoresizingMask as needed.
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 50)];
[view setBackgroundColor:[UIColor clearColor]];
CGRect lblFrame = view.bounds;
lblFrame.origin.y = 15;
lblFrame.size.height = 20;
UILabel *lbl = [[UILabel alloc] initWithFrame:lblFrame];
lbl.autoresizingMask = UIViewAutoresizingFlexibleWidth;
lbl.lineBreakMode = NSLineBreakByWordWrapping;
lbl.numberOfLines = 0;
[lbl setText:#"Powered By"];
[lbl setFont:[UIFont systemFontOfSize:10]];
[lbl setTextAlignment:NSTextAlignmentCenter];
[lbl setTextColor:[UIColor blackColor]];
District *school = [District new];
CGRect logoFrame = CGRectMake((view.bounds.size.width - 150) / 2.0, 25, 150, 37.5);
UIImageView * logoView = [[UIImageView alloc]initWithFrame:logoFrame];
logoView.image = [UIImage imageNamed:#"Logo.png"];
logoView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
[view addSubview:logoView];
[view addSubview:lbl];
return view;
}
This assumes the label should fill the width and the image should stay centered left-to-right.

how to Remove Dynamically Created UIView and UILabel text?

I am adding some dynamic UIView named as *cellSeparator and other UILabels...now what happen is when i again call this code then its rewrite the label text and overwrite on previously created label text...i am not very much aware to this ios development.so can anyone please tell me how can i remove this UIView dynamically before creating again?beacause UIView is dynamically created i dont know how to remove that UIview
UILabel *indexLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, self.view.frame.size.height-150, self.view.frame.size.width/2,30)];
[indexLabel setBackgroundColor:[UIColor clearColor]];
indexLabel.textColor = [UIColor whiteColor];
indexLabel.text = #"Details:-";
indexLabel.font = [UIFont systemFontOfSize:20.00];
UILabel *tagLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, self.view.frame.size.height-120, self.view.frame.size.width/2, 30)];
tagLabel.backgroundColor = [UIColor clearColor];
NSLog(#"LOg %#",imageId);
NSLog(#"LOg %#",imageStyle);
NSLog(#"LOg %#",imageType);
NSLog(#"LOg %#",imageWeight);
tagLabel.text = [NSString stringWithFormat:#"The Id of Jewl Is: %#",imageId];
imageTypelabel= [[UILabel alloc] initWithFrame:CGRectMake(20, self.view.frame.size.height-90, self.view.frame.size.width/2, 30)];
imageTypelabel.backgroundColor = [UIColor clearColor];
imageTypelabel.text = [NSString stringWithFormat:#"The Type of Jewl Is: %#",imageType];
imageStylelabel = [[UILabel alloc] initWithFrame:CGRectMake(20, self.view.frame.size.height-60, self.view.frame.size.width/2, 30)];
imageTypelabel.backgroundColor = [UIColor clearColor];
imageStylelabel.text = [NSString stringWithFormat:#"The style of Jewl Is: %#",imageStyle];
imageWeightlabel = [[UILabel alloc] initWithFrame:CGRectMake(20, self.view.frame.size.height-30, self.view.frame.size.width/2, 30)];
imageStylelabel.backgroundColor = [UIColor clearColor];
imageWeightlabel.text = [NSString stringWithFormat:#"The weight of Jewl Is: %#",imageWeight];
imageWeightlabel.backgroundColor = [UIColor clearColor];
imageWeightlabel.textColor = [UIColor whiteColor];
imageTypelabel.textColor = [UIColor whiteColor];
imageWeightlabel.textColor = [UIColor whiteColor];
tagLabel.textColor = [UIColor whiteColor];
UIImage *imageBegin = [UIImage imageNamed:imageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:imageBegin];
UIView *cellSeparator = [[UIView alloc] initWithFrame:CGRectMake(0,545, self.view.frame.size.width ,3)];
cellSeparator.tag=1;
[cellSeparator setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin |
UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleWidth];
[cellSeparator setContentMode:UIViewContentModeTopLeft];
[cellSeparator setBackgroundColor:[UIColor whiteColor]];
[self.view addSubview:cellSeparator];
You could write a method to remove all the subviews of the view and modify this code according to your need.
- (void)removeSubviewsOfView
{
NSArray *subViews = [self.view subviews];
for(UIView *view in subViews)
{
[view removeFromSuperview];
}
}

How to autoresize UIScrollView

In my app is an infopage. The textllength could differ due to localization. Beside that has a 3.5" screen less real-estate than a 4" display. So what I try to achieve is that the UILabel "grows" inside a ScrollView and the Scrollview adapts to the screen.
This is my code:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// Create a ScrollView and a label
UIScrollView *infoScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(20.0f, 55.0f, 300.0f, 400.0f)];
CGRect labelFrame = CGRectMake(0, 0, 280, 800);
UILabel *infoLabel = [[UILabel alloc] initWithFrame:labelFrame];
NSString *labelText = NSLocalizedString (#"InfoText",#"");
[infoLabel setText:labelText];
// Tell the label to use an unlimited number of lines
[infoLabel setNumberOfLines:0];
[infoLabel setBackgroundColor:[UIColor clearColor]];
[infoLabel setFont:[UIFont boldSystemFontOfSize:17]];
infoLabel.textColor = [UIColor colorWithRed:255 green:255 blue:255 alpha:1.0];
[infoLabel sizeToFit];
infoScroll.contentSize = CGSizeMake(infoScroll.contentSize.width, infoLabel.frame.size.height);
[infoScroll addSubview:infoLabel];
[self.view addSubview:infoScroll];
}
Please advice, thanks in advance
Modify your function by below way:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// Create a ScrollView and a label
UIScrollView *infoScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(20.0f, 55.0f, self.view.frame.size.width - 20.0, self.view.frame.size.height - 55)];
infoScroll.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
UILabel *infoLabel = [[UILabel alloc] initWithFrame:CGRectZero];
infoLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
NSString *labelText = NSLocalizedString (#"InfoText",#"");
[infoLabel setText:labelText];
// Tell the label to use an unlimited number of lines
[infoLabel setNumberOfLines:0];
[infoLabel setBackgroundColor:[UIColor clearColor]];
[infoLabel setFont:[UIFont boldSystemFontOfSize:17]];
infoLabel.textColor = [UIColor colorWithRed:255 green:255 blue:255 alpha:1.0];
//[infoLabel sizeToFit];
CGSize infoLabelSize = [infoLabel.text sizeWithFont:infoLabel.font
constrainedToSize:CGSizeMake(infoScroll.frame.size.width, 5000)
lineBreakMode:UILineBreakModeWordWrap];
infoLabel.frame = CGRectMake(0, 0, infoLabelSize.width, infoLabelSize.height);
infoScroll.contentSize = infoLabelSize;
[infoScroll addSubview:infoLabel];
[self.view addSubview:infoScroll];
}

Resources