ios - How to make the UIButton title label font same in size regardless of the text width -
i generating few buttons should tabbar. have done.
scroltab=[[uiscrollview alloc] initwithframe:cgrectmake(0, h-48, w, 48)]; [scroltab setcontentinset:uiedgeinsetsmake(0.0, 0.0, 0.0, 180.0)]; [scroltab setshowshorizontalscrollindicator:no]; [self.view addsubview:scroltab]; (int i=1; i<6; i++) { [scroltab addsubview:[self gettabbaritems:i :5]]; }
then gettabbaritems method this.
-(uibutton *)gettabbaritems :(nsinteger)index:(nsinteger)count { uibutton *item=[[uibutton alloc] initwithframe:cgrectmake(tabbuttonx, 0, w/count,49)]; [item.titlelabel setfont:[uifont fontwithname:@"helveticaneue-light" size:10.0]]; item.titlelabel.numberoflines = 1; item.titlelabel.adjustsfontsizetofitwidth = yes; item.titlelabel.linebreakmode = nslinebreakbyclipping; cgsize imagesize=item.imageview.image.size; cgfloat space=6.0; if (index==1) { [item settitle:@"menu" forstate:uicontrolstatenormal]; [item setimage:[uiimage imagenamed:@"menubtn"] forstate:uicontrolstatenormal]; } else if (index==2) { [item settitle:@"my profile" forstate:uicontrolstatenormal]; [item setimage:[uiimage imagenamed:@"proftab"] forstate:uicontrolstatenormal]; } else if (index==3) { [item settitle:@"notification" forstate:uicontrolstatenormal]; [item setimage:[uiimage imagenamed:@"notification"] forstate:uicontrolstatenormal]; } else if (index==4) { [item settitle:@"things-todo" forstate:uicontrolstatenormal]; [item setimage:[uiimage imagenamed:@"thingstodo"] forstate:uicontrolstatenormal]; } else if (index==5) { [item settitle:@"search" forstate:uicontrolstatenormal]; [item setimage:[uiimage imagenamed:@"search"] forstate:uicontrolstatenormal]; } [item settitleedgeinsets:uiedgeinsetsmake(-(imagesize.height+(-20)), -10.0, -(imagesize.height+space), 10.0)]; cgsize titlesize=[item.titlelabel.text sizewithattributes:@{nsfontattributename: item.titlelabel.font}]; cgrect txtlabelframe=item.titlelabel.frame; item.imageedgeinsets = uiedgeinsetsmake(0.0, -(txtlabelframe.size.width+txtlabelframe.origin.x)/2, 10.0, - titlesize.width); tabbuttonx=tabbuttonx+item.frame.size.width; return item; }
i want make font in same size, if make fonts in same size truncate middle , middle of text shows dots. how can make fonts looks in same size , show full text in button.
please me. output code.
thanks
don't add button title, have add label subview button
uilabel *label2 = [[uilabel alloc]initwithframe:cgrectmake(-10, button.frame.size.height-20, 100, 30)];//adjust frame required [label2 settextcolor:[uicolor redcolor]]; label2.text = @"lengthy string here"; [label2 setfont:[uifont fontwithname:button.titlelabel.font.fontname size:button.titlelabel.font.pointsize]]; [button addsubview:label2];
if label touches near button, use code
uilabel *label2 = [[uilabel alloc]initwithframe:cgrectmake(-10, button.frame.size.height-20, 150, 60)]; //adjust frame string length [label2 settextcolor:[uicolor redcolor]]; nsmutableattributedstring *muatrstr = [[nsmutableattributedstring alloc]initwithstring:@"lengthy string\nshould here"]; label2.numberoflines = 0; [label2 setattributedtext:muatrstr]; [label2 setfont:[uifont fontwithname:button.titlelabel.font.fontname size:button.titlelabel.font.pointsize]]; [button addsubview:label2];
Comments
Post a Comment