UILabel numberOfLines - iphone

I need to adjust numberOfLines of label in cellForRowAtIndexPath. Label is in word wrap mode.
However, the actual calculation of label's height is taking place in heightForRowAtIndexPath.
How to calculate numberOfLines in cellForRowAtIndexPath? For now I'm just using some very big number.

From the Apple Docs:
To remove any maximum limit, and use
as many lines as needed, set the value
of this property to 0.
Then you may use this technique in the heightForRowAtIndexPath method to determine how tall the cell should be.
Constants:
static const CGFloat kCellFontSize = 14.0;
static const CGFloat kCellWidth = 300.0;
static const CGFloat kCellHeightMax = 999.0;
static const CGFloat kCellPadding = 10.0;
Method:
CGSize maxSize = CGSizeMake( kCellWidth, kCellHeightMax );
CGSize labelSize =
[[self cellTextString] sizeWithFont: [self cellFont]
constrainedToSize: maxSize
lineBreakMode: UILineBreakModeTailTruncation];
return (labelSize.height + (2 * kCellPadding));

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;

Convert CGSize to CGFloat and use that to size a RoundRectButton

I'm trying to find the physical pixel size of a string of text. I then want to use this size to set the length of a roundRectButton. The method I'm using to get the length however returns a CGSize. How do I convert that to a CGFloat? Alternatively perhaps someone can suggest a totally different way to accomplish this.
This is the code I have currently:
// Note: tagAsString is a string of Tag names (Example "tag1, tag2, tag3")
// Note: Code to set this is not relevant to the question.
// get length of tagsAsString.
CGSize tagStringLength = [tagsAsString sizeWithFont:[UIFont fontWithName:#"Helvetica-Bold" size:13.0] forWidth:100.0 lineBreakMode:UILineBreakModeTailTruncation];
// size button to fit length of string
// Note: spotTags is a roundRectButton
cell.spotTags.frame = CGRectMake(96, 33, tagStringLength, 25);
// set the title of the roundRectButton to the tag string
[cell.spotTags setTitle:tagsAsString forState:UIControlStateNormal];
As you can see above I'm using the CGRectMake method which takes 4 arguments as CGFloat. I am however passing in a CGSize which is causing a run-time error.
CGSize is a structure:
struct CGSize {
CGFloat width;
CGFloat height;
};
typedef struct CGSize CGSize;
Just use tagStringLength.width to get the number you care about.
And I'm pretty sure you should be getting a compile-time error, not a run-time error.
CGSize is a struct of two elements: CGFloat width and CGFloat height.
Example code:
CGSize size;
CGFLoat width = size.width;
CGFLoat height = size.height;

Resizing UITableViewCells in iOS4 versus iOS3

I'm using the following code to size a UITableViewCellStyleSubtitle cell to fit text that can run to more than one line:
- (CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath {
CGRect frame = [UIScreen mainScreen].bounds;
CGFloat width = frame.size.width;
int section = indexPath.section;
int row = indexPath.row;
NSString *title_string = [[my_array_ objectAtIndex:row]
valueForKey:#"keyOne"];
NSString *detail_string = [[my_array_ objectAtIndex:row]
valueForKey:#"keyTwo"];
CGSize title_size = {0, 0};
CGSize detail_size = {0, 0};
if (title_string && ![title_string isEqualToString:#""]) {
title_size = [title_string sizeWithFont:
[UIFont systemFontOfSize:TITLE_FONT_SIZE]
constrainedToSize:CGSizeMake(width, 4000)
lineBreakMode:UILineBreakModeWordWrap];
}
if (detail_string && ![detail_string isEqualToString:#""]) {
detail_size = [detail_string sizeWithFont:
[UIFont systemFontOfSize:DETAIL_FONT_SIZE]
constrainedToSize:CGSizeMake(width, 4000)
lineBreakMode:UILineBreakModeWordWrap];
}
CGFloat title_height = title_size.height;
CGFloat detail_height = detail_size.height;
CGFloat content_size = title_height + detail_height;
CGFloat height;
switch (section) {
case 0:
height = content_size; //+ offset;
break;
default:
height = 44.0;
break;
}
return height;
}
This runs fine in iOS4.* and gives cells that accommodate the text. (If anything they're slightly too big but that's OK.)
But when I try this in iOS3.* the cell is messed up: The textLabel appears low down in the cell and the detailLabel text often then spills over into the next cell or is truncated with "...". The textLabel seems to appear in the middle of the cell, rather than at the top.
What is different between iOS3 and iOS4 that might cause this? What can I do to resolve this? Or, is there a good alternative to the above (say) that will work on both iOSes.
Thanking you kindly in advance.
In this piece of code if you're just calculating the right line height and giving it to the table. So I suppose the cell height will be correct in both cases (iOS 4 and iOS 3). What happens is that you're not doing anything to adjust the two labels in the cell contentView, so probably the iOS 4 cell is auto-adjusting, while iOS 3 no.
Now you may try to study the iOS 3 subtitle cell hierarchy and apply the right changes or you can make your own custom labels and add them to the contentView hierarchy: of course these custom labels will have their frame, number of lines, font, ... calculated in the same way you used in the line height code (so you should start refactoring your code by exporting the line height calculation in a common method).

How to figure out the font size of a UILabel when -adjustsFontSizeToFitWidth is set to YES?

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

String length with given font to fit UITextView 2 - The Return

In this question I asked for a good way to truncate a string to fit a given UITextView. Since there was no way provided by the SDK directly, I've ended up writing the recursive method below (only called by the following public method). However, this doesn't work unless I subtract a fudge factor of 15 (kFudgeFactor) from the field width when calculating the string's height. If I don't do that, the string returned is actually too long for the field, and displays in an extra line below it. Anyone any idea why, and what I should really use instead of this fudge factor?
#pragma mark Size string to fit the new view
#define kFudgeFactor 15.0
#define kMaxFieldHeight 9999.0
// recursive method called by the main API
-(NSString*) sizeStringToFit:(NSString*)aString min:(int)aMin max:(int)aMax
{
if ((aMax-aMin) <= 1)
{
NSString* subString = [aString substringToIndex:aMin];
return subString;
}
int mean = (aMin + aMax)/2;
NSString* subString = [aString substringToIndex:mean];
CGSize tallerSize = CGSizeMake(self.frame.size.width-kFudgeFactor,kMaxFieldHeight);
CGSize stringSize = [subString sizeWithFont:self.font constrainedToSize:tallerSize lineBreakMode:UILineBreakModeWordWrap];
if (stringSize.height <= self.frame.size.height)
return [self sizeStringToFit:aString min:mean max:aMax]; // too small
else
return [self sizeStringToFit:aString min:aMin max:mean];// too big
}
-(NSString*)sizeStringToFit:(NSString*)aString
{
CGSize tallerSize = CGSizeMake(self.frame.size.width-kFudgeFactor,kMaxFieldHeight);
CGSize stringSize = [aString sizeWithFont:self.font constrainedToSize:tallerSize lineBreakMode:UILineBreakModeWordWrap];
// if it fits, just return
if (stringSize.height < self.frame.size.height)
return aString;
// too big - call the recursive method to size it
NSString* smallerString = [self sizeStringToFit:aString min:0 max:[aString length]];
return smallerString;
}
UIScrollView seems to use a fixed 8-pixel inset on both sides. This is independent of alignment or font size (based on testing & observation, not any explicit knowledge of the internals).
So it seems you are right to use your fudge factor, but it should probably be 16.0, not 15.0.
This is probably because the frame of the UIView is not the same size as the content view.
UITextView subclasses from UIScrollView.