nsstring

Using a NSString to set a color for a label

时光毁灭记忆、已成空白 提交于 2019-12-04 14:59:13
I want to create a method and use a string value "redColor" to set the UIColor for a label. tableColorName is the NSString "redColor" and I tried to apply a selector to create the UIColor and apply it to my textLabel. Thanks -(void) changeLabelColor { SEL labelColor = NSSelectorFromString([NSString stringWithFormat:[@"%@", tableColorName]]); UIColor *color = [[UIColor class] performSelector:labelColor]; self.textLabel.textColor = color; } Use this method -(UIColor *)giveColorfromStringColor:(NSString *)colorname { SEL labelColor = NSSelectorFromString(colorname); UIColor *color = [UIColor

NSPredicate versus NSString: Which is better/faster for finding superstrings?

一笑奈何 提交于 2019-12-04 14:31:44
问题 I have a large number of strings that I'm searching to see if a given substring exists. It seems there are two reasonable ways to do this. Option 1: Use the NSString method rangeOfSubstring and test whether .location exists: NSRange range = [string rangeOfSubstring:substring]; return (range.location != NSNotFound); Option 2. Use the NSPredicate syntax CONTAINS : NSPredicate *regex = [NSPredicate predicateWithFormat:@"SELF CONTAINS %@", substring]; return ([regex evaluateWithObject:string] ==

NSUTF8StringEncoding causes file to render improperly in TextEdit/Pages, but not BBEdit/vi

最后都变了- 提交于 2019-12-04 13:31:19
问题 I'm running into a bit of a weird issue. Whenever I create a new text file in my iOS application, I set its encoding to be NSUTF8StringEncoding . If I edit the file and input any characters with diacritics and save the changes, the diacritics render properly in some applications such as BBEdit, TextMate, cat and vi but not in others such as TextEdit, Quick Look and Pages. I'm using the following code to save the contents of a UITextView to the plain txt files. NSError *error; NSString

Cocoa - Calling a variadic method from another variadic one (NSString stringWithFormat call)

南笙酒味 提交于 2019-12-04 12:32:16
I have a problem with [NSString strigWithFormat:format] because it returns an id, and I have a lot of code where I changed a NSString var to an other personal type. But the compiler does not prevent me that there are places where a NSString is going to be set into another type of object. So I'm writing a category of NSString and I'm goind to replace all my calls to stringWithFormat to myStringWithFormat . The code is : @interface NSString (NSStringPerso) + (NSString*) myStringWithFormat:(NSString *)format; @end @implementation NSString (NSStringPerso) + (NSString*) myStringWithFormat:(NSString

How to count the number of uppercase characters in a NSString?

北城余情 提交于 2019-12-04 11:28:26
I'm trying to find out the best way to count the number of uppercase characters that are in a NSString. I know how to find out if a certain character is uppercase by using this code: NSString *s = @"This is a string"; BOOL isUppercase = [[NSCharacterSet uppercaseLetterCharacterSet] characterIsMember:[s characterAtIndex:0]]; What would be the best way to count the number of uppercase letters in a NSString? Thanks. NSString *s = @"This is a string"; int count=0; for (i = 0; i < [s length]; i++) { BOOL isUppercase = [[NSCharacterSet uppercaseLetterCharacterSet] characterIsMember:[s

iOS drawInRect:attributes perform text truncation with multiple lines

China☆狼群 提交于 2019-12-04 09:47:02
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 which case I can see no alternative other than continuing to use deprecated methods. The deprecated

Call a method on every word in NSString

倾然丶 夕夏残阳落幕 提交于 2019-12-04 09:40:47
I would like to loop through an NSString and call a custom function on every word that has certain criterion (For example, "has 2 'L's"). I was wondering what the best way of approaching that was. Should I use Find/Replace patterns? Blocks? -(NSString *)convert:(NSString *)wordToConvert{ /// This I have already written Return finalWord; } -(NSString *) method:(NSString *) sentenceContainingWords{ // match every word that meets the criteria (for example the 2Ls) and replace it with what convert: does. } The two ways I know of looping an array that will work for you are as follows: NSArray

Compare two time values in ios? [duplicate]

安稳与你 提交于 2019-12-04 09:29:41
问题 This question already has answers here : Comparing time and date in objective C (2 answers) Closed 5 years ago . In my app i want to check whether the current time is before or after the time saved in a variable. like my time1 is time1=@"08:15:12"; and my time2 is time2=@"18:12:8"; so i wanna compare between time1 and time2.Currently these variables are in NSString Format. Following are the code used to get time,and i dont know how to compare them. CODE: NSDateFormatter *dateFormatter = [

Check string containing URL for “http://”

不打扰是莪最后的温柔 提交于 2019-12-04 08:36:42
I am trying to check the URL entered by the user, but I am fighting against some errors and warnings. -(BOOL) textFieldShouldReturn:(UITextField *)textField { //check "http://" NSString *check = textField.text; NSString *searchString = @"http://"; NSRange resultRange = [check rangeWithString:searchString]; BOOL result = resultRange.location != NSNotFound; if (result) { NSURL *urlAddress = [NSURL URLWithString: textField.text]; } else { NSString *good = [NSString stringWithFormat:@"http://%@", [textField text]]; NSURL *urlAddress = [NSURL URLWithString: good]; } // open url NSURLRequest

NSString sizeWithAttributes: content rect

不羁的心 提交于 2019-12-04 08:11:12
问题 How do I get a NSString's size as if it drew in an NSRect. The problem is when I try -[NSString sizeWithAttributes:], it returns an NSSize as if it had infinite width. I want to give a maximum width to the method. Is there any way of doing so? (BTW: Mac OS, not iPhone OS) Thanks, Alex 回答1: float heightForStringDrawing(NSString *myString, NSFont *myFont, float myWidth) { NSTextStorage *textStorage = [[[NSTextStorage alloc] initWithString:myString] autorelease]; NSTextContainer *textContainer =