Wednesday, 11 February 2015

How to set multiple color and multiple font for single uilable text?

Here I used two variables as a two different string and combined that string in to one string variable and then assign to UILable, after that I define NSFont variables as well as NSMutableAttributedString and set different property for that objects and after assign that modified attribute string to the UILable text.

    NSString *strCombineMessage = @"";
    strCombineMessage = [NSString stringWithFormat:@"%@ : %@",strUsername,strMessage];

    UIFont *font = [UIFont fontWithName:@"Helvetica-Bold" size:14.0];
    UIFont *secondFont = [UIFont fontWithName:@"Helvetica-Oblique" size:14.0];
    self.lblMessage.text = strCombineMessage;
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:self.lblMessage.text];
    [str addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:23.0/255.0 green:163.0/255.0 blue:181.0/255.0 alpha:1] range:[self
                                                                                                                                           .lblMessage.text rangeOfString:strUsername]];
    [str addAttribute:NSFontAttributeName value:font range:[self
                                                            .lblMessage.text rangeOfString:strUsername]];
    [str addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:[self
                                                                                       .lblMessage.text rangeOfString:strMessage]];
    [str addAttribute:NSFontAttributeName value:secondFont range:[self
                                                                  .lblMessage.text rangeOfString:strMessage]];

    self.lblMessage.attributedText = str;

Here you have different type of property availble for NSMutableAttributedString for ex. NSForegroundColorAttributeName, NSFontAttributeName,etc....

Tuesday, 18 November 2014

How to set separator inset to 0 ( frame of separator) of UITableView ?

Just put this code in your .m class and it will work.

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [tableView setSeparatorInset:UIEdgeInsetsZero];
    if ([tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [tableView setLayoutMargins:UIEdgeInsetsZero];
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];

Tuesday, 26 August 2014

Display LoadingView for any Device from AppDelegate on any UIViewController

Here this bellow code is used to display Custom LoadingView on any View whenever you want.
Whenever you call any url or web-service at that time activity indicator or loading view required for display that system in process so user wait for the response.

Here using my bellow code and methods you can easily Apply this code in your projects and easily you can use it in any class.

Here another method is used to get instance of AppDelegate and you can call It's method from any class.

First in AppDelegate.h file just create this bellow objects and methods.

@interface AppDelegate : UIResponder <UIApplicationDelegate>{
    UIView *activityView;
    UIView *loadingView;
    UILabel *lblLoad;

-(void) showLoadingView;
-(void) hideLoadingView;
+(AppDelegate *)sharedInstance;

and in AppDelegate.m class just paste these bellow code...

#pragma  Loading View

-(void) showLoadingView {
    if (loadingView == nil) {
        loadingView = [[UIView alloc] initWithFrame:self.window.frame];
        UIImageView *imgBack = [[UIImageView alloc]initWithFrame:loadingView.frame];
        [loadingView addSubview:imgBack];
        imgBack.opaque = NO;
        imgBack.backgroundColor = [UIColor darkGrayColor];
        imgBack.alpha = 0.5;
        UIView *subloadview=[[UIView alloc] initWithFrame:CGRectMake(84.0, 190.0,150.0 ,50.0)];
        subloadview.backgroundColor=[UIColor blackColor];
        subloadview.layer.masksToBounds = YES;
        subloadview.layer.cornerRadius = 6.0;
        lblLoad=[[UILabel alloc]initWithFrame:CGRectMake(50.0, 7.0,80.0, 33.0)];
        lblLoad.backgroundColor=[UIColor clearColor];
        lblLoad.textColor=[UIColor whiteColor];
        [subloadview addSubview:lblLoad];
        UIActivityIndicatorView *spinningWheel = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(10.0, 11.0, 25.0, 25.0)];
        [spinningWheel startAnimating];
        spinningWheel.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhite;
        [subloadview addSubview:spinningWheel]; =;

        [loadingView addSubview:subloadview];
    [self.window addSubview:loadingView];

-(void) hideLoadingView {
    if (loadingView) {
        [loadingView removeFromSuperview];
        loadingView = nil;

+(AppDelegate *)sharedInstance
    return (AppDelegate *)[[UIApplication sharedApplication] delegate];

whenever you want to display loadingview then just use bellow code...

        [[AppDelegate sharedInstance]showLoadingView];

and for this you just need to import AppDelegate.h file in you class where you use above code of showLoadingView like bellow..

#import "AppDelegate.h"

Wednesday, 30 July 2014

How to set view for particular orientation in any view forcefully?

Here using this bellow code you can set any orientation forcefully to any view...

[[UIDevice currentDevice] performSelector:NSSelectorFromString(@"setOrientation:")

                                                                      withObject:(__bridge id)((void*)UIInterfaceOrientationPortrait)];

after put this code you get warning that "PerformSelector may cause a leak because its selector is unknown"

for remove this code you can use this above code with bellow solution...

First define the bellow code in your .m file

#define SuppressPerformSelectorLeakWarning(Stuff) \
do { \
_Pragma("clang diagnostic push") \
_Pragma("clang diagnostic ignored \"-Warc-performSelector-leaks\"") \
Stuff; \
_Pragma("clang diagnostic pop") \
} while (0)

after use that above code with bellow method...

         [[UIDevice currentDevice] performSelector:NSSelectorFromString(@"setOrientation:")
                                                                      withObject:(__bridge id)((void*)UIInterfaceOrientationPortrait)];

you can use this code in viewDidLoad method or anywhere in which you want to display whole view in particular orientation forcefully

Friday, 25 July 2014

Set All Orientation for only one UIViewController in your whole application.

In AppDelegate class just use this bellow method which is available in iOS 6 and later....

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
    // Get topmost/visible view controller
    UIViewController *currentViewController = [self topViewController];// this topViewController is my custom method which i post bellow...

    if ([currentViewController isKindOfClass:[YourClassName class]]) {
            // Unlock landscape view orientations for this view controller
            return UIInterfaceOrientationMaskAllButUpsideDown;
    // Only allow portrait (standard behaviour)
    return UIInterfaceOrientationMaskPortrait;

This is custom methods which is used to find out top viewController

- (UIViewController*)topViewController {
    return [self topViewControllerWithRootViewController:[UIApplication sharedApplication].keyWindow.rootViewController];

- (UIViewController*)topViewControllerWithRootViewController:(UIViewController*)rootViewController {
    if ([rootViewController isKindOfClass:[UITabBarController class]]) {
        UITabBarController* tabBarController = (UITabBarController*)rootViewController;
        return [self topViewControllerWithRootViewController:tabBarController.selectedViewController];
    } else if ([rootViewController isKindOfClass:[UINavigationController class]]) {
        UINavigationController* navigationController = (UINavigationController*)rootViewController;
        return [self topViewControllerWithRootViewController:navigationController.visibleViewController];
    } else if (rootViewController.presentedViewController) {
        UIViewController* presentedViewController = rootViewController.presentedViewController;
        return [self topViewControllerWithRootViewController:presentedViewController];
    } else {
        return rootViewController;


Also For do some changes in your that class in which you want to available different orientation then in that class's use bellow method...

-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
    if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
        NSLog(@"Landscape Orientation Called");
        NSLog(@"Potrait Orientation Called");

If Any suggestion then please post comment...

Wednesday, 9 July 2014

Set Landscape Oriantation forcefull with bellow code.

    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];
    float   angle = M_PI/2//rotate 180°, or 1 π radians

    self.view.layer.transform = CATransform3DMakeRotation(angle, 0, 0.0, 1.0);

Thursday, 24 April 2014

Rotate UIView or UIImageView with this bellow code.

 For use this code import QuartzCore framework like bellow

#import <QuartzCore/QuartzCore.h>   

and then use bellow code

   CABasicAnimation *theAnimation;
    theAnimation=[CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    theAnimation.duration=1.0; // Animation duration
    theAnimation.repeatCount=1; // no of times you want to do animation
    theAnimation.autoreverses=YES; // reverses the animation
    theAnimation.fromValue=[NSNumber numberWithFloat:0.0]; // initial stage of animation
    theAnimation.toValue=[NSNumber numberWithFloat:3.0]; // rotation angle

    [yourImageView.layer addAnimation:theAnimation forKey:@"animateRotation"]; // add animation to thelayer of a view for which you want animation.