The IOS for loop creates a Button,button width based on the text above from fit.

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.