+[NSString stringWithString:] — what’s the point?

You might have a NSMutableString (or some home-grown NSString subclass) that you want to duplicate. NSMutableString *buffer = [NSMutableString string]; // do something with buffer NSString *immutableStringToKeepAround = [NSString stringWithString:buffer]; Of course, you can also just make a copy: NSMutableString *buffer = [NSMutableString string]; // do something with buffer NSString *immutableStringToKeepAround = [[buffer copy] autorelease]; … Read more

How to know if a UITextField in iOS has blank spaces

You can “trim” the text, that is remove all the whitespace at the start and end. If all that’s left is an empty string, then only whitespace (or nothing) was entered. NSString *rawString = [textField text]; NSCharacterSet *whitespace = [NSCharacterSet whitespaceAndNewlineCharacterSet]; NSString *trimmed = [rawString stringByTrimmingCharactersInSet:whitespace]; if ([trimmed length] == 0) { // Text was … Read more

How to know if a UITextField in iOS has blank spaces

You can “trim” the text, that is remove all the whitespace at the start and end. If all that’s left is an empty string, then only whitespace (or nothing) was entered. NSString *rawString = [textField text]; NSCharacterSet *whitespace = [NSCharacterSet whitespaceAndNewlineCharacterSet]; NSString *trimmed = [rawString stringByTrimmingCharactersInSet:whitespace]; if ([trimmed length] == 0) { // Text was … Read more

How to get the width of an NSString?

Here’s a relatively simple approach. Just create an NSAttributedString with the appropriate font and ask for its size: – (CGFloat)widthOfString:(NSString *)string withFont:(NSFont *)font { NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:font, NSFontAttributeName, nil]; return [[[NSAttributedString alloc] initWithString:string attributes:attributes] size].width; }

How do I draw an NSString at an angle?

Here’s an example that uses a transform to rotate the drawing context. Essentially it’s just like setting a color or shadow, just make sure to use -concat instead of -set. CGFloat rotateDeg = 4.0f; NSAffineTransform *rotate = [[NSAffineTransform alloc] init]; [rotate rotateByDegrees:rotateDeg]; [rotate concat]; // Lock focus if needed and draw strings, images here. [rotate … Read more

String won’t url encode in iOS

For future reference, this is what I found to work (i.e. encode everything properly) + (NSString*)encodeURL:(NSString *)string { NSString *newString = (__bridge_transfer NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (__bridge CFStringRef)string, NULL, CFSTR(“:/?#[]@!$ &'()*+,;=\”<>%{}|\\^~`”), CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding)); if (newString) { return newString; } return @””; }

Combine Two String in different Language RTL & LTR

I’m pretty sure that the problem here is that the hebrew date in strDate is carrying unicode characters that make it display right-to-left. That’s causing chaos when combined with the ‘ordinary’ left-to-right string in timeForResponse. The date formatter is picking that up from the hebrew locale. Try this: Change your date format string to [dateFormatter … Read more

What is difference between URLWithString and fileURLWithPath of NSURL?

+URLWithString: produces an NSURL that represents the string as given. So the string might be @”http://www.google.com” and the URL represents http://www.google.com. +fileURLWithPath: takes a path, not a URL, and produces an NSURL that represents the path using a file:// URL. So if you give it /foo/bar/baz the URL would represent file:///foo/bar/baz. You can of course … Read more