Skip to content

Instantly share code, notes, and snippets.

@bdalziel
Created October 22, 2016 16:46
Show Gist options
  • Save bdalziel/140466b4d75774c06732eee96661d05c to your computer and use it in GitHub Desktop.
Save bdalziel/140466b4d75774c06732eee96661d05c to your computer and use it in GitHub Desktop.
+ (CGFloat)getUITextElementBelowBaselineHeight:(ASPUITextElement)element traitCollection:(UITraitCollection *)contextTraitCollection {
// 1. Get the style guide typography style we're using for this UI element
ASPUITextStyle elementTextStyle = [self mapUIElementToStyle:element elementVariation:ASPUITextElementVariationDefault];
UIFont *font = [ASPFontUtil getUIFont:elementTextStyle traitCollection:contextTraitCollection];
// 2. Get line height multiple for typography - this is important because line height affects descender and ascender calculations
CGFloat lineHeightMultiple = [self getUILineHeightMultiple:elementTextStyle font:font traitCollection:contextTraitCollection];
// 3. If we're using a non-default line height, calculate an adjusment
CGFloat lineHeightAdjustment = 0.0;
if (lineHeightMultiple != 1) {
lineHeightAdjustment = (font.lineHeight - (font.lineHeight * lineHeightMultiple))/2;
}
// 4. We're ready to do our font metric math - we want the height below the baseline, which is lineHeight minus ascender (adjusted to match the actual line height we use)
return ((font.lineHeight - font.ascender) - lineHeightAdjustment);
}
+(CGFloat)roundToSubPixelPrecision:(CGFloat)value deviceScale:(CGFloat)deviceScale {
if (deviceScale > 0) {
return (round(value*deviceScale))/deviceScale;
}
ASP_Log(@"Worry about nil/zero device scale");
return value;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment