Why UIView size is changed by adding label on it? - ios

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

Related

Scroll down and up in UIScrollVIew Objective C

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

Font size is not changed for title and subtitle in navigationItem.titleView

My goal is to use title and subtitle with different font sizes in navigation controller page title (title should be bigger, subtitle should be lower accordingly).
I found example of code to implement this. The only one problem is that font size is not applied - both title and subtitle have the same font size. Like the code for font size doesn't work.
How to fix that? Thank you
// prepare title label
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.textColor = [UIColor whiteColor];
titleLabel.font = [UIFont fontWithName:#"HelveticaNeueLight" size:19.0];
titleLabel.text = locationInfo;
[titleLabel sizeToFit];
// prepare subtitle label
UILabel *subtitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 18, 0, 0)];
subtitleLabel.backgroundColor = [UIColor clearColor];
subtitleLabel.textColor = [UIColor whiteColor];
subtitleLabel.font = [UIFont fontWithName:#"HelveticaNeueLight" size:12.0];
subtitleLabel.text = dateInfo;
[subtitleLabel sizeToFit];
UIView *twoLineTitleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, MAX(subtitleLabel.frame.size.width, titleLabel.frame.size.width), 30)];
[twoLineTitleView addSubview:titleLabel];
[twoLineTitleView addSubview:subtitleLabel];
float widthDiff = subtitleLabel.frame.size.width - titleLabel.frame.size.width;
if (widthDiff > 0) {
CGRect frame = titleLabel.frame;
frame.origin.x = widthDiff / 2;
titleLabel.frame = CGRectIntegral(frame);
} else{
CGRect frame = subtitleLabel.frame;
frame.origin.x = fabs(widthDiff) / 2;
subtitleLabel.frame = CGRectIntegral(frame);
}
self.navigationItem.titleView = twoLineTitleView;
It's with the setFont method, not .font
[titleLabel setFont:[UIFont fontWithName:#"HelveticaNeue-Light" size:12.0]];
And also you have an error in the font name:
it's HelveticaNeue-Light
Use SetFont instead of Font::
[titleLabel setFont:[UIFont fontWithName:#"HelveticaNeue-UltraLight" size:14.0]];

UILabel how to autofit content?

I'm working on a personal project which is a simple quote generator.
Users can save favourite their quotes to a list, and in the list view I would like the UILabel to size responsively to the content (some quotes are short, and some are long - which can look a bit odd).
Any help would be greatly appreciated.
UILabel *favouriteLabel;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
favouriteLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, -10, 400, 140)];
}else {
favouriteLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, -20, 180, 130)];
}
favouriteLabel.backgroundColor = [UIColor clearColor];
favouriteLabel.text = [self.arrayFromFile objectAtIndex:indexPath.row];
NSLog(#"Row alloc %d", indexPath.row);
NSLog(#"Load label %#", favouriteLabel.text);
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
favouriteLabel.font = [UIFont fontWithName: #"Eras Bold ITC" size: 18];
}else if([GUIHelper isPhone5]){
favouriteLabel.font = [UIFont fontWithName: #"Dosis-Medium" size: 15];
}
else
favouriteLabel.font = [UIFont fontWithName: #"Dosis-Medium" size: 15];
favouriteLabel.textColor = [UIColor colorWithRed:68.0/255.0 green:68.0/255.0 blue:68.0/255.0 alpha:1];
favouriteLabel.textAlignment = UITextAlignmentCenter;
favouriteLabel.numberOfLines = 0;
favouriteLabel.center = CGPointMake(cell.center.x - 40, cell.center.y + 20);
favouriteLabel.tag = 900;
[cell.contentView addSubview:favouriteLabel];
UIImage *lineImg = [UIImage imageNamed:#"quote-divider#2x.png"];
CGRect lineImageFrame;
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
lineImageFrame = CGRectMake(0, 120, lineImg.size.width, lineImg.size.height);
}else{
lineImageFrame = CGRectMake(0, 62, lineImg.size.width * 0.9, 0.8*lineImg.size.height);
}
UIImageView *lineImageView = [[UIImageView alloc] initWithFrame:lineImageFrame];
lineImageView.image = lineImg;
lineImageView.center = CGPointMake(cell.center.x - 40, lineImageView.center.y + 35);
Before you add the label to the view, try [favoriteLabel sizeToFit].

boundingRectWithSize adds extra bottom padding to UILabel

UILabel is created like this:
label = [[UILabel alloc] initWithFrame:CGRectMake(32, 10, 268, 44)];
label.textAlignment = NSTextAlignmentLeft;
label.backgroundColor = [UIColor yellowColor];
label.numberOfLines = 2;
label.lineBreakMode = NSLineBreakByTruncatingTail;
Then to update the label I do this:
label.text = someText;
CGRect frame = label.frame;
NSDictionary *attributes = #{NSFontAttributeName: label.font};
CGRect rect = [someText boundingRectWithSize:CGSizeMake(label.frame.size.width, MAXFLOAT)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:attributes
context:nil];
frame.size.height = rect.size.height;
label.frame = frame;
This works fine with the label only has one line of text, but I have a maximum of two lines. When the 2nd line gets truncated, extra bottom padding gets added to the label, which makes it impossible to position the label below it (the red label in the images):
Why does this happen? If it's a bug, is there a workaround?
Try something like this:
NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:text attributes:attributes];
UILabel *anotherLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, myLabel.frame.size.width, MAXFLOAT)];
[anotherLabel setAttributedText:attrString];
[anotherLabel setNumberOfLines:2];
[anotherLabel setText:NSTextAlignmentLeft];
[anotherLabel setLineBreakMode:NSLineBreakByTruncatingTail];
[anotherLabel sizeToFit];
[myLabel setAttributedText:attrString];
[myLabel setFrame:CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, [anotherLabel frame].size.height)];

UIscrollview frame Changes to super view frame?

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.

Resources