Recently, you need to use a tab to make a demo.
the size of the label is based on the text above to fit the size, you need to rely on the data returned by the active line. Not joined
Nsarray *arr = @[@ "ignorance", @ "changeable", @ "Shi Naian", @ "Alas", @ "Simon blowing Snow", @ "Oh Da", @ "look at", @ "hole-up", @ "a beast Sniper", @ "Acacia flower", @ "big event", @ "not sincere disturb", @ "Ah hehe"] ; CGFloat w = 0;//The width of the previous button and the distance of the previous button from the edge of the screen cgfloat h = 200;//is used to control the button distance from the parent view of the high for (int i = 0; i < Arr.cou nt i++) {UIButton *button = [UIButton Buttonwithtype:uibuttontypesystem]; Button.tag = + i; Button.backgroundcolor = [Uicolor Greencolor]; [Button addtarget:self action: @selector (Handleclick:) forcontrolevents:uicontroleventtouchupinside]; [Button Settitlecolor:[uicolor Redcolor] forstate:uicontrolstatenormal]; Based on the size of the computed text nsdictionary *attributes = @{nsfontattributename:[uifont Systemfontofsize:12]}; CGFloat length = [Arr[i] Boundingrectwithsize:cgsizemake (+) Options:nsstringdrawinguseslinefragmentorigin Attributes:attributes Context:nil].size.width; Assign a button [button settitle:arr[i] forstate:uicontrolstatenormal]; Set the button's frame Button.framE = CGRectMake (Ten + W, h, length + 15, 30); When the button's position exceeds the edge of the screen, wrap 320 is just the width of the parent view of the button if (Ten + W + length + >) {w = 0;//When the line is replaced W is 0 H = h + button.frame.size.height + 10;//distance from parent view also varies button.frame = CGRectMake (Ten + W, h, length + 15, 30);//Reset Button's Frame} w = button.frame.size.width + button.frame.origin.x; [Self.view Addsubview:button]; } Click Event-(void) Handleclick: (UIButton *) btn{NSLog (@ "%ld", Btn.tag);}
The IOS for loop creates a Button,button width based on the text above from fit.