MMMStylesheet

Objective-C

@interface MMMStylesheet : NSObject

Swift

class MMMStylesheet : NSObject

A base for app-specific stylesheets: commonly used paddings, colors, fonts, etc in a single place.

  • The size class of the current device. See the MMSize* string constants below.

    Declaration

    Objective-C

    @property (nonatomic, readonly) NSString *_Nonnull currentSizeClass;

    Swift

    var currentSizeClass: String { get }
  • Allows to avoid code that picks values (fonts, sizes, etc) by explicitely matching currentSizeClass. A mapping of size classes to values is passed here instead and a match is returned, falling back either to the value under MMMSizeRest key, or, if it is not present, to the value under the key that seems the closest to the current size class.

    Declaration

    Objective-C

    - (nonnull id)valueForCurrentSizeClass:
        (nonnull NSDictionary<NSString *, id> *)sizeClassToValue;

    Swift

    func value(forCurrentSizeClass sizeClassToValue: [String : Any]) -> Any
  • A version of valueForCurrentSizeClass: unwrapping the result as a float, which is handy for numeric values.

    Declaration

    Objective-C

    - (CGFloat)floatForCurrentSizeClass:
        (nonnull NSDictionary<NSString *, NSNumber *> *)sizeClassToValue;

    Swift

    func float(forCurrentSizeClass sizeClassToValue: [String : NSNumber]) -> CGFloat
  • Deprecated

    Try using widthBasedConverter instead or a custom converter if you relied on 2 dimensions

    Deprecated. Similar to floatForCurrentSizeClass: but instead of falling back to the value under MMMSizeRest key it tries to extrapolate the requested dimension using 1 or 2 values provided for other size classes using the widthBasedConverter.

    Declaration

    Objective-C

    - (CGFloat)extrapolatedFloatForCurrentSizeClass:
        (nonnull NSDictionary<NSString *, NSNumber *> *)sizeClassToValue;

    Swift

    func extrapolatedFloat(forCurrentSizeClass sizeClassToValue: [String : NSNumber]) -> CGFloat
  • Deprecated

    The code using this might be confusing and/or hard to support. If you need to specify values for different size classes, then list them all explicitly in a call to floatForCurrentSizeClass:

    Deprecated. Similar to extrapolatedFloatForCurrentSizeClass:, but allows to override values for certain size classes in the exceptions paramater.

    Declaration

    Objective-C

    - (CGFloat)extrapolatedFloatForCurrentSizeClass:
                   (nonnull NSDictionary<NSString *, NSNumber *> *)sizeClassToValue
                                             except:
                                                 (nonnull NSDictionary *)exceptions;

    Swift

    func extrapolatedFloat(forCurrentSizeClass sizeClassToValue: [String : NSNumber], except exceptions: [AnyHashable : Any]) -> CGFloat
  • Converts dimensions given for one size class into dimensions suitable for the current size class based on the ratio of screen widths associated with the current and source size classes.

    Declaration

    Objective-C

    @property (nonatomic, readonly) id<MMMStylesheetConverter> _Nonnull widthBasedConverter;

    Swift

    var widthBasedConverter: any MMMStylesheetConverter { get }
  • A standard set of paddings. The actual stylesheet should override all these or at least the normalPadding. They are defined here so insetsFromRelativeInsets can be defined here as well. In case only normalPadding is overriden then the rest will be calculated based on it using sqrt(2) as a multiplier, so every second padding is exactly 2x larger.

    Declaration

    Objective-C

    @property (nonatomic, readonly) CGFloat extraExtraSmallPadding;

    Swift

    var extraExtraSmallPadding: CGFloat { get }
  • Undocumented

    Declaration

    Objective-C

    @property (nonatomic, readonly) CGFloat extraSmallPadding

    Swift

    var extraSmallPadding: CGFloat { get }
  • Undocumented

    Declaration

    Objective-C

    @property (nonatomic, readonly) CGFloat smallPadding

    Swift

    var smallPadding: CGFloat { get }
  • Undocumented

    Declaration

    Objective-C

    @property (nonatomic, readonly) CGFloat normalPadding

    Swift

    var normalPadding: CGFloat { get }
  • Undocumented

    Declaration

    Objective-C

    @property (nonatomic, readonly) CGFloat largePadding

    Swift

    var largePadding: CGFloat { get }
  • Undocumented

    Declaration

    Objective-C

    @property (nonatomic, readonly) CGFloat extraLargePadding

    Swift

    var extraLargePadding: CGFloat { get }
  • Actual insets from relative ones.

    Each offset in relative insets is a fixed number corresponding to the actual paddings defined above:

    • .125 - extraExtraSmallPadding
    • .25 — extraSmallPadding
    • .5 — smallPadding
    • 1 — normalPadding
    • 2 — largePadding
    • 4 — extraLargePadding

    Note that the large padding is not necessarily 2x larger than the normal one, etc (by default the extra large is), but we intentionally use them here like this to allow more compact notation for insets which is easy to remember and easy to tweak. Compare, for example:

    \code UIEdgeInsetsMake([MHStylesheet shared].normalPadding, [MHStylesheet shared].largePadding, [MHStylesheet shared].normalPadding, [MHStylesheet shared].largePadding) \endcode

    and the equivalent:

    \code [[MHStylesheet shared] insetsFromRelativeInsets:UIEdgeInsetsMake(1, 2, 1, 2)] \endcode

    Declaration

    Objective-C

    - (UIEdgeInsets)insetsFromRelativeInsets:(UIEdgeInsets)insets;

    Swift

    func insets(fromRelativeInsets insets: UIEdgeInsets) -> UIEdgeInsets
  • This is what insetsFromRelativeInsets: is using internally. Might be useful when making similar methods.

    Declaration

    Objective-C

    - (CGFloat)paddingFromRelativePadding:(CGFloat)padding;

    Swift

    func padding(fromRelativePadding padding: CGFloat) -> CGFloat
  • A metrics dictionary that can be used with Auto Layout with keys/values corresponding to all the paddings we support, e.g. “extraSmallPadding”, etc.

    Declaration

    Objective-C

    - (nonnull NSDictionary<NSString *, NSNumber *> *)dictionaryWithPaddings;

    Swift

    func dictionaryWithPaddings() -> [String : NSNumber]
  • A dictionary with 4 values under keys “Top”, “Bottom”, “Left”, “Right” corresponding to the insets obtained from the provided relative ones via insetsFromRelativeInsets:. (A shortcut composing insetsFromRelativeInsets method with MMMDictinaryFromUIEdgeInsets().)

    Declaration

    Objective-C

    - (nonnull NSDictionary<NSString *, NSNumber *> *)
        dictionaryFromRelativeInsets:(UIEdgeInsets)insets
                           keyPrefix:(nonnull NSString *)keyPrefix;

    Swift

    func dictionary(fromRelativeInsets insets: UIEdgeInsets, keyPrefix: String) -> [String : NSNumber]
  • A dictionary with 4 values obtained from the insets returned by insetsFromRelativeInsets:insets under the keys “paddingTop”, “paddingBottom”, “paddingLeft”, “paddingRight”, i.e. it’s a shortcut for dictionaryFromRelativeInsets:insets keyPrefix:@"padding".

    Declaration

    Objective-C

    - (nonnull NSDictionary<NSString *, NSNumber *> *)
        paddingDictionaryFromRelativeInsets:(UIEdgeInsets)insets;

    Swift

    func paddingDictionary(fromRelativeInsets insets: UIEdgeInsets) -> [String : NSNumber]