Any way to bold part of a NSString?

What you could do is use an NSAttributedString. NSString *boldFontName = [[UIFont boldSystemFontOfSize:12] fontName]; NSString *yourString = …; NSRange boldedRange = NSMakeRange(22, 4); NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:yourString]; [attrString beginEditing]; [attrString addAttribute:kCTFontAttributeName value:boldFontName range:boldedRange]; [attrString endEditing]; //draw attrString here… Take a look at this handy dandy guide to drawing NSAttributedString objects with Core Text.

How to get substring of NSString?

Option 1: NSString *haystack = @”value:hello World:value”; NSString *haystackPrefix = @”value:”; NSString *haystackSuffix = @”:value”; NSRange needleRange = NSMakeRange(haystackPrefix.length, haystack.length – haystackPrefix.length – haystackSuffix.length); NSString *needle = [haystack substringWithRange:needleRange]; NSLog(@”needle: %@”, needle); // -> “hello World” Option 2: NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@”^value:(.+?):value$” options:0 error:nil]; NSTextCheckingResult *match = [regex firstMatchInString:haystack options:NSAnchoredSearch range:NSMakeRange(0, haystack.length)]; NSRange needleRange … Read more

Check that a input to UITextField is numeric only

You can do it in a few lines like this: BOOL valid; NSCharacterSet *alphaNums = [NSCharacterSet decimalDigitCharacterSet]; NSCharacterSet *inStringSet = [NSCharacterSet characterSetWithCharactersInString:myInputField.text]; valid = [alphaNums isSupersetOfSet:inStringSet]; if (!valid) // Not numeric — this is for validating input is numeric chars only. Look at the documentation for NSCharacterSet for the other options. You can use characterSetWithCharactersInString … Read more

Check if NSString instance is contained in an NSArray

Yes, hard-coded NSStrings (string literals) (that is any @”…” in your source code) are turned into strings that exist indefinitely while your process is running. However NSArray‘s containsObject: methods calls isEqual: on its objects, hence even a dynamically created string such as [NSString stringWithFormat:@”%d”, 2] would return YES in your sample snippet. This is because … Read more

sizeWithFont method is deprecated. boundingRectWithSize returns an unexpected value

How about create new label and using sizeThatFit:(CGSize)size ?? UILabel *gettingSizeLabel = [[UILabel alloc] init]; gettingSizeLabel.font = [UIFont fontWithName:@”YOUR FONT’s NAME” size:16]; gettingSizeLabel.text = @”YOUR LABEL’s TEXT”; gettingSizeLabel.numberOfLines = 0; gettingSizeLabel.lineBreakMode = NSLineBreakByWordWrapping; CGSize maximumLabelSize = CGSizeMake(310, CGFLOAT_MAX); CGSize expectSize = [gettingSizeLabel sizeThatFits:maximumLabelSize]; Edit: This upper code is not good for ios 7 and above, … Read more

Understanding NSString comparison

The reason why == works is because of pointer comparison. When you define a constant NSString using @””, the compiler uniquifies the reference. When the same constants are defined in other places in your code, they will all point to the same actual location in memory. When comparing NSString instances, you should use the isEqualToString: … Read more

Converting NSString to NSDictionary / JSON

I believe you are misinterpreting the JSON format for key values. You should store your string as NSString *jsonString = @”{\”ID\”:{\”Content\”:268,\”type\”:\”text\”},\”ContractTemplateID\”:{\”Content\”:65,\”type\”:\”text\”}}”; NSData *data = [jsonString dataUsingEncoding:NSUTF8StringEncoding]; id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; Now if you do following NSLog statement NSLog(@”%@”,[json objectForKey:@”ID”]); Result would be another NSDictionary. { Content = 268; type = text; }