Pages

September 27, 2013

Remove the extension from a file

A convenient and easy to remember function for removing the file extension.


/** Returns only the name of a file without its extension.

 It's only a shortcut for `[[ NSString lastPathComponent] stringByDeletingPathExtension]`.

 @param str Name of the file (or even the full path).
 @return Returns the file name without extension.

 */
+ (NSString *)removeExtensionFromFile:(NSString *)str {
    return [[str lastPathComponent] stringByDeletingPathExtension];
}



Download the ready-for-use source file (.m) of Remove the extension from a file

September 26, 2013

Get current date and time in a well-kwnown format

How to get the current date and time printed out in standard formats?
Using NSDateFormatter is quite a joke, so this is a powerful function.


/** Get the current date and time in any of the standard formats.

 Parameter "format" must be one of these values:
 0: "dd/MM/yyyy - HH:mm"
 1: "dd_MM_yyyy_HH_mm_ss"
 2: "yyyy-MM-dd"
 3: "HH:mm:ss"
 4: "dd/MM/yyyy - HH:mm:ss"

 @param format Type of format and therefore also the result type.
 @return Returns a string representing the current date and time using selected format.

 */
+ (NSString *)currentDateAndTimeUsingFormatMode:(NSInteger)format {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    switch (format) {
        case 0:
            [formatter setDateFormat:@"dd/MM/yyyy - HH:mm"];
            break;
        case 1:
            [formatter setDateFormat:@"dd_MM_yyyy_HH_mm_ss"];
            break;
        case 2:
            [formatter setDateFormat:@"yyyy-MM-dd"];
            break;
        case 3:
            [formatter setDateFormat:@"HH:mm:ss"];
            break;
        case 4:
            [formatter setDateFormat:@"dd/MM/yyyy - HH:mm:ss"];
            break;
        default:
            [formatter setDateFormat:@"dd/MM/yyyy - HH:mm"];
            break;
    }

    return [formatter stringFromDate:[NSDate date]];
}



Download the ready-for-use source file (.m) of Get current date and time in a well-kwnown format


September 25, 2013

Get the maximum available width (or height) for items in a box

I have a UIView container and some items with fixed size.
What's the maximum allowed space betweem them?


/** Returns the maximum space between items so that they could stay within a container.

 @param numItems Number of items that must stay within a given container.
 @param itemDimenions Size of single item.
 @param containerDim Size of container.
 @return Returns the dimension of the space between all items.
 @see maxNumberOfItemsWithDimension:minSpacer:inContainerOfDimension:

 */
+ (CGFloat)maxSpaceBetweenItems:(NSInteger)numItems withDimension:(CGFloat)itemDimenions inContainerOfDimension:(CGFloat)containerDim {
    return (containerDim - numItems * itemDimenions) / (numItems + 1);
}



Download the ready-for-use source file (.m) of Get the maximum available width (or height) for items in a box