So I have a custom table view cell and I want a label to be resized based on the text in it. I've already tried doing this in the cellForRowAtIndexPath method-
cell.titleLabel.adjustsFontSizeToFitWidth = YES;
cell.titleLabel.numberOfLines = 1;
[cell.titleLabel setMinimumScaleFactor:8.0/[UIFont labelFontSize]];
I also tried setting minimum scale factor and minimum font size in IB...but the label font doesn't resize.
The case I want to take care of is the case where the text in the label is larger than the phone width and so I want to resize when that happens.
What's going wrong/How do I achieve that?
Related
I have a problem. I'm trying to implement dynamic cell height in the annex, when I have nothing. Normally, in my cell I have one UIImageview and three UILabel. My task is to implement dynamic height of the text without changing the picture size. The picture comes from the server and scaled inside the cell with the mod Scale to fill. My Text does not change its height in the cell.
self.tableView.estimatedRowHeight = 415.0f;
self.tableView.rowHeight = UITableViewAutomaticDimension;
I always found numberOfLines = 0;
The problem is that the picture is stretched to the server, which should not be a text of its size does not change.
The problem is that you have not placed any limiting constraints on the image view's size. Therefore it adopts the size of the image.
I have a universal app which i used the story board to constrain a button so that from the iPhone 4 to the iPhone 6plus, the size will be well proportioned to the device's screen. The title of the button is the number "2" (All that is shown on the screen is the number "2" which can be pressed). For some reason I cannot figure out how to auto resize the font with the button size so that the number looks proportioned to the screen as intended.
Ideas?
Use Interface Builder only. You can add an UILabel as the same size as your button. Remove the title of Your button. Set text for the UILabel. Then change the AutoShrink property of the UILabel to "Minimum Font Scale". But you will not able to change the text color for different states.
Use code to fit width. Try this:
self.yourButton.titleLabel.adjustsFontSizeToFitWidth = YES;
self.yourButton.titleLabel.minimumScaleFactor = 0.5;
If you want to fit the height of title. Maybe you must calculate the height by yourself. Use [font lineHeight] to make sure the font fit your button.
Call this function (from e.g. viewDidAppear) for every button in your app:
func setAppropriateFontSize( butt:UIButton){
let maxFontSize = butt.frame.height
butt.titleLabel!.font = UIFont(name: "Helvetica", maxFontSize)
butt.titleLabel!.adjustsFontSizeToFitWidth = true
butt.titleLabel!.minimumScaleFactor = 0.01
}
Above, maxFontSize is the maximum font-size for which the text still fits vertically within the button. This font is probably too large, and therefore we allow it to shrink (adjustsFontSizeToFitWidth = true means that we allow the font to shrink, as it will never increase to fit the width).
My Xcode 5 project has a label where the User enters a string of numbers. That string is then sent to one of two other labels (either labelA or labelB) by the User pressing one of two buttons. Font size in both labelA and labelB are dynamically resized (if necessary) using:
_labelA.numberOfLines = 1;
_labelA.minimumScaleFactor = 8./_inchesDisplayLabel.font.pointSize;
_labelA.adjustsFontSizeToFitWidth = YES;
and:
_labelB.numberOfLines = 1;
_labelB.minimumScaleFactor = 8./_inchesDisplayLabel.font.pointSize;
_labelB.adjustsFontSizeToFitWidth = YES;
If a string in labelA gets resized to fit within the label, how can the font size in labelB be programmatically resized to match the smaller resized font in labelA? Thanks!
After labelA's font gets resized, use this to make labelB's font match labelA's:
[labelB setFont:[UIFont fontWithName:labelA.font.fontName size:labelA.font.pointSize]];
i'm currently making an app where the suer selects an MKMapView annotation and then the title of the annotation(pin) is set to a detailtextLabel in a Right Detail UITableViewCell.
My Problem is that when the text is large, the detailTextLabel becomes multiple lines. When this happens the TextLabel of the cell(the one the left) shifts up. Heres What I've tried:
In the cellForRowAtIndexPath Method, I tried adjusting the frame through the following code:
CGRect frame = cell.textLabel.frame;
frame.origin.y = cell.frame.size.height/2;
cell.textLabel.frame = frame;
Where cell is a UITableViewCell that is set to right detail
Subclass the cell and tun try to adjust the frame in the -(void)layoutSubviews
How do I stop it from going up and keep it at the center of the cell?
If you want to do a custom layout of UITableViewCell, then you need to add your custom UI elements to its -[UITableViewCell contentView] instead of trying to modify geometry of standard UI elements that are created by default.
So, in your case, you need to add two UILabels and set their position so that:
Title label will not move at all
Detail text label will be also multiline
In this way you'll be able to solve this problem!
Please try to make the font size adjustable amount to the text.
I think you can use,
cell.detailTextLabel.adjustFontSizeToWidth = Yes;
And also set the numberOfLines to 0.
Hope that solves the purpose.
This question already has answers here:
Calling heightForRowAtIndexPath from within cellForRowAtIndexPath?
(3 answers)
Closed 10 years ago.
If I'm making a custom UITableViewCell and I need it to have a variable height depending on the length of the input (for instance, for a Twitter reader or something), how can I make this work?
I've found lots of other examples here that can set it for a the standard, non-custom cell, but my cell's main text label is smaller, etc., so when I try to use any of those methods on my cell, they give a variety of weird results (text overlapping the bottom of the cell, etc.)
Is there a standardized way of designing the cell (for example, how tall should I make it in Interface Builder?), and let's say my label was half the width of that cell.. how would I go about calculating the height the cell would need to be to display the string loaded into that label? Here's a method that I found here which works fine on the normal cell, but screws up custom ones with weird heights, overlapping text, etc: (I have absolutely NO idea what the 300 / 200000 do here, if anyone could explain that I'd be grateful, too!)
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
CGSize textSize = {300.f, 200000.0f};
CGSize size = [[_quoteStringsFromPlist objectAtIndex: indexPath.row] sizeWithFont: [UIFont systemFontOfSize:12.0f] constrainedToSize: textSize lineBreakMode: NSLineBreakByWordWrapping];
size.height += 5.0f;
float result = MAX(size.height, 32.0f);
return result;
}
Something like this should work.
CGRect labelFrame = // enter default frame of the label here
UIFont *labelFont = // enter label font here
CGFloat labelBottomMargin = // enter the space between the bottom of the label and the bottom of the cell here
CGSize size = [labelString sizeWithFont:labelFont constrainedToSize:CGSizeMake(labelFrame.size.width, MAX_FLOAT)];
size.height += labeFrame.origin.y;
size.height += labelBottomMargin;
size.height = MAX(size.height, 32.0f);
return size.height;
First, head over to your xib file and see how large are the top/bottom margins of your text area, and how wide it is.
After that, you need to calculate the height needed for it with the width you have available, and then add that value to the top/bottom margins your text area already has.
The result should look correctly regardless of the size of each cell in IB or the text you are trying to put in them.
EDIT
Imagine your cell size is {700, 300} in your IB, and your text area is located on {{100, 100}, {300,100}}, your text area has a 100px margin top and 100px margin bot, and it's width is 300.
When you calculate the height you require for your text, you calculate it with an available 300 width. It will return a size, something like {300, 250}. That 250 is the height required by your text, but your cell has other stuff in it and you need to add those top and bot margins to that, so the result is 450.
Remember to set an autoresizing mask or autolayout so that your text area stretches vertically and the margins are fixed (UIViewAutoresizingFlexibleHeight)