Skip to content

Instantly share code, notes, and snippets.

View UjwalManjunath's full-sized avatar

Ujwal Manjunath UjwalManjunath

View GitHub Profile
import Foundation
import UIKit
extension UIView {
func constraintForAttribute(attribute:NSLayoutAttribute) -> NSLayoutConstraint? {
for constraint in self.constraints {
if constraint.firstAttribute == attribute {
return constraint
}
public extension UITableView {
func registerClass(myClass:AnyClass) {
let bundle = NSBundle(forClass: myClass)
self.registerNib(UINib(nibName: String(myClass), bundle: bundle), forCellReuseIdentifier: String(myClass))
}
func dequeue<T where T:UITableViewCell>(myclass:T.Type) -> T {
return self.dequeueReusableCellWithIdentifier(String(myclass)) as! T
}
@UjwalManjunath
UjwalManjunath / UIColor+HexString.swift
Created February 5, 2016 17:18
creates a UIColor from a hex string
extension UIColor {
class func HTSDColorWithHexString (hex:String) -> UIColor {
var cString:String = hex.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()).uppercaseString
if (cString.hasPrefix("#")) {
cString = (cString as NSString).substringFromIndex(1)
}
if (cString.characters.count != 6) {
@UjwalManjunath
UjwalManjunath / PrintFonts.swift
Created February 5, 2016 17:00
Print all fonts: IOS - Swift 2.1
func printFonts() {
let fontFamilyNames = UIFont.familyNames()
for familyName in fontFamilyNames {
print("------------------------------")
print("Font Family Name = [\(familyName)]")
let names = UIFont.fontNamesForFamilyName(familyName)
print("Font Names = [\(names)]")
}
}
@UjwalManjunath
UjwalManjunath / CollectionViewUtility.swift
Created November 4, 2015 16:50
CollectionViewExtension
import UIKit
extension UICollectionView {
func registerCell(cellType:UICollectionViewCell.Type) {
self.registerNib(cellType.nib(), forCellWithReuseIdentifier: cellType.id())
}
func registerSupplementaryView(classType:UICollectionReusableView.Type, kind:String) {
self.registerNib(classType.nib(), forSupplementaryViewOfKind: kind, withReuseIdentifier: classType.id())
@UjwalManjunath
UjwalManjunath / HTSDReachability.swift
Last active December 7, 2015 16:39
IOS Reachability: Swift 2.0
import UIKit
import SystemConfiguration
public let ReachabilityChangedNotification = "ReachabilityChangedNotification"
class HTSDReachabiltiy: NSObject {
typealias NetworkReachable = (HTSDReachabiltiy) -> ()
typealias NetworkUnreachable = (HTSDReachabiltiy) -> ()
enum NetworkStatus: CustomStringConvertible {
@UjwalManjunath
UjwalManjunath / singleSectionSingleCheck
Created August 1, 2013 19:15
adding checkmark to one cell at a time(single section)
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
}
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone;
}
@UjwalManjunath
UjwalManjunath / Multiplecheckmultiplesection
Created August 1, 2013 19:14
adding checkmark accessary to multiple sections(checking one cell at a time in each section)
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForItem:indexPath.row inSection:indexPath.section]].accessoryType = UITableViewCellAccessoryCheckmark;
[self deSelectOtherCellsInTableView:tableView Except:indexPath];
}
-(void)deSelectOtherCellsInTableView:(UITableView *)tableView Except:(NSIndexPath *)indexPath{
for(UITableViewCell *cell in [tableView visibleCells]){
NSIndexPath *index = [tableView indexPathForCell:cell];
if(index.section == indexPath.section && index.row != indexPath.row){
@UjwalManjunath
UjwalManjunath / UIImageView roundrected
Last active December 20, 2015 11:49
Making UIimage view roundrected
#import <QuartzCore/QuartzCore.h>
UIImageView * roundedView = [[UIImageView alloc] initWithImage: [UIImage imageNamed:@"wood.jpg"]];
// Get the Layer of any view
CALayer * l = [roundedView layer];
[l setMasksToBounds:YES];
[l setCornerRadius:10.0];
// You can even add a border
[l setBorderWidth:4.0];
@UjwalManjunath
UjwalManjunath / UITableView Custom Header
Last active December 20, 2015 10:31
adding a custom Header( UIlabel )to a UItableview
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *view = [[UIView alloc ]initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 18)];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 15, tableView.frame.size.width, 18)];
[label setFont:[UIFont boldSystemFontOfSize:12]];
NSString *string =@"Enter Email Address";
/* Section header is in 0th index... */
[label setText:string];
view.backgroundColor = [UIColor clearColor];