Monday, April 30, 2012

How to find if NSString contains decimal + certain other characters.


NSMutableCharacterSet *_mySet = [NSMutableCharacterSet characterSetWithCharactersInString:@"aAfF"];
[_mySet formUnionWithCharacterSet:[NSCharacterSet decimalDigitCharacterSet]];
NSCharacterSet *myStringSet = [NSCharacterSet characterSetWithCharactersInString:mystring];

if ([_mySet isSupersetOfSet: myStringSet])
{
//This is will be true if myString contains ONLY " decimal numbers(0-9) and characters a,A,f,F"
        
}



Note: First 2 code lines tell you how to join 2 character sets.

How to check if NSString contains only numbers.




NSCharacterSet *_NumericOnly = [NSCharacterSet decimalDigitCharacterSet];
NSCharacterSet *myStringSet = [NSCharacterSet characterSetWithCharactersInString:mystring];
    
if ([_NumericOnly isSupersetOfSet: myStringSet])
{
    //String entirely contains decimal numbers only.
   
        
}






Friday, April 27, 2012

How to capitalize first letter of NSString ?


NSString *myString = @"i want first letter capitalized.";
NSString *newString = [myString stringByReplacingCharactersInRange:NSMakeRange(0,1) withString:[[myString substringToIndex:1] capitalizedString]];
sdsad

This will give output of newString as  I want first letter capitalized

Fetch address for a contact from addressbook.


    ABMultiValueRef multi = ABRecordCopyValue(person, kABPersonAddressProperty);
    
    if(multi)
    {
        NSArray *address = (NSArray *)ABMultiValueCopyArrayOfAllValues(multi);
        for (int i = 0; i < [address count]; i++) {
        {
            NSDictionary *addressDict=[address objectAtIndex:i];
            NSString *country = [addressDict objectForKey:(NSString *)kABPersonAddressCountryKey];
            NSString *zip = [addressDict objectForKey:(NSString *)kABPersonAddressZIPKey];
            NSString *city = [addressDict objectForKey:(NSString *)kABPersonAddressCityKey];
            NSString *street = [addressDict objectForKey:(NSString *)kABPersonAddressStreetKey];
            
        }
    }


Similarly, you can fetch phone numbers as well as email address and other details by changing property kABPersonAddressProperty to corresponding property.

How to append phone number to an existing contact.


    //We need to create a mutable copy of ABMultiValueRef with property kABPersonPhoneProperty
    
    ABMultiValueRef Phones = ABRecordCopyValue(contact, kABPersonPhoneProperty);
    ABMutableMultiValueRef Phone = ABMultiValueCreateMutableCopy(Phones);
    ABMultiValueAddValueAndLabel(Phone,@"413-675-1234", kABOtherLabel, NULL);
    ABMultiValueAddValueAndLabel(Phone@"413-675-1235", kABOtherLabel, NULL); 
    ABRecordSetValue(contact, kABPersonPhonePropertyPhone,nil);
    CFRelease(Phone);

    //This will add 2 numbers with others title under phone section of contact.
    //You can use other label too like following kABWorkLabel or kABPersonPhoneMobileLabel 

    ABMultiValueAddValueAndLabel(Phone,@"413-675-1234"kABPersonPhoneMobileLabelNULL);
    ABMultiValueAddValueAndLabel(Phone@"413-675-1235"kABWorkLabelNULL); 


    //Dont forget to save your addressbook after that.

    ABAddressBookSave(addressbook, &error);
    if (error != NULL
    {
        NSLog(@"failed");

    }
      NSLog(@"success.");

Note: If you don't make a  mutable copy, you will get following error for trying add  phone number to ABMultiValueRef instead of ABMutableMultiValueRef. As you created ABMultiValueRef by copying from ABRecordRef using method ABRecordCopyValue.
Assertion failed: (((ABCMultiValue *)multiValue)->flags.isMutable), function ABMultiValueAddValueAndLabel





Remove contact from address book

Remove a record from address book.


ABAddressBookRef addressbook = ABAddressBookCreate();
ABAddressBookRemoveRecord(addressbook, record, nil);
ABAddressBookSave(addressbook, nil);
CFRelease(addressbook);


----------------------------------------------------------------------------------------


Remove  record(s) from address book with specific name.

ABAddressBookRef addressbook = ABAddressBookCreate();
CFStringRef nameRef = (CFStringRef)[NSString stringWithFormat:@"%@ %@"@"Davis"@"Scott"];
CFArrayRef  AllRecords_ = ABAddressBookCopyPeopleWithName(addressbook, nameRef);
if (AllRecords_ != NULL
{
    int count = CFArrayGetCount(AllRecords_);          
    for (int i = 0; i < count; ++i) 
    {                
           ABRecordRef contact = CFArrayGetValueAtIndex(AllRecords_, i);
           ABAddressBookRemoveRecord(addressbook, contactnil);
    }
    
}
ABAddressBookSave(addressbook, nil);
CFRelease(addressbook);


Add new contact in address book



    CFErrorRef error = NULL;
    ABAddressBookRef iPhoneAddressBook = ABAddressBookCreate();
    ABRecordRef newPerson = ABPersonCreate();
    ABRecordSetValue(newPerson, kABPersonFirstNameProperty,@"Davis", &error);
    ABRecordSetValue(newPerson, kABPersonLastNameProperty, @"Scott", &error);
    
    
    //Add my phone number
    ABMutableMultiValueRef PhoneVar = ABMultiValueCreateMutable(kABMultiStringPropertyType);
    ABMultiValueAddValueAndLabel(PhoneVar,valueForKey:@"6176352360", kABPersonPhoneMainLabel, NULL);
    ABRecordSetValue(newPerson, kABPersonPhonePropertyPhoneVar,nil);
    CFRelease(PhoneVar);
    
    
    //Add my email address
    ABMutableMultiValueRef EmailVar = ABMultiValueCreateMutable(kABMultiStringPropertyType);
    ABMultiValueAddValueAndLabel(EmailVar,@"d.scott@hotmail.com" , kABWorkLabel, NULL);
    ABRecordSetValue(newPerson, kABPersonEmailPropertyEmailVar,nil);
    CFRelease(EmailVar);
    
   
    //Add my mailing address
    ABMutableMultiValueRef Address = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);
    NSMutableDictionary *addressDict = [[NSMutableDictionary alloc] init];
    [addressDictionary setObject:streetString forKey:(NSString *) kABPersonAddressStreetKey];
    [addressDictionary setObject:@"Boston" forKey:(NSString *)kABPersonAddressCityKey];
    [addressDictionary setObject:@"02124" forKey:(NSString *)kABPersonAddressZIPKey];
    [addressDictionary setObject:@"US" forKey:(NSString *)kABPersonAddressCountryKey];
    ABMultiValueAddValueAndLabel(AddressaddressDict, kABWorkLabel, NULL);
    ABRecordSetValue(newPerson, kABPersonAddressPropertyAddress,&error);
    CFRelease(Address);
    
    //Finally saving the contact in the address book
    ABAddressBookAddRecord(iPhoneAddressBook, newPerson, &error);
    ABAddressBookSave(iPhoneAddressBook, &error);
    if (error != NULL
    {
        NSLog(@"Saving contact failed.");
    }


    NSLog(@"Contact saved.");

Wednesday, April 18, 2012

How to create a transparent background in CGContext ?



    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSaveGState(context);
    CGRect a=self.frame;
    a.origin.x=0;
    a.origin.y=0;
    CGContextSetBlendMode(context, kCGBlendModeClear);
    CGContextSetFillColorWithColor(context, [UIColor blackColor].CGColor);    
    CGContextAddRect(context, a);
    CGContextFillPath(context);
    CGContextRestoreGState(context);



Don't forget to add the background color as clear color.



- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [self setBackgroundColor:[UIColor clearColor]];
    }
    return self;
}




How to compare CGPoint




if (CGPointEqualToPoint(point1,point2) == YES)
{
       NSLog(@"Points are equal");
}



Tuesday, April 17, 2012

MultiLine text on a UIButton



aButton.titleLabel.lineBreakMode = UILineBreakModeWordWrap;
[aButton setTitle: @"View MIMChartLib \n Created by Reetu Raj" forState: UIControlStateNormal];




MIMChartLibrary Beta 1.2 released.

New Additions have been done in PieCharts , a lot more styles are available for users to choose. All y ou have to do is copy and paste the sample codes from test files in your code.
For code, please see the MIMChartLibAppDelegate.m file in the code.



Get the latest code from GitHub

https://github.com/ReetuRaj/MIMChart-Library


Some people have said that they are not able to use this library in their iPhone project. I am going to post an update on it soon. So that its solves their problem too.


1. BASIC PIE CHART

Given below are screenshots from the user sample code. If you think any specific look suits your app,  you can straightaway copy paste the code. You don't have to boggle your mind through the documentation.

You can be creative and use your own combination of settings for creating a new look of basic pie chart.



Default look (Of course, you can change the colors to your choice of colors using delegate method -(NSArray *)colorsForPie:(BasicPieChart *)pieChart; )



















 With Inbuilt Tints: If you are confused about what colors you should use, here are some color combinations selected by us and put into 3 categories : GREENTINT,REDTINT and BEIGETINT





NOTE: Remember to pass nil in following Delegate method: -(NSArray *)colorsForPie:(id)pieChart
If you anyways ended up giving colors array too, it will ignore the tint value defined in its properties.






























































2. PADDED PIE CHARTS












________________________________________________________________________________

3. BEVELED PIE CHART











________________________________________________________________________________

4. BITRANS PIE CHART


 










________________________________________________________________________________

5. SECTIONED PIE CHART