Possible Duplicate:
Measuring the pixel width of a string in Objective C
I use the [string drawInRect:] methods, and would like the string to be truncated to the maximum length that would fit in that size. It's not as simple as finding out how many characters there are per line, as every letter is a different width. The reason I want to find where the string is no longer visible is so I can add elipses onto the end of the string.
Look at the documentation of drawInRect:withAttributes:
in the NSString Additions Reference. Under "Constants" you will find an option that does what you want automatically:
NSStringDrawingTruncatesLastVisibleLine
Truncates and adds the ellipsis character to the last visible line if the text doesn't fit into the bounds specified.
You can use if the solution given by @Mundi are insufficient or you need to know how much text fit.
- (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size
in a loop decreasing the size of the text until it fits.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With