Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the size of a NSString

A "quicky": how can I get the size (width) of a NSString?

I'm trying to see if the string width of a string to see if it is bigger than a given width of screen, case in which I have to "crop" it and append it with "...", getting the usual behavior of a UILabel. string.length won't do the trick since AAAAAAAA and iiiiii have the same length but different sizes (for example).

I'm kind of stuck.

Thanks a lot.

like image 246
camilo Avatar asked Apr 19 '10 16:04

camilo


People also ask

What does NSString mean?

A static, plain-text Unicode string object that bridges to String ; use NSString when you need reference semantics or other Foundation-specific behavior. iOS 2.0+ iPadOS 2.0+ macOS 10.0+ Mac Catalyst 13.0+ tvOS 9.0+ watchOS 2.0+

How do you find the length of a string in Objective C?

int len = [myString length];

What is the difference between NSString and string?

NSString is class and String is struct , I understand but NSString is an reference type ,how it is working inside struct.

Do I need to release NSString?

If you create an object using a method that begins with init, new, copy, or mutableCopy, then you own that object and are responsible for releasing it (or autoreleasing it) when you're done with it. If you create an object using any other method, that object is autoreleased, and you don't need to release it.


2 Answers

This is a different approach. Find out the minimum size of the text so that it won't wrap to more than one line. If it wraps to over one line, you can find out using the height.

You can use this code:

CGSize maximumSize = CGSizeMake(300, 9999); NSString *myString = @"This is a long string which wraps"; UIFont *myFont = [UIFont fontWithName:@"Helvetica" size:14]; CGSize myStringSize = [myString sizeWithFont:myFont                             constrainedToSize:maximumSize                                 lineBreakMode:self.myLabel.lineBreakMode]; 

300 is the width of the screen with a little space for margins. You should substitute your own values for font and size, and for the lineBreakMode if you're not using IB.

Now myStringSize will contain a height which you can check against the height of something you know is only 1 line high (using the same font and size). If it's bigger, you'll need to cut the text. Note that you should add a ... to the string before you check it again (adding the ... might push it over the limit again).

Put this code in a loop to cut the text, then check again for the correct height.

like image 132
nevan king Avatar answered Oct 04 '22 05:10

nevan king


Use below method.

Objective-C

- (CGSize)findHeightForText:(NSString *)text havingWidth:(CGFloat)widthValue andFont:(UIFont *)font {     CGSize size = CGSizeZero;     if (text) {         CGRect frame = [text boundingRectWithSize:CGSizeMake(widthValue, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{ NSFontAttributeName:font } context:nil];         size = CGSizeMake(frame.size.width, frame.size.height + 1);     }     return size; } 

Swift 3.0

func findHeight(forText text: String, havingWidth widthValue: CGFloat, andFont font: UIFont) -> CGSize {     var size = CGSizeZero     if text {         var frame = text.boundingRect(withSize: CGSize(width: widthValue, height: CGFLOAT_MAX), options: .usesLineFragmentOrigin, attributes: [NSFontAttributeName: font], context: nil)         size = CGSize(width: frame.size.width, height: frame.size.height + 1)     }     return size } 
like image 38
Himanshu padia Avatar answered Oct 04 '22 06:10

Himanshu padia