ios - Fonts not fitting properly in UILabel - Ascender or Descender cropped -
i've done extensive searching/reading/testing , cannot find solution problem. i've tried since ios 4.3 , it's still not resolved in ios7.
the problem this: fonts @ large sizes can have ascenders or descenders cropped in uilabel. here's screenshot directly xcode 5.1 ui (no code @ all!) showing problem - font size 300 points:
as can see, simple font helvetica neue (bold or not) has it's descender cropped. (you're seeing uiviewcontroller > uiview > uilabel)
if try , change point size you'll see font scale down, , descender not cropped. here again @ 160 points:
notice fonts not cropped , others - try noteworthy, or papyrus, or savoye let - of standard ios & fonts....
i'm talking height here - know can use adjustsfontsizetofitwidth=yes see entire length, , know can use sizetofit, neither guarantees no cropping of ascender/descender.
notice calculating height using ascender/descender values not main issue font not centered vertically within label when drawn. (if were, simple calculation.)
so here question: how can show font tall possible , assured ascender/descender not cropped regardless of font used?
edit: re-read question , realized did not ask - i'm able resize label fit font - that's not problem. here's revised question:
how can draw text in uilabel large possible , assured centered vertically, no cropping of ascender or descender?
i can figure out overall height of text, , once know fit, how can draw in uilabel vertically centered?
for example: in first screenshot, text "tg" cropped, short enough fit vertically in label. in fact, larger , still fit if centered. know of no way center vertically...
the size of label can sized according length of string, font attribute used , size of font. use method lot , works great such requirements -
nsstring *textwithinlabel = @"whatever like, passed ever"; cgsize maximumlabelsize = cgsizemake(300, 1000); //place maximum sizes here //here i've used helvetica, though can pass font name or font size here try out nsdictionary *stringattributes = [nsdictionary dictionarywithobject:[[uifont fontwithname:@"helvetica" size:15] forkey: nsfontattributename]; cgsize newexpectedlabelsize = [textwithinlabel boundingrectwithsize:maximumlabelsize options:nsstringdrawingtruncateslastvisibleline|nsstringdrawinguseslinefragmentorigin attributes:stringattributes context:nil].size; cgrect frame = self.yourlabel.frame; frame.size.height = newexpectedlabelsize.height; self.yourlabel.frame = frame;
this example change height of label, though can use change width etc. stringattributes here used calculate size, not set attributes. example, if label using 14pts , calculate height 30pts, won't change height of font, increase size of label accommodate larger font size. if want method change font attributes, need add appropriate code @ bottom of method - self.yourlabel.text.font = ...
etc.
i hope answers question,
thanks, jim.
Comments
Post a Comment