Rectangle 27 9

In iOS7 I needed the logic to return the correct height for the tableview:heightForRowAtIndexPath method, but the sizeWithAttributes always returns the same height regardless of the string length because it doesn't know that it is going to be put in a fixed width table cell. I found this works great for me and calculates the correct height taking in consideration the width for the table cell! This is based on Mr. T's answer above.

NSString *text = @"The text that I want to wrap in a table cell."

CGFloat width = tableView.frame.size.width - 15 - 30 - 15;  //tableView width - left border width - accessory indicator - right border width
UIFont *font = [UIFont systemFontOfSize:17];
NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:text attributes:@{NSFontAttributeName: font}];
CGRect rect = [attributedText boundingRectWithSize:(CGSize){width, CGFLOAT_MAX}
                                           options:NSStringDrawingUsesLineFragmentOrigin
                                           context:nil];
CGSize size = rect.size;
size.height = ceilf(size.height);
size.width  = ceilf(size.width);
return size.height + 15;  //Add a little more padding for big thumbs and the detailText label

objective c - Replacement for deprecated sizeWithFont: in iOS 7? - Sta...

objective-c deprecated ios7
Rectangle 27 9

In iOS7 I needed the logic to return the correct height for the tableview:heightForRowAtIndexPath method, but the sizeWithAttributes always returns the same height regardless of the string length because it doesn't know that it is going to be put in a fixed width table cell. I found this works great for me and calculates the correct height taking in consideration the width for the table cell! This is based on Mr. T's answer above.

NSString *text = @"The text that I want to wrap in a table cell."

CGFloat width = tableView.frame.size.width - 15 - 30 - 15;  //tableView width - left border width - accessory indicator - right border width
UIFont *font = [UIFont systemFontOfSize:17];
NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:text attributes:@{NSFontAttributeName: font}];
CGRect rect = [attributedText boundingRectWithSize:(CGSize){width, CGFLOAT_MAX}
                                           options:NSStringDrawingUsesLineFragmentOrigin
                                           context:nil];
CGSize size = rect.size;
size.height = ceilf(size.height);
size.width  = ceilf(size.width);
return size.height + 15;  //Add a little more padding for big thumbs and the detailText label

objective c - Replacement for deprecated sizeWithFont: in iOS 7? - Sta...

objective-c deprecated ios7
Rectangle 27 1

After googling for a long time I did not find a solution working under iOS7. Right now I use the following workaround, knowing that it is very ugly. I render a UILabel in memory, take a screenshot and draw that. UILabel is able to shrink the text correctly.

UILabel *myLabel = [[UILabel alloc] initWithFrame:myLabelFrame];
    myLabel.font = [UIFont fontWithName:@"HelveticaNeue-BoldItalic" size:16];
    myLabel.text = @"Some text that is too long";
    myLabel.minimumScaleFactor = 0.5;
    myLabel.adjustsFontSizeToFitWidth = YES;
    myLabel.backgroundColor = [UIColor clearColor];

    UIGraphicsBeginImageContextWithOptions(myLabelFrame.size, NO, 0.0f);
    [[myLabel layer] renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    [screenshot drawInRect:myLabel.frame];

this workaround actually works, even if it is a bit dirty. Thanks!

ios - iOS7's deprecation of NSString's drawAtPoint:forWidth:withFont:m...

ios objective-c nsstring ios7 deprecated
Rectangle 27 2

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(@"didUpdateToLocation: %@", newLocation);
[locationManager stopUpdatingLocation];

CLLocation *currentLocation = newLocation;

if (currentLocation != nil) {

 GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:newLocation.coordinate.latitude
                                                    longitude:newLocation.coordinate.longitude
                                                         zoom:17.0];
  [googleMapView animateToCameraPosition:camera];

    NSString *getcurrlong = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude];
    NSString *getcurrlat = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];


}
}
func locationManager(_ manager: CLLocationManager, didUpdateTo newLocation: CLLocation, from oldLocation: CLLocation) {
print("didUpdateToLocation: \(newLocation)")
locationManager.stopUpdatingLocation()
let currentLocation: CLLocation? = newLocation
if currentLocation != nil {
    let camera = GMSCameraPosition.camera(with: newLocation.coordinate.latitude, longitude: newLocation.coordinate.longitude, zoom: 17.0)
    googleMapView.animate(to: camera)
    var getcurrlong = String(format: "%.8f", currentLocation?.coordinate?.longitude)
    var getcurrlat = String(format: "%.8f", currentLocation?.coordinate?.latitude)
}
}

In swift 3 i have used didUpdateLocations instead of didUpdateToLocation

ios - How to store the current location Latitude and Longitude in NSSt...

ios google-maps nsstring google-maps-sdk-ios
Rectangle 27 0

In my case when I used NSASCIIStringEncoding NSString was returning nil on iOS7 (it works good on iOS6), NSUTF8StringEncoding works fine

I just finished moving my CalcFxC app to iOS 7 and there were a lot of things that have worked since iOS 4 that no longer worked. Many to do with text and UITextView.

email - Mail Attachment Not Being Sent from iOS App that has been in A...

ios email email-attachments mfmailcomposeviewcontroll
Rectangle 27 0

If I understand correctly, you are using boundingRectWithSize: just as a way of getting the size you would get with sizeWithFont (meaning you want directly the CGSize, not the CGRect)?

This looks like what you are looking for :

They are using sizeWithAttributes: to get the size, as a replacement for sizeWithFont.

Do you still get the wrong size using something like this :

UIFont *fontText = [UIFont fontWithName:[AppHandlers zHandler].fontName size:16];
                    // you can use your font.

expectedLabelSize = [myString sizeWithAttributes:@{NSFontAttributeName:fontText}];

but how can i use CGSize maximumLabelSize = CGSizeMake(310, 9999)? I need correct height and width.

Is there any attributes that tells sizeWithAttributes: to constraint its width/height?

Sorry, but I don't get what your problem is. You want to size the label with the font size, but not more than a maximum height, if I understand correctly. You say that you are able to do that with sizeWithFont, but you want an alternative now that this method is deprecated, correct ? You should be able to plug in sizeWithAttribues: in the same way you were using sizeWithFont: . How were you constraining the label size when you were using sizeWithFont ? Can you post the code that was working for you using sizeWithFont ?

He was using sizeWithFont:constrainedToSize:lineBreakMode: and there is no way to pass constrainedToSize value to sizeWithAttributes.

objective c - sizeWithFont method is deprecated. boundingRectWithSize ...

objective-c nsstring ios7 uilabel
Rectangle 27 0

for finding size of label run time sizewithfont is deprecated for iOS 7.0 instead of that you have to use -boundingRectWithSize:options:attributes:context: method

you can use it like below code

CGSize constraint = CGSizeMake(MAXIMUM_WIDHT, TEMP_HEIGHT);
NSRange range = NSMakeRange(0, [[self.message body] length]);

NSDictionary *attributes = [YOUR_LABEL.attributedText attributesAtIndex:0 effectiveRange:&range];
CGSize boundingBox = [myString boundingRectWithSize:constraint options:NSStringDrawingUsesFontLeading attributes:attributes context:nil].size;
int numberOfLine = ceil((boundingBox.width) / YOUR_LABEL.frame.size.width);
CGSize descSize = CGSizeMake(ceil(boundingBox.width), ceil(self.lblMessageDetail.frame.size.height*numberOfLine));

CGRect frame=YOUR_LABEL.frame;
frame.size.height=descSize.height;
YOUR_LABEL.frame=frame;

here you have to give width to maximum length for finding height or width.

objective c - sizeWithFont method is deprecated. boundingRectWithSize ...

objective-c nsstring ios7 uilabel
Rectangle 27 0

+ (CGSize)text:(NSString*)text sizeWithFont:(UIFont*)font {    
    CGSize size = [text sizeWithAttributes:@{NSFontAttributeName: font}];
    return CGSizeMake(ceilf(size.width), ceilf(size.height));
}

+ (CGSize)text:(NSString*)text sizeWithFont:(UIFont*)font constrainedToSize:(CGSize)size{
    CGRect textRect = [text boundingRectWithSize:size
                                     options:NSStringDrawingUsesLineFragmentOrigin
                                  attributes:@{NSFontAttributeName: font}
                                     context:nil];
    return CGSizeMake(ceilf(textRect.size.width), ceilf(textRect.size.height));
}

iphone - Replacement for deprecated -sizeWithFont:constrainedToSize:li...

ios iphone ios7 nsstring sizewithfont
Rectangle 27 0

UIFont *font = [UIFont boldSystemFontOfSize:16];
CGRect new = [string boundingRectWithSize:CGSizeMake(200, 300) options:NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName: font} context:nil];
CGSize stringSize= new.size;

ios - sizeWithFont: replacements - Stack Overflow

ios nsstring ios7 nsattributedstring
Rectangle 27 0

Ran into this issue myself. it seems they fixed a bug from iOS6 to iOS7 regarding [NSString keyCString:maxLength:encoding] The old method on iOS6 would cut off part of the buffer to fill the keyPtr.

An easy fix would be to increase the keyPTr size to

char keyPtr = kCCKeySizeAES256 * 2 + 1;

However keep in mind that when you upgrade apps form 6 to 7, while it should work. the keyCString truncates the length to match the size of keyPtr. and replaces the first character with 0. So to make sure that it works on both platforms. Add the above code, and set keyPtr[0] = 0;

objective c - CCCrypt not work on xcode 5 for ios 7 - Stack Overflow

objective-c ios7 xcode5
Rectangle 27 0

NSString* sentence = @"Male, Female";

if ([sentence rangeOfString:@"Male"].location != NSNotFound)
{
   NSLog(@"Male is found");
}

if ([sentence rangeOfString:@"Female"].location != NSNotFound)
{
   NSLog(@"Female is found");
}

Of course, if it's @"female, female" then the search for @"male" will return true.

true but searching for @"Male" won't ;)

ios - How do I search an NSString sentence with words separated by com...

ios objective-c cocoa-touch nsstring nspredicate
Rectangle 27 0

iOS 7 adds the method - (CGSize)sizeWithAttributes:(NSDictionary *)attrs to NSString that gives you the size of a string. Under iOS7, UILabel objects are generally very close in size to this value. It can be called on a background thread.

You can also implement tableView:estimatedHeightForRowAtIndexPath: on your UITableViewDelegate. Providing an estimate the height of rows can improve the user experience when loading the table view. If the table contains variable height rows, it might be expensive to calculate all their heights and so lead to a longer load time. Using estimation allows you to defer some of the cost of geometry calculation from load time to scrolling time.

I did not know about tableView:estimatedHeightForRowAtIndexPath:, it was really all that was needed to fix the performance. Thanks!

ios - Need a faster way to calculate UILabel sizeThatFits - Stack Over...

ios objective-c uilabel
Rectangle 27 0

CGRect textRect = [text boundingRectWithSize:size
                                 options:NSStringDrawingUsesLineFragmentOrigin
                              attributes:@{NSFontAttributeName:FONT}
                                 context:nil];

CGSize size = textRect.size;

Just change "FONT" for an "[UIFont font....]"

And where do you mention NSLineBreakByWordWrapping? Wherre did it go?

NSLineBreakByWordWrapping
NSParagraphStyle
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init]; paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
NSParagraphStyleAttributeName: paragraphStyle.copy

@ffried in my case adding the paragraphStyle with line break other than NSLineBreakByWordWrapping caused size to be calculated for just one line Any thoughts?

boundingRectWithSize:options:attributes:context:
ceil(textRect.size.height)
ceil(textRect.size.width)

What the heck is BOLIVIASize?

iphone - Replacement for deprecated -sizeWithFont:constrainedToSize:li...

ios iphone ios7 nsstring sizewithfont
Rectangle 27 0

As we cant use sizeWithAttributes for all iOS greater than 4.3 we have to write conditional code for 7.0 and previous iOS.

if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")) {
   CGSize size = CGSizeMake(230,9999);
   CGRect textRect = [specialityObj.name  
       boundingRectWithSize:size
                    options:NSStringDrawingUsesLineFragmentOrigin
                 attributes:@{NSFontAttributeName:[UIFont fontWithName:[AppHandlers zHandler].fontName size:14]}
                    context:nil];
   total_height = total_height + textRect.size.height;   
}
else {
   CGSize maximumLabelSize = CGSizeMake(230,9999); 
   expectedLabelSize = [specialityObj.name sizeWithFont:[UIFont fontWithName:[AppHandlers zHandler].fontName size:14] constrainedToSize:maximumLabelSize lineBreakMode:UILineBreakModeWordWrap]; //iOS 6 and previous. 
   total_height = total_height + expectedLabelSize.height;
}
UILabel *gettingSizeLabel = [[UILabel alloc] init];
gettingSizeLabel.font = [UIFont fontWithName:[AppHandlers zHandler].fontName size:16]; // Your Font-style whatever you want to use.
gettingSizeLabel.text = @"YOUR TEXT HERE";
gettingSizeLabel.numberOfLines = 0;
CGSize maximumLabelSize = CGSizeMake(310, 9999); // this width will be as per your requirement

CGSize expectedSize = [gettingSizeLabel sizeThatFits:maximumLabelSize];

The first solution is sometime fail to return proper value of height. so use another solution. which will work perfectly.

The second option is quite well and working smoothly in all iOS without conditional code.

why 230, 999? Where do you get the number>

The 230 can be any number. It represents the width you want for your label. The 9999 I'd rather replace with INFINITY or MAXFLOAT.

the second solution is perfect!!! thanks.

The second solution is working fine, thanks

Second solution is working like a charm. Thanks Nirav.

iphone - Replacement for deprecated -sizeWithFont:constrainedToSize:li...

ios iphone ios7 nsstring sizewithfont
Rectangle 27 0

In most cases I used the method sizeWithFont:constrainedToSize:lineBreakMode: to estimate the minimum size for a UILabel to accomodate its text (especially when the label has to be placed inside a UITableViewCell)...

...If this is exactly your situation you can simpy use the method:

CGSize size = [myLabel textRectForBounds:myLabel.frame limitedToNumberOfLines:mylabel.numberOfLines].size;

Apple's documentation says that you should not call this method directly.

iphone - Replacement for deprecated -sizeWithFont:constrainedToSize:li...

ios iphone ios7 nsstring sizewithfont
Rectangle 27 0

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(@"didUpdateToLocation: %@", newLocation);
[locationManager stopUpdatingLocation];

CLLocation *currentLocation = newLocation;

if (currentLocation != nil) {

 GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:newLocation.coordinate.latitude
                                                    longitude:newLocation.coordinate.longitude
                                                         zoom:17.0];
  [googleMapView animateToCameraPosition:camera];

    NSString *getcurrlong = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude];
    NSString *getcurrlat = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];


}
}

ios - How to store the current location Latitude and Longitude in NSSt...

ios google-maps nsstring google-maps-sdk-ios
Rectangle 27 0

This will work for ios7 and above.

- (UIImage *)decodeBase64ToImage:(NSString *)strEncodeData {
  NSData *data = [[NSData alloc]initWithBase64EncodedString:strEncodeData options:NSDataBase64DecodingIgnoreUnknownCharacters];
  return [UIImage imageWithData:data];
}

And use the code like this:

UIImage *img = [self decodeBase64ToImage:<ur base64 string>];

ios - Decode Base-64 encoded PNG in an NSString - Stack Overflow

ios cocoa-touch base64 nsdata
Rectangle 27 0

Seems like your only option is to implement yourself the logic behind the original function using boundingRectWithSize:options:attributes:context: and the detailed description of the deprecated function (under "Discussion").

Using boundingRectWithSize:options:attributes:context: you can specify a UIFont with a specific size, and get the resulted width. Then you can compare the resulted width with your required width. You still need however some trials and errors to determine the actual font size.

iphone - Which alternative to use for sizeWithFont:minFontSize:actualF...

iphone ios nsstring uikit ios7
Rectangle 27 0

After googling for a long time I did not find a solution working under iOS7. Right now I use the following workaround, knowing that it is very ugly. I render a UILabel in memory, take a screenshot and draw that. UILabel is able to shrink the text correctly.

UILabel *myLabel = [[UILabel alloc] initWithFrame:myLabelFrame];
    myLabel.font = [UIFont fontWithName:@"HelveticaNeue-BoldItalic" size:16];
    myLabel.text = @"Some text that is too long";
    myLabel.minimumScaleFactor = 0.5;
    myLabel.adjustsFontSizeToFitWidth = YES;
    myLabel.backgroundColor = [UIColor clearColor];

    UIGraphicsBeginImageContextWithOptions(myLabelFrame.size, NO, 0.0f);
    [[myLabel layer] renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    [screenshot drawInRect:myLabel.frame];

this workaround actually works, even if it is a bit dirty. Thanks!

ios - iOS7's deprecation of NSString's drawAtPoint:forWidth:withFont:m...

ios objective-c nsstring ios7 deprecated
Rectangle 27 0

How about create new label and using sizeThatFit:(CGSize)size ??

UILabel *gettingSizeLabel = [[UILabel alloc] init];
gettingSizeLabel.font = [UIFont fontWithName:@"YOUR FONT's NAME" size:16];
gettingSizeLabel.text = @"YOUR LABEL's TEXT";
gettingSizeLabel.numberOfLines = 0;
gettingSizeLabel.lineBreakMode = NSLineBreakByWordWrapping;
CGSize maximumLabelSize = CGSizeMake(310, 9999);

CGSize expectSize = [gettingSizeLabel sizeThatFits:maximumLabelSize];

After hours and hours of searching, experimenting and dead-ends, this is the only solution that works for iOS7 and Xamarin / Monotouch

@Ossir, do you have a better idea?

objective c - sizeWithFont method is deprecated. boundingRectWithSize ...

objective-c nsstring ios7 uilabel