ios - UILabel cuts off custom font. How do I dynamically adjust UILabel height based on custom font selected? -
some of custom fonts i've loaded onto app getting cut off when displayed in uilabel. have multiple custom fonts need display. how can fix this?
as stated, had annoying problem custom fonts in uilabel cut off due something. later found out due ascenders , descenders (font characteristics).
after searching found solution required download program, adjust font's ascender , descender using terminal , test out on app until it's perfect.
this fine if didn't have 20+ fonts. decided dig around , see if access font's ascender , descender values. turns out uifont has exact attributes!
with information, able subclass uilabel , adjust frame dynamically adding ascender , descender values (use absolute value negative) height.
here's snippet of implementation code below, last line money line:
uifont *font = [uifont fontwithname:nameoffontused size:44.0]; nsdictionary *attrsdict = [nsdictionary dictionarywithobject:font forkey:nsfontattributename]; nsmutableattributedstring *thestring = [[nsmutableattributedstring alloc] initwithstring:[nsstring stringwithformat:@"%@", enteredstring] attributes:attrsdict]; //add other attributes desire nsmutableparagraphstyle *paragraphstyle = [[nsmutableparagraphstyle alloc] init]; paragraphstyle.linebreakmode = nslinebreakbycharwrapping; paragraphstyle.lineheightmultiple = 5.0; [thestring addattribute:nsparagraphstyleattributename value:paragraphstyle range:nsmakerange(0, [thestring length])]; [self setattributedtext:thestring]; [self sizetofit]; [self setframe:cgrectmake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, self.frame.size.height+font.ascender+abs(font.descender))];
Comments
Post a Comment