sizeWithFont:minFontSize:actualFontSize:forWidth:lineBreakMode: iOS7 alternative - ios7

Help me please to find an alternative for deprecated method...
CGSize size = [title sizeWithFont:buttonFont
minFontSize:10
actualFontSize:nil
forWidth:_view.bounds.size.width
lineBreakMode:NSLineBreakByClipping];
Can (boundingRectWithSize:options:attributes:context:) do this? Something like this...
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
[UIFont systemFontOfSize:10], NSFontAttributeName,
nil];
CGSize size = [title boundingRectWithSize:CGSizeMake(_view.bounds.size.width-kBorder*2, _view.bounds.size.height)
options:NSLineBreakByClipping
attributes:attributes
context:nil].size;
Am I right?
Looking forward your advices :)

Have a look to a previous answer I made here using this code :
- (CGSize)text:(NSString *)text sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size
{
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(7.0))
{
NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
font, NSFontAttributeName,
nil];
CGRect frame = [text boundingRectWithSize:size
options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading)
attributes:attributesDictionary
context:nil];
return frame.size;
}
else
{
return [text sizeWithFont:font constrainedToSize:size];
}
}

Related

XCode 7 Instruments - Why is this leaking?

I fell into a strange thing: Why is the following code fragment producting leaks (attrDefault not released)? (assume, 'font' has been defined before and self.text refers to an NSString*):
{
NSStringDrawingContext* context = [[NSStringDrawingContext alloc] init];
NSDictionary* attrDefault = #{ NSForegroundColorAttributeName : [UIColor blackColor],
NSBackgroundColorAttributeName : [UIColor clearColor],
NSFontAttributeName : font,
};
frame.size.height = 1024;
CGRect bounding = [self.text boundingRectWithSize:frame.size
options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading|NSStringDrawingUsesDeviceMetrics
attributes:attrDefault context:context];
frame.size.height = bounding.size.height;
[context release];
}
Any idea?
Update:
Surprisingly, if the same code is embedded into an #autoreleasepool statement, the leaks are gone.
#autoreleasepool {
NSStringDrawingContext* context = [[NSStringDrawingContext alloc] init];
NSDictionary* attrDefault = #{ NSForegroundColorAttributeName : [UIColor blackColor],
NSBackgroundColorAttributeName : [UIColor clearColor],
NSFontAttributeName : font,
};
frame.size.height = 1024;
NSString* text = self.text;
CGRect bounding = [text boundingRectWithSize:frame.size
options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading|NSStringDrawingUsesDeviceMetrics
attributes:attrDefault context:context];
frame.size.height = bounding.size.height;
[context release]; }
I did not get any leaks with XCode 6, by the way.
This is pretty strange, as I use NSDictionary constructors ( #{....} ) at a lot of locations in my code and the DO NOT produce any leaks.
Anybody with similiar experience?

SizeWithFont deprecated in IOS7

How to change
CGSize size=[tempStr sizeWithFont:[UIFont boldSystemFontOfSize:17] constrainedToSize:CGSizeMake(200, 56)];
to make it work with IOS7
You have to use sizeWithAttributes:, using something like:
UIFont *font = [UIFont boldSystemFontOfSize:17.0f];
NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
CGSize size = [tempStrig sizeWithAttributes:attrDictionary];
CGFloat finalHeight;
CGSize constrainedSize = CGSizeMake(requiredWidth,9999);
NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:yourRequiredFont, NSFontAttributeName,nil];
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:yourText attributes:attributesDictionary];
CGRect requiredHeight = [string boundingRectWithSize:constrainedSize options:NSStringDrawingUsesLineFragmentOrigin context:nil];
if (requiredHeight.size.width > requiredWidth) {
requiredHeight = CGRectMake(0,0,requiredWidth, requiredHeight.size.height);
}
finalHeight=requiredHeight.size.height;

UIlabel height issue in Custom UItableViewCell in iOS 6 and iOS 7

i am displaying different items in custom table cell. when i try to display multiline uilabel text. it si only showing single line. even though i calculate the label height
also i try to set new frame with new height after calculation.
my code is as follows:
+(CGRect )getlabelHeight:(CGRect)frame withFontName:(NSString *)font andFontSize:(float)fontSize andText:(NSString *)text
{
CGSize constrainedSize = CGSizeMake(frame.size.width, 9999);
NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:font size:fontSize], NSFontAttributeName,
nil];
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:text attributes:attributesDictionary];
CGRect requiredHeight = [string boundingRectWithSize:constrainedSize options:NSStringDrawingUsesLineFragmentOrigin context:nil];
if (requiredHeight.size.width > constrainedSize.width) {
requiredHeight = CGRectMake(0,0, constrainedSize.width, requiredHeight.size.height);
}
CGRect newFrame = frame;
newFrame.size.height = requiredHeight.size.height;
frame = newFrame;
return frame;
}
my tabelview method is as follows:
CGRect descFrame=[Utility getlabelHeight:CGRectMake(65,35+titleFrame.size.height, 250, 20) withFontName:appFont andFontSize:15 andText:[[allComments objectAtIndex:indexPath.row]valueForKey:#"comment"]];
NSLog(#"%# \n %f",[[allComments objectAtIndex:indexPath.row]valueForKey:#"comment"],descFrame.size.height);
[cell.title setFont:[UIFont fontWithName:appFont size:15]];
[cell.title setText:[[allComments objectAtIndex:indexPath.row]valueForKey:#"comment"]];
[cell.title setLineBreakMode:NSLineBreakByWordWrapping];
[cell.title setNumberOfLines:0];
[cell.title setFrame:descFrame];
[cell.title setTextColor:[UIColor blackColor]];
Please help me!!1 i am screwed...
thanks you!!!

sizeWithFont:constrainedToSize - iOS7

I have the following method that I used in iOS6 but with iOS7 I'm getting errors on
CGSize labelHeight = [tweetText sizeWithFont:[UIFont systemFontOfSize:13.0f] constrainedToSize:CGSizeMake(self.tweetsTableView.bounds.size.width - 84, 4000)];
full method below, any ideas on how to amend for iOS7?
- (CGFloat)heightForCellAtIndex:(NSUInteger)index {
NSDictionary *tweet = self.tweets[index];
CGFloat cellHeight = 50;
NSString *tweetText = tweet[#"text"];
CGSize labelHeight = [tweetText sizeWithFont:[UIFont systemFontOfSize:13.0f] constrainedToSize:CGSizeMake(self.tweetsTableView.bounds.size.width - 84, 4000)];
cellHeight += labelHeight.height;
return cellHeight;
}
I know this is an old question & late answer, but it's still very relevant,
This sizeWithFont method is now deprecated, this new method works best
NSString *content = **Whatever your label's content is expected to be**
CGSize maximumLabelSize = CGSizeMake(390, 1000);
NSDictionary *stringAttributes = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:13] forKey: NSFontAttributeName];
CGSize newExpectedLabelSize = [content boundingRectWithSize:maximumLabelSize options:NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesLineFragmentOrigin attributes:stringAttributes context:nil].size;
So you can adjust your label (or table cell etc) to
label.frame.size.height = newExpectedLabelSize.height;
I hope this helps, cheers, Jim.
Add this lines:
UIFont *font = [UIFont boldSystemFontOfSize:16];
CGRect new = [string boundingRectWithSize:CGSizeMake(200, 300) options:NSStringDrawingUsesFontLeading attributes:#{NSFontAttributeName: font} context:nil];
CGSize stringSize= new.size;

NSString sizeWithFont: alternative in iOS7

The method (sizeWithFont: forWidth: lineBreakMode:) was deprecated in iOS 7.0.
But how can I do a same thing like following code in iOS 7.0?
CGSize fontSize =[self.text sizeWithFont:self.font forWidth:self.frame.size.width lineBreakMode:NSLineBreakByTruncatingTail];
Can (boundingRectWithSize:options:attributes:context:) do this? I am not sure but this is the method I searched on apple document.
Thanks for your assistance.
I have got a category for NSString to get the width or heigth of a string:
- (CGFloat)widthWithFont:(UIFont *)font
{
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:font, NSFontAttributeName, nil];
return [[[NSAttributedString alloc] initWithString:self attributes:attributes] size].width;
}
- (CGFloat)heigthWithWidth:(CGFloat)width andFont:(UIFont *)font
{
NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:self];
[attrStr addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, [self length])];
CGRect rect = [attrStr boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:nil];
return rect.size.height;
}
For those who want the height with width function but in swift its:
func HeigthWithWidth(stringToSize : String, width : CGFloat, font : UIFont) -> CGFloat {
var attrStr = NSMutableAttributedString(string: stringToSize);
attrStr.addAttribute(NSFontAttributeName, value: font, range: NSRange.init(location: 0, length: stringToSize.characters.count));
var rect = attrStr.boundingRectWithSize(CGSize(width: width, height: CGFloat.max), options: [NSStringDrawingOptions.UsesLineFragmentOrigin, NSStringDrawingOptions.UsesFontLeading], context: nil);
return rect.size.height;
}
Hope that helps those who come looking later