Wednesday, February 22, 2017

To find if current device is iPhone 4 or 4S


iPhone 4 and 4S have shorter screens, so we need to adjust the UI height in many cases. This macro makes it easier : 

#define IS_IPHONE5 (([[UIScreen mainScreen] bounds].size.height - 568)? NO:YES)

UIButton Title gets cut at the top when using custom font


Like shown in the image, if we change the font in a UIButton, the title text gets cut. The fix is simple, just use: 

backBtn?.contentVerticalAlignment = .fill



Wednesday, February 15, 2017

Shadow on UIView using UIBezierPath


//Shadow on the right side
UIBezierPath *shadowPath = [UIBezierPath bezierPathWithRect:CGRectMake(view.width - 1, -4, 2, view.height)];
view.layer.masksToBounds = NO;
view.layer.shadowColor   = [UIColor blackColor].CGColor;
view.layer.shadowOffset  = CGSizeMake(0.0f, 5.0f);
view.layer.shadowOpacity = 0.4f;
view.layer.shadowPath    = shadowPath.CGPath;

view.layer.shadowRadius  = 2.0f;

//Shadow on the bottom side
let shadowPath:UIBezierPath   = UIBezierPath.init(rect:CGRect(x: -4y: self.height - 1width : self.width,height: 1))
self.layer.masksToBounds      = false
self.layer.shadowColor        = UIColor.black.cgColor
self.layer.shadowOffset       = CGSize(width : 0.0, height :1.0)
self.layer.shadowOpacity      = 0.4
self.layer.shadowPath         = shadowPath.cgPath
self.layer.shadowRadius       = 2.0

[There are lot of customisation options for shadows using UIBezierPath... 

Check links below: 

https://nachbaur.com/2010/11/16/fun-shadow-effects-using-custom-calayer-shadowpaths/

https://markpospesel.wordpress.com/2012/04/03/on-the-importance-of-setting-shadowpath/



Swift 3.0 version : 


        //Shadow on the bottom side
        let shadowPath:UIBezierPath   = UIBezierPath.init(rect:CGRect(x: -4,
                                                                      y: self.height - 1,
                                                                 width : self.width,
                                                                 height: 2))
        self.layer.masksToBounds      = false
        self.layer.shadowColor        = UIColor.black.cgColor
        self.layer.shadowOffset       = CGSize(width : 0.0, height :5.0)
        self.layer.shadowOpacity      = 0.4
        self.layer.shadowPath         = shadowPath.cgPath
        

        self.layer.shadowRadius       = 2.0

Show only the top and right borders of a view

CALayer *bordersLayer               = [CALayer layer];
bordersLayer.borderColor            = COLOR_GRAY_212.CGColor;
bordersLayer.borderWidth            = 1;
bordersLayer.frame                  = CGRectMake(-1, 0, CGRectGetWidth(view.frame)+1, CGRectGetHeight(view.frame)+1);

[view.layer addSublayer:bordersLayer];