Tuesday, 15 March 2011

format - Make the label in an iOS UISegmentedcontrol span 2 lines -



format - Make the label in an iOS UISegmentedcontrol span 2 lines -

in app, using uisegmentedcontrol. because label in each of 3 segments spans 2 lines, need customize uisegmentedcontrol. want 2 things: (1) increment height of uisegmentedcontrol, , (2) create text label span 2 lines

i found solution #1 here: ios: alter height of uisegmentedcontrol , here change height of uisegmentedcontrol , here http://iphonedevsdk.com/forum/iphone-sdk-development/74275-change-height-of-segmented-control.html

i couldn't find out how create label within uisegmentedcontrol span 2 lines. help appreciated!

cheers, frank

try this... create category uisegmentedcontrol ,suppose uisegmentedcontrol+multiline

in uisegmentedcontrol+multiline.h

#import <uikit/uikit.h> @interface uisegmentedcontrol (multiline) - (void)insertsegmentwithmultilinetitle:(nsstring *)title atindex:(nsuinteger)segment animated:(bool)animated; @end

and in uisegmentedcontrol+multiline.m

#import "uisegmentedcontrol+multiline.h" @interface uiview (layershot) - (uiimage *)imagefromlayer; @end @implementation uiview (layershot) - (uiimage *)imagefromlayer { uigraphicsbeginimagecontextwithoptions(self.bounds.size, no, 0); [self.layer renderincontext:uigraphicsgetcurrentcontext()]; uiimage *image = uigraphicsgetimagefromcurrentimagecontext(); uigraphicsendimagecontext(); homecoming image; } @end @implementation uisegmentedcontrol (multiline) - (void)insertsegmentwithmultilinetitle:(nsstring *)title atindex:(nsuinteger)segment animated:(bool)animated { uilabel *label = [[uilabel alloc] initwithframe:cgrectzero]; [label settextcolor:[self tintcolor]]; [label setbackgroundcolor:[uicolor clearcolor]]; [label setfont:[uifont systemfontofsize:13]]; [label settextalignment:nstextalignmentcenter]; [label setlinebreakmode:nslinebreakbywordwrapping]; [label setnumberoflines:0]; [label settext:title]; [label sizetofit]; [self insertsegmentwithimage:[label imagefromlayer] atindex:segment animated:animated]; } @end

finally import uisegmentedcontrol+multiline.h in class , utilize follows

uisegmentedcontrol * segmentcontrol = [[uisegmentedcontrol alloc]initwithframe:cgrectmake(3, 66, 314, 30)]; [segmentcontrol insertsegmentwithmultilinetitle:@"first\nline" atindex:0 animated:yes]; [segmentcontrol insertsegmentwithmultilinetitle:@"second\line" atindex:1 animated:yes]; [segmentcontrol insertsegmentwithmultilinetitle:@"third\line" atindex:2 animated:yes]; [segmentcontrol setselectedsegmentindex:0]; [segmentcontrol addtarget:self action:@selector(segmentcontrolclicked:) forcontrolevents:uicontroleventvaluechanged]; [self.view addsubview:segmentcontrol];

ios format label uisegmentedcontrol

No comments:

Post a Comment