nsstring

How does the methods “shouldChangeTextInRange” and “stringByReplacingCharactersInRange” work?

大憨熊 提交于 2020-01-01 14:27:17
问题 I would like to know the working of the following code. - (BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string]; return !([newString length] > 10); } What does "stringByReplacingCharactersInRange" do? And how does the above method limit the number of characters that can be entered in the textField? Thanks in advance! 回答1:

iOS drawInRect:attributes perform text truncation with multiple lines

帅比萌擦擦* 提交于 2020-01-01 12:36:09
问题 How can I draw a long text string in a rect with tail truncation? I tried to use drawInRect:withAttributes: with NSLineBreakByTruncatingTail paragraph style, but it always renders text on a single line. It only renders multi-line text with NSLineBreakByWordWrapping and NSLineBreakByCharWrapping break modes and when using this option there is no truncation. Is there any property that I need to set on the paragraph style in order to make this happen? Or is this simply no longer supported, in

NSMutableUrlRequest eats plus signs

浪尽此生 提交于 2020-01-01 11:56:27
问题 I create NSMutableUrlRequest for sending data to server, add all necessary fields to it and then add the string for sending like this: [theRequest setHTTPBody:[postString dataUsingEncoding: NSUTF8StringEncoding]]; postString is a usual NSString. The problem is, when I receive this request at the server, all the plus (+) signs disappear from the http body. So if I had "abcde+fghj" on iPhone, I get "abcde fghj" on the server". Can this be some encoding problem from using dataUsingEncoding:

Why does NSString sizeWithFont: return the wrong size?

喜你入骨 提交于 2020-01-01 06:52:05
问题 I need to work out the height of a UITextView from the top down to the cursor. I am trimming the text it contains (so it only goes up to the cursor) and then using NSString's sizeWithFont:constrainedToSize:lineBreakMode: method to work out the height, like so: NSRange range; range.location = noteTextView.selectedRange.location; range.length = noteTextView.text.length - range.location; NSString *string = [noteTextView.text stringByReplacingCharactersInRange:range withString:@""]; CGSize size =

How can I make unicode characters from integers?

独自空忆成欢 提交于 2020-01-01 05:30:28
问题 I want to make an array of Unicode characters, but I don't know how to convert integers into a Unicode representation. Here's the code I have so far NSMutableArray *uniArray = [[NSMutableArray alloc] initWithCapacity:0]; int i; for (i = 32; i < 300; i++) { NSString *uniString = [NSString stringWithFormat:@"\u%04X", i]; [uniArray addObject:uniString]; } Which gives me an error "incomplete universal character name \u" Is there a better way to build an array of Unicode symbols? Thanks. 回答1: You

How to convert an NSString to an unsigned int in Cocoa?

六眼飞鱼酱① 提交于 2020-01-01 05:08:50
问题 My application gets handed an NSString containing an unsigned int . NSString doesn't have an [myString unsignedIntegerValue]; method. I'd like to be able to take the value out of the string without mangling it, and then place it inside an NSNumber . I'm trying to do it like so: NSString *myUnsignedIntString = [self someMethodReturningAString]; NSInteger myInteger = [myUnsignedIntString integerValue]; NSNumber *myNSNumber = [NSNumber numberWithInteger:myInteger]; // ...put |myNumber| in an

NSData from NSKeyedArchiver to NSString

随声附和 提交于 2020-01-01 05:03:27
问题 I'm trying to convert NSData generated from NSKeyedArchiver to an NSString so that I can pass it around and eventually convert it back to NSData. I have to pass this as a string (I'm using three20 URL passing). I've gone through various encodings, UTF8, ASCII, etc. and can't get anything to work. NSKeyedArchiver says that the NSData is formated as a property list: NSPropertyListBinaryFormat_v1_0. Does anyone have any idea how I can convert this NSData to a String and back again? Size of the

Extract values between parenthesis from NSString

倾然丶 夕夏残阳落幕 提交于 2020-01-01 03:18:06
问题 I have an NSString that will be something like "xxxx (yyyyy)" where x and y can be any character. I'd like to extract just the y from inside the parenthesis. I managed to extract the x using a NSScanner but I haven't figured out the proper way to extract out the y. 回答1: Just to be complete: If you are absolutely sure of the format of your output you can use the array methods: NSString *inputString; // this is in the form "xxxx(yyyy)" NSCharacterSet *delimiters = [NSCharacterSet

How to calculate the height of the text rectangle from an NSString?

旧城冷巷雨未停 提交于 2020-01-01 03:13:07
问题 I know there is this one: sizeWithFont:minFontSize:actualFontSize:forWidth:lineBreakMode: But since the CGSize always has the same height and doesn't adjust to any shrinked text or whatsoever, the CGSize is not telling how heigh the text is. Example: Make a UILabel with 320 x 55 points and put a loooooooooooooong text in there. Let the label shrink the text down. Surprise: CGSize.height remains the same height even if the text is so tiny that you need a microscope. Ok so after banging my head

“Whole word” search in a NSString through NSPredicate

99封情书 提交于 2020-01-01 01:13:16
问题 I would like to search if in the attribute description (an NSString instance) there is a given word. I tried with this predicate: [NSPredicate predicateWithFormat:@"description CONTAINS[cd] %@", theWord]; It works, but it finds also sub-words . For example, given theWord : Car it will mach also this description : A Christmas Carol Instead, I would like that my predicate will match only a , or christmas , or carol . (I'm using Core Data and NSFetchRequest.) 回答1: Something like this? NSString