ios - how to do truncation of text in UILabel -
i have problem truncation of text in uilabel
have set linebreakmode = nslinebreakbywordwrapping
perfectly.
here code snippet :
lblselectedtext = [[uilabel alloc] init]; lblselectedtext.numberoflines = 0; lblselectedtext.linebreakmode = nslinebreakbywordwrapping; lblselectedtext.font = [uifont fontwithname:@"myriadpro-regular" size:(is_ipad_pro?13.0:9.0)]; lblselectedtext.textalignment = nstextalignmentcenter; lblselectedtext.textcolor = [uicolor lightgraycolor]; lblselectedtext.text = strkey; // here text dynamic cgfloat width = 150; cgsize strsize = [self findheightfortext:strkey havingwidth:width andfont:lblselectedtext.font]; lblselectedtext.frame = cgrectmake(12, 10, cgrectgetwidth(acontainercontroller.view.frame)-20, strsize.height+15); - (cgsize)findheightfortext:(nsstring *)text havingwidth:(cgfloat)widthvalue andfont:(uifont *)font { cgsize size = cgsizezero; if (text) { if (is_english) { cgrect frame = [text boundingrectwithsize:cgsizemake(widthvalue, cgfloat_max) options:nsstringdrawinguseslinefragmentorigin attributes:@{ nsfontattributename:font } context:nil]; size = cgsizemake(frame.size.width, frame.size.height + 10); } else { cgrect frame = [text boundingrectwithsize:cgsizemake(widthvalue, cgfloat_max) options:nsstringdrawinguseslinefragmentorigin attributes:@{ nsfontattributename:font } context:nil]; size = cgsizemake(frame.size.width, frame.size.height + 1); } } return size; }
please note these labels in collection view cell. here reference images.
if have solution of problem please share me...
thanks.
as have mentioned in comment characters goes in next line means there not enough width fill every letter in single line thats why going second line set number of line 0(i.e. multiple lines). there nothing wrong in it. normal behavior according setup made in code.
now if want not goes second line set numberoflines
property 1
.
another option can set minimunscalefactor
0 1
resize font fit in available width.
you can like,
label.adjustsfontsizetofitwidth = yes; label.minimumscalefactor = 0.5f;
so, reduce font size half of actual fit in availabel width.
and once try changing linebreakmode
nslinebreakbytruncatingtail
instead of nslinebreakbywordwrapping
.
Comments
Post a Comment