iOS: CGRectMake doesn't change position of a UILabel - iphone

I've created a label via storyboard. The order is like this:
View -> Scroll View -> Bunch of labels.
Here is the text output I want to achieve:
2 December <30 days left>
the <> enclosed text is in smaller letters an in a different UI Label.
Since the month name is dynamic and the width changes I've tried using CGRectMake to alter the position of the <> enclosed text.
But it isn't working. I tried changing the parameters of the function but the label stays put. It doesn't move from its initial position set in storyboard.
label_days_left.frame = CGRectMake(50, 10, 100, 99);
EDIT:
- (void) alignDaysLeft{
//code to align the label just after the match date
CGFloat dateLabelWidth = [label_date.text sizeWithFont:label_date.font].width;
CGFloat dateLabelHeight = [label_date.text sizeWithFont:label_date.font].height;
CGFloat someGap = 0;
CGFloat daysLeftX = label_date.frame.origin.x + dateLabelWidth + someGap;
CGFloat daysLeftY = label_date.frame.origin.y;
[label_date sizeToFit];
//label_days_left.frame = CGRectMake(daysLeftX, daysLeftY, 100, dateLabelHeight);
label_days_left.frame = CGRectMake(50, 10, 100, 99);
label_days_left.text = [NSString stringWithFormat:#"lolo"];
NSLog(#"date width - %f", daysLeftX);
}
label_date is the variable which contains the month and day.
label_days_left is the label variable which I am trying to place next to the month.
EDIT 2:
EDIT 3:
I created a new project.
Dragged a label on the view.
Created an outlet.
Synthesized it.
And used the following code to move it -
[labia setFrame:CGRectMake(0, 100, 5, 99)];
It didn't work. What mistake am I doing?

Do you have AutoLayout turned on? If so then that might be your problem, as you are not supposed to worry about setting the frame with AutoLayout, and instead set constraints.

NSString *someString = [NSString stringWithFormat:#"%#",label_date.text]
UIFont *yourFont = [UIFont systemFontOfSize:22.0];
CGSize stringBoundingBox = [someString sizeWithFont:yourFont];
[label_days_left setFrame:CGRectMake(label_days_left.frame.origin.x, 10, stringBoundingBox.width, 99)];
[label_days_left setText:someString];
Pls try this once....

Related

Monotouch - calculate UILabel height

I am trying to create a custom cell which consists a few UILabels.
The first label might take one or more rows, so I need to resize the label according to the number of lines (after setting the number of lines to 0, so multi-line will be enabled).
I have tried setting sizeToFit(), but it changed the alignment and width of my label.
I found this answer
but I don't know how to convert it to C#.
Can anyone point me to an example? (I already tried Googling it off-course)
This is the method from the link:
// UILabel *myLabel;
CGSize labelSize = [myLabel.text sizeWithFont:myLabel.font
constrainedToSize:myLabel.frame.size
lineBreakMode:UILineBreakModeWordWrap];
CGFloat labelHeight = labelSize.height;
int lines = [myLabel.text sizeWithFont:myLabel.font
constrainedToSize:myLabel.frame.size
lineBreakMode:UILineBreakModeWordWrap].height/16;
// '16' is font size
var size = myLabel.StringSize("Some really long string", myLabel.Font, myLabel.Frame.Size, UILineBreakMode.CharacterWrap);
var lines = size.Height / myLabel.Font.CapHeight;

UItextView text hidden

I am notes.app like app. I am setting UITextView's contentInset as
myTextView.contentInset = UIEdgeInsetsMake(0, 25, 0, -25);
myTextView.contentSize = CGSizeMake(295, myTextView.contentSize.height);
but that does not seems to work as I expected. Two characters are hidden.
I get this
but if I set
myTextView.contentInset = UIEdgeInsetsMake(0, 25, 0, 0);
myTextView.contentSize = CGSizeMake(295, myTextView.contentSize.height);
I get actual text as
please help.
fibnochi you can use a different approach to do this. I have used this in one of my app and it is working fine
First of all you need to get the size of UItextView which will carry all the data, for this you need to use NSString class delegate function
NSString* str = #"Your Entire string will come here";
CGSize size = [str sizeWithFont:[UIFont systemFontOfSize:17.0] constrainedToSize:CGSize(295, any large digit for maximum height say 10000) lineBreakMode:UILineBreakModeWordWrap];
textView.contentSize = size;
Will this solve your problem or not. Please let me know
Can you please try the below?
myTextView.contentInset = UIEdgeInsetsMake(0, 25, 0, 25);
myTextView.contentSize = CGSizeMake(295, myTextView.contentSize.height);
Apple's discussions
here.
"
Edge inset values are applied to a rectangle to shrink or expand the area represented by that rectangle. Typically, edge insets are used during view layout to modify the view’s frame. Positive values cause the frame to be inset (or shrunk) by the specified amount. Negative values cause the frame to be outset (or expanded) by the specified amount.
"
You can do like UIEdgeInsetsMake(0,25,0,25)
try this code :)
myTextView.contentInset = UIEdgeInsetsMake(0, 25, 0, -25);
myTextView.contentSize = CGSizeMake(270, myTextView.contentSize.height);
May this will help you out :)

How to find the end coordinates of UILabel?

So I am dynamically adding UILabels to a view along a "timeline". What I would like to do is figure out how to put the starting coordinates of the next UILabel a couple of pixels past where the last one was. I'm having a hard time figuring out where the last one ended. I've looked at a few similar questions and I can't seem to grasp how to do it. Everything is taking place in a for loop so I can update the xcoordinate variable each loop, but I need to know how to get the label size. Anyone have any ideas? I tried this but it didn't seem to work:
UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake(labelsXpoint,
topLabelYpoint,
labelWidth,
20)];
CGSize labelSize = [title.text sizeWithFont:title.font constrainedToSize:title.frame.size
lineBreakMode:title.lineBreakMode];
labelsXpoint += labelSize.width;
This is how you would get the top right & bottom right coordinates of the title label:
CGPoint topRight = CGPointMake(title.frame.origin.x + title.frame.size.width, title.frame.origin.y);
CGPoint bottomRight = CGPointMake(title.frame.origin.x + title.frame.size.width, title.frame.origin.y + title.frame.size.height);
I would set the frame after you alter labelsXpoint. Something like:
UILabel *title = [[UILabel alloc]init];
CGSize labelSize = [title.text sizeWithFont:title.font constrainedToSize:title.frame.size lineBreakMode:title.lineBreakMode];
labelsXpoint += labelSize.width;
title.frame = CGRectMake(labelsXpoint, topLabelYpoint, labelWidth, 20);
You can use the functions: CGRectGetMaxX and CGRectGetMaxY:
CGPoint topRight = CGPointMake(CGRectGetMaxX(title.frame), title.frame.origin.y);
CGPoint bottomRight = CGPointMake(CGRectGetMaxX(title.frame), CGRectGetMaxY(title.frame));

How to resize scrollView content and put textViews in that according to their text size? [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
How can i fix my scrollView dynamically when i have more than one textViews in it?
I made a scrollView in interface builder and put 3 textViews and 2 button in that. Sometimes textViews have more text and sometimes less. i managed to give them flexible frame size. but if top textView has more text then some of its text comes over 2nd textView. i tried this code. i think it should work but its not working properly.
storyTitle.text = sTitle;
storyTitle.font = [UIFont boldSystemFontOfSize:18];
date.text = sDate;
date.font = [UIFont systemFontOfSize:16];
summary.text = sSummary;
summary.font = [UIFont systemFontOfSize:16];
CGRect titleFrame = storyTitle.frame;
titleFrame.size.height = storyTitle.contentSize.height;
storyTitle.frame = titleFrame;
CGRect dateFrame = date.frame;
dateFrame.size.height = label.contentSize.height;
date.frame = dateFrame;
CGRect summaryFrame = summary.frame;
summaryFrame.size.height = summary.contentSize.height;
summary.frame = summaryFrame;
CGRect topTextViewFrame = storyTitle.frame;
CGRect middleTextViewFrame = date.frame;
NSLog(#"size: %d",topTextViewFrame.origin.y);
middleTextViewFrame.origin.y = topTextViewFrame.origin.y + topTextViewFrame.size.height;
CGRect bottomTextViewFrame = summary.frame;
bottomTextViewFrame.origin.y = middleTextViewFrame.origin.y + middleTextViewFrame.size.height;
// then adjust other controls based on these frames, for example:
CGRect myButtonFrame = detailButton.frame;
myButtonFrame.origin.y = bottomTextViewFrame.origin.y + bottomTextViewFrame.size.height;
CGRect myButtonFrame2 = fbButton.frame;
myButtonFrame2.origin.y = myButtonFrame.origin.y + myButtonFrame.size.height;
// finally adjust the contentSize of the scrollview assuming the button is the bottom element
CGSize csize = scrollView.contentSize;
csize.height = myButtonFrame2.origin.y + myButtonFrame2.size.height;
scrollView.contentSize = csize;
can anyone tell me whats wrong with this? and how should i right it so it ll work fine. thanx in advance
hi if your text is not editable by user the try using UILabel instead of UITextView.If you set numberOfLines property of UILabel = 0 then it can go to more that one line and then u can calculate the required height for the label by the method
CGSize size = [label.text sizeWithFont:label.font constrainedToSize:CGSizeMake(label.frame.size.width, FLT_MAX) lineBreakMode:UILineBreakModeWordWrap ];
the u can set the frame of label according to height you get form here like
label.frame = CGRectMake(label.frame.origin.x,label.frame.origin.y,label.frame.size.width,size.height);
and if you have such multiple labels the uyou can do following
CGSize size = [label1.text sizeWithFont:label1.font constrainedToSize:CGSizeMake(label1.frame.size.width, FLT_MAX) lineBreakMode:UILineBreakModeWordWrap ];
label1.frame = CGRectMake(label1.frame.origin.x,label1.frame.origin.y,label1.frame.size.width,size.height);
size = [label2.text sizeWithFont:label2.font constrainedToSize:CGSizeMake(label2.frame.size.width, FLT_MAX) lineBreakMode:UILineBreakModeWordWrap ];
label2.frame = CGRectMake(label2.frame.origin.x,label1.frame.origin.y+label1.frame.size.height+10,label2.frame.size.width,size.height);
size = [label3.text sizeWithFont:label3.font constrainedToSize:CGSizeMake(label3.frame.size.width, FLT_MAX) lineBreakMode:UILineBreakModeWordWrap ];
label3.frame = CGRectMake(label3.frame.origin.x,label2.frame.origin.y+label2.frame.size.height+10,label3.frame.size.width,size.height);
and finaly you can adjust the content height of your scroll view according tho last label.

NSString sizeWithFont: returning inconsistent results? known bug?

I'm trying to create a simple custom UIView wich contain a string drawn with a single font, but where the first character is slightly larger.
I thought this would be easily implemented with two UILabel:s placed next to eachother.
I use NSString sizeWithFont to measure my string to be able to lay it out correctly.
But I noticed that the font baseline in the returned rectangle varies with +/- 1 pixel depending on the font size I set.
Here is my code:
NSString* ctxt = [text substringToIndex:1];
NSString* ttxt = [text substringFromIndex:1];
CGSize sz = [ctxt sizeWithFont: cfont ];
clbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, sz.width, sz.height)];
clbl.text = ctxt;
clbl.font = cfont;
clbl.backgroundColor = [UIColor clearColor];
[contentView addSubview:clbl];
CGSize sz2 = [ttxt sizeWithFont: tfont];
tlbl = [[UILabel alloc] initWithFrame:CGRectMake(sz.width, (sz.height - sz2.height), sz2.width, sz2.height)];
tlbl.text = ttxt;
tlbl.font = tfont;
tlbl.backgroundColor = [UIColor clearColor];
[contentView addSubview:tlbl];
If I use 12.0 and 14.0 as sizes, it works fine.
But if I instead use 13.0 and 15.0, then the first character is 1 pixel too high.
Is this a known problem?
Any suggestions how to work around it?
Creating a UIWebView with a CSS and HTML page seems way overkill for this. and more work to handle dynamic strings. Is that what I'm expected to do?
Found the answer...
Ofcourse, I also have to check the descender value on the font, and compensate for that in the layout.
New rect for the second label is:
CGRectMake(sz.width, (sz.height - sz2.height) + floor(cfont.descender - tfont.descender), sz2.width, sz2.height)
floor() is to make sure it snaps to pixel position, or the font will look blurry