I have been trying to get this working for much longer than I care to admin, but I have searched just about every s.o article there is and none of the solutions seem to be doing anything...
My problem is that I am trying to resize a title (UILabel) based on the amount of text, after resizing the title I want to move the image 100 pixels below the title.
The image frame shows that the value has changed to what it has been calculated to be but there is never a change in the application, it only shows the design that was made in the storyboard.
Storyboard layout:
View
Scroll View
Image View
Label - TItle
The code below shows some printouts of values, without running it an example of those are:
Before: [0.000000]
After: [24.000000]
Label Expected Size: 280.000000, 96.000000
After Title Assign: [96.000000]
Image Y Before: [0.000000]
Image Y After: [196.000000]
Code:
self.Title.text = NewsCell.Title;
self.Image.image = NewsCell.Image;
// fit title
printf("Before: [%f]\n", self.Title.frame.size.height);
[self.Title sizeToFit];
printf("After: [%f]\n", self.Title.frame.size.height);
//self.Title.frame = CGRectMake(20, 5, 280, self.Title.frame.size.height);
self.Title.numberOfLines = 0;
CGSize maximumLabelSize = CGSizeMake(280, 100);
CGSize expectedLabelSize = [self.Title.text sizeWithFont:self.Title.font constrainedToSize:maximumLabelSize lineBreakMode:self.Title.lineBreakMode];
self.Title.frame = CGRectMake(20,0,expectedLabelSize.width, expectedLabelSize.height);
printf("Label Expected Size: %f, %f\n", expectedLabelSize.width, expectedLabelSize.height);
printf("After Title Assign: [%f]\n", self.Title.frame.size.height);
// set image location to be bototm of title + 100
printf("Image Y Before: [%f]\n", self.Image.frame.origin.y);
double titleBottom = self.Title.frame.origin.y + self.Title.frame.size.height + 100;
self.Image.frame = CGRectMake(0, titleBottom, 320, 200);
printf("Image Y After: [%f]\n", self.Image.frame.origin.y);
// set description location
double imageBottom = self.Image.frame.size.height + self.Image.frame.origin.y + 5;
// description expand
[self.Description.text stringByAppendingString: #"\n\n © 2013 Geekosystem, LLC"];
[self.Description sizeToFit];
self.Description.frame = CGRectMake(20, imageBottom, 280, self.Description.frame.size.height);
self.Description.numberOfLines = 0;
Related
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....
I have an image for user's icon and a label for user's name
And I want the image and the label align center horizontally in the screen.
Because the length of the label changes as the length of user's name, (the size of image is fixed)
I can't set the positon as a fixed value.
Now I change the positon of the image and lalel at runtime,
It's not convenient.
Is there any good way to do this?
Thank you:)
Here is the snapshot:
All right.
I know there is no a very convenient way to do this just with IB.
And I learn the function of [label sizetoFit].
It's very helpful.
In android, it's very convenient to do this just with the xml layout.
But in ios I have to write code to control the positon of image and label.
Yes...Not too bad.
Thanks everybody :)
[label sizetoFit];
label.center =imageView.center;
this will make the label center on the image center..and it will appear on top...
now you can just use CGRect manipulation to move the label origin downwards based on your image height.
Edit .. now you can do this ..
CGRect frame = label.frame;
frame.origin.x -= imageview.frame.size.width/2 - 15; // you can change 15 to a more appropriate number based on your preference..
label.frame = frame;
Do the above after you do the 2 lines of code given on top in my answer.
Do some calculation
Width of UIImageView + Offset width (Space b/n UIImageView and UILabel "Keep this as constant value") + UILabel Width = You Will get some 'X' width.
iPhone screen width is 460.
320-X = Remaining width.
Remaining width/2 pass this value to UIImageView's X value. This might help you.
#define MARGIN 10.0f
In your -layoutSubviews method, do this:
[ label sizeToFit ] ;
CGRect r = label.frame ;
r.origin = (CGPoint){ CGRectGetMaxX( imageView.frame ) + MARGIN, CGRectGetMidY( imageView.frame ) - 0.5 * r.size.height } ;
label.frame = CGRectIntegral( r ) ;
update here's how to center the the image view and label unit
bounds should be set to bounds of enclosing view
//
// center an image view + label in parent view, with label to right of image view
//
[ label sizeToFit ] ;
CGSize size = (CGSize){ imageView.image.size.width + MARGIN + label.bounds.size.width,
MAX( imageView.image.size.height, label.bounds.size.height } ;
CGRect r = (CGRect){
{ CGRectGetMidX( bounds ) - 0.5 * size.width,
CGRectGetMidY( bounds ) - 0.5 * size.height },
size }
} ;
CGRect imageFrame, labelFrame ;
CGRectDivide( r, &imageFrame, &labelFrame, imageView.image.size.width, CGRectMinXEdge ) ;
imageView.frame = imageFrame ;
labelView.frame = labelFrame ;
in my application i have a textview . i am displaying some string content in the my textview programmically . I have to place a UIButton named 'Send' after the texView . Means here i am parsing the string content from an xml file . So the string length should vary in every case .
But i have to place a Send button after the textView . How to do it . I found the following code snippet . But sometimes it displayig the send button in position after the textView. Sometimes its not in the position (its overlapping) . help me ...
newsSecondPart is my textView .....
int secondStringLength = secondStr.length;
int height = secondStringLength / 48;
height = height * 18.0;
//newsSecondPart is my textView
CGRect newsSecondPartFrame = newsSecondPart.frame;
if (height < 400)
newsSecondPartFrame.size.height = height + 475; // for text unhiding
else
newsSecondPartFrame.size.height = height + 975; // for text unhiding
if (secondStringLength > 5000) {
newsSecondPartFrame.size.height += 150;
}
newsSecondPart.frame = newsSecondPartFrame;
CGRect viewFrame = superView.frame;
viewFrame.size.height = newsSecondPartFrame.size.height + 200 ;
superView.frame = viewFrame;
scrollView.contentSize = superView.bounds.size;
scrollView.contentSize = CGSizeMake( 0, newsSecondPartFrame.size.height+300);
newsFirstPart.text = firstStr;
newsSecondPart.text = secondStr;
Please help me .....
To get dynamic size of textview, you need following code
CGSize size = [secondStr sizeWithFont:txtView.font constrainedToSize:CGSizeMake(txtView.frame.size.width,10000)];
size.height +=10;
This way you will get dynamic height of string.
Now you can set frame of textview
CGRect frame = txtView.frame;
frame.size.height= size.height;
txtView.frame = frame;
Now you can set button frame:
CGRect frame = btnSend.frame;
frame.origin.y = txtView.frame.origin.y + txtView.frame.size.height+10;
btnSend.frame = frame;
Hope this helps.
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.
When myLabel.adjustsFontSizeToFitWidth = YES, UILabel will adjust the font size automatically in case the text is too long for the label. For example, if my label is just 100px wide, and my text is too long to fit with the current font size, it will shrink down the font size until the text fits into the label.
I need to get the actual displayed font size from UILabel when the font size got shrunk down. For example, let's say my font size was actually 20, but UILabel had to shrink it down to 10. When I ask UILabel for the font and the font size, I get my old font size (20), but not the one that's displayed (10).
I'm not sure if this is entirely accurate, but it should be pretty close, hopefully. It may not take truncated strings into account, or the height of the label, but that's something you might be able to do manually.
The method
- (CGSize)sizeWithFont:(UIFont *)font minFontSize:(CGFloat)minFontSize actualFontSize:(CGFloat *)actualFontSize forWidth:(CGFloat)width lineBreakMode:(UILineBreakMode)lineBreakMode
will return the text size, and notice that it also has a reference parameter for the actual font size used.
In case anybody still needs the answer.
In iOS9 you can use boundingRectWithSize:options:context: to calculate actual font size. Note that context.minimumScaleFactor should not be 0.0 for scaling to work.
- (CGFloat)adjustedFontSizeForLabel:(UILabel *)label {
NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithAttributedString:label.attributedText];
[text setAttributes:#{NSFontAttributeName:label.font} range:NSMakeRange(0, text.length)];
NSStringDrawingContext *context = [NSStringDrawingContext new];
context.minimumScaleFactor = label.minimumScaleFactor;
[text boundingRectWithSize:label.frame.size options:NSStringDrawingUsesLineFragmentOrigin context:context];
CGFloat adjustedFontSize = label.font.pointSize * context.actualScaleFactor;
return adjustedFontSize;
}
For one-line UILabel works fine this simple solution:
//myLabel - initial label
UILabel *fullSizeLabel = [UILabel new];
fullSizeLabel.font = myLabel.font;
fullSizeLabel.text = myLabel.text;
[fullSizeLabel sizeToFit];
CGFloat actualFontSize = myLabel.font.pointSize * (myLabel.bounds.size.width / fullSizeLabel.bounds.size.width);
//correct, if new font size bigger than initial
actualFontSize = actualFontSize < myLabel.font.pointSize ? actualFontSize : myLabel.font.pointSize;
Swift 5
For one-line UILabel
extension UILabel {
var actualFontSize: CGFloat {
//initial label
let fullSizeLabel = UILabel()
fullSizeLabel.font = self.font
fullSizeLabel.text = self.text
fullSizeLabel.sizeToFit()
var actualFontSize: CGFloat = self.font.pointSize * (self.bounds.size.width / fullSizeLabel.bounds.size.width);
//correct, if new font size bigger than initial
actualFontSize = actualFontSize < self.font.pointSize ? actualFontSize : self.font.pointSize;
return actualFontSize
}
}
Getting the actual font size is then as simple as:
let currentLabelFontSize = myLabel.actualFontSize
UILabel *txtLabel = [[UILabel alloc] initWithFrame:rectMax];
txtLabel.numberOfLines = 1;
txtLabel.font = self.fontMax;
txtLabel.adjustsFontSizeToFitWidth = YES;
txtLabel.minimumScaleFactor = 0.1;
[txtLabel setText:strMax];
UILabel *fullSizeLabel = [UILabel new];
fullSizeLabel.font = txtLabel.font;
fullSizeLabel.text = txtLabel.text;
fullSizeLabel.numberOfLines = 1;
[fullSizeLabel sizeToFit];
CGFloat actualFontSize = txtLabel.font.pointSize * (txtLabel.bounds.size.width / fullSizeLabel.bounds.size.width);
actualFontSize = actualFontSize < txtLabel.font.pointSize ? actualFontSize : txtLabel.font.pointSize;
// the actual font
self.fontMax = [UIFont fontWithName:self.fontMax.fontName size:actualFontSize];
my code works great, part from #Igor