viernes, 30 de septiembre de 2016

iOS: Remove left gap from row separator

Objective C

#pragma mark - UITableViewDelegate method

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Remove seperator inset
    if ([cell respondsToSelector : @selector (setSeparatorInset :)]) {
        [cell setSeparatorInset : UIEdgeInsetsZero ];
    }
    
    // Prevent the cell from inheriting the Table View's margin settings
    if ([cell respondsToSelector : @selector (setPreservesSuperviewLayoutMargins :)]) {
        [cell setPreservesSuperviewLayoutMargins : NO ];
    }
    
    // Set Your explictly cell's layout margins
    if ([cell respondsToSelector : @selector (setLayoutMargins :)]) {
        [cell setLayoutMargins : UIEdgeInsetsZero ];
    }

}

Swift

// In viewDidLoad
tableView?.cellLayoutMarginsFollowReadableWidth = false

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        if cell.responds(to: #selector(setter: UITableViewCell.separatorInset)) {
            cell.separatorInset = UIEdgeInsets.zero
        }
        if cell.responds(to: #selector(setter: UIView.preservesSuperviewLayoutMargins)) {
            cell.preservesSuperviewLayoutMargins = false
        }
        if cell.responds(to: #selector(setter: UIView.layoutMargins)) {
            cell.layoutMargins = UIEdgeInsets.zero
        }
    }

jueves, 29 de septiembre de 2016

iOS: Navigation bar with custom font

Swift

self.navigationBar.titleTextAttributes = [ NSFontAttributeName: UIFont(name: "Questrial-Regular", size: 15)!, NSForegroundColorAttributeName: UIColor.whiteColor()]

iOS: Transparent navigation bar

Swift
        self.navigationBar.shadowImage = UIImage()
        self.navigationBar.isTranslucent = true
        self.navigationBar.setBackground(UIImage(), forBarMetrics: .default)

iOS: Setup navigation bar

Swift

On .plist:
View controller-based status bar appearance -> YES

private func _setupNavigationBar() -> Void {
        
        self.navigationBar.shadowImage = nil;
        self.navigationBar.translucent = false;
        self.navigationBar.barTintColor = UIColor.clearColor() // Bar background color
        self.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()] // Title text color
        self.navigationBar.setBackgroundImage(nil, forBarMetrics: .Default) // No background image with background clear color makes the bar transparent

    }

override var preferredStatusBarStyle: UIStatusBarStyle {
        return .lightContent
    }

Note: 

1) If on a presented view controller:

On presenter view controller:
override var childViewControllerForStatusBarHidden: UIViewController? {
        return self.presentedViewController
    }
    
    override var childViewControllerForStatusBarStyle: UIViewController? {
        return self.presentedViewController

    }

On presented view controller:


override var preferredStatusBarStyle: UIStatusBarStyle {
        return .lightContent
    }
    
    override var prefersStatusBarHidden: Bool {
        return false

    }

2) CARE Status bar's background color will be the views background color

iOS: Generate amazon signature

NSMutableArray* array = @[ [NSString stringWithFormat:@"AWSAccessKeyId=%@", self.credentials[@"AWSAccessKeyId"]],
                                   [NSString stringWithFormat:@"AssociateTag=%@", self.credentials[@"AssociateTag"]],
                                   @"Availability=Available",
                                   [NSString stringWithFormat:@"Keywords=%@", [string urlencode]],
                                   @"Operation=ItemSearch",
                                   @"ResponseGroup=Medium",
                                   // [NSString stringWithFormat:@"SearchIndex=%@", category],
                                   @"SearchIndex=All",
                                   @"Service=AWSECommerceService-Y",
                                   [NSString stringWithFormat:@"Timestamp=%@", [[DateHelper UTFStringFromDate:[NSDate date]] urlencode]]
                                   ].mutableCopy;

if (page != 0) {
    [array insertObject:[NSString stringWithFormat:@"ItemPage=%ld", page] atIndex:3];
}
       
[array addObject:[NSString stringWithFormat:@"Signature=%@", [self _getSignatureFromParams:array]]];

...

- (NSString*)_getSignatureFromParams:(NSArray*)params
{
    NSString* paramsString = [params componentsJoinedByString:@"&"];
   
    NSString* string = [NSString stringWithFormat:@"GET\nwebservices.amazon.com\n/onca/xml\n%@", paramsString];
   
    NSData *keyData = // AWSSecretKey"
    NSData *paramData = [string dataUsingEncoding:NSUTF8StringEncoding];
    NSMutableData* hash = [NSMutableData dataWithLength:CC_SHA256_DIGEST_LENGTH ];
    CCHmac(kCCHmacAlgSHA256, keyData.bytes, keyData.length, paramData.bytes, paramData.length, hash.mutableBytes);
   
    return [[hash base64EncodedStringWithOptions:0] urlencode];

}

miércoles, 28 de septiembre de 2016

Swift: Add icon to label

let attachment: NSTextAttachment = NSTextAttachment()
        attachment.image = UIImage(named: "information icon")
        attachment.bounds = CGRect(x: 0, y: -5, width: attachment.image!.size.width, height: attachment.image!.size.height)
        
        let attachmentString: NSAttributedString = NSAttributedString(attachment: attachment)
        let attributedText: NSMutableAttributedString = NSMutableAttributedString()
        attributedText.append(attachmentString)
        attributedText.append(NSAttributedString(string: " We will never post anything to Facebook"))
        

        informationLabel.attributedText = attributedText

Swift: Tex with various colors or formats

        let differentColorText: String = "60 seconds"
        
        let fullText: NSString = "Match when you engage\nmore than 60 seconds in\na conversation."
        
        let range: NSRange = fullText.rangeOfString(differentColorText)
        
        let string: NSMutableAttributedString = NSMutableAttributedString(string: "Match when you engage\nmore than 60 seconds in\na conversation.")
        string.addAttribute(NSForegroundColorAttributeName, value: UIColor.lightGrayColor(), range: range)
        

        titleLabel.attributedText = string

martes, 13 de septiembre de 2016

iOS: UITableView add selected background and select row

Cell:

- (void)awakeFromNib
{
    UIView *selectionColor = [[UIView alloc] init];
    selectionColor.backgroundColor = [ColorHelper sickGreen];
    self.selectedBackgroundView = selectionColor;
}

ViewController containing table view:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
   // Set selected or not selected here. Doing it anywhere else has no result because prior to showing
   // the cell iOS sets selected to NO after that this method is called
}

jueves, 1 de septiembre de 2016

iOS: Change statusbar background color

- (void)_setStatusBarBackgroundColor:(UIColor *)color
{
    UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
    
    if ([statusBar respondsToSelector:@selector(setBackgroundColor:)])
    {
        statusBar.backgroundColor = color;
        
    }

}