IOS Multiple tabs dynamic arrangement

Source: Internet
Author: User

As shown in the figure above:

Custom label, self-arranged, can control margin, line spacing, enumerate, achieve neat free arrangement, say not much, see code

. m Main Page #import "ViewController.h" #import "TagsFrame.h" @interface Viewcontroller () {tagsframe *_frame;
Nsarray *_tagsarray;

    @end @implementation Viewcontroller-(void) viewdidload {[Super viewdidload]; _tagsarray = @[@ "All", @ "haha haha", @ "a little bit more", @ "he la la la la", @ "hair ha several", @ "鞥 UNV", @ "Lushan International regret", @ "lllllldaffff", @ "Lalalalall", @ " La la la la la, @ "meow meow Meow", @ "囖 囖 囖 囖 Everyone development and 囖 囖", @ "Ann", @ "right", @ "wrong", @ "initial tone Miku", @ "Animenz hahaha haha", @ "Penbeat", @ "OP", @ "Ilem", @ "original",

    @ "Work with BGM", @ "hit the car", @ "David repeatedly", @ "BGM", @ "LAUNCHPAD"];
    _frame = [[Tagsframe alloc] init];
    _frame.tagsarray = _tagsarray;
[Self createsubview];
    }-(void) Createsubview {UIView *backview = [[UIView alloc] init];
    Backview.frame = CGRectMake (0, Self.view.frame.size.width, self.view.frame.size.height-64);

    [Self.view Addsubview:backview]; For (Nsinteger i=0 i< _tagsarray.count; i++) {UIButton *tagsbtn = [UIButton buttonwithtype:uibuttontypecustom
        ]; [Tagsbtn Settitle:_tagsarray[i] Forstate:uicontrolsTatenormal];
        [Tagsbtn Settitlecolor:[uicolor Blackcolor] forstate:uicontrolstatenormal];
        TagsBtn.titleLabel.font = Tagstitlefont;
        Tagsbtn.backgroundcolor = [Uicolor Whitecolor];
        TagsBtn.layer.borderWidth = 1; TagsBtn.layer.borderColor = [Uicolor Lightgraycolor].
        Cgcolor;
        TagsBtn.layer.cornerRadius = 4;
        TagsBtn.layer.masksToBounds = YES;
        Tagsbtn.frame = cgrectfromstring (_frame.tagsframes[i]);
    [Backview ADDSUBVIEW:TAGSBTN]; }} @end
//label frame package #import <Foundation/Foundation.h> #import <UIKit/UIKit.h> #define WIDTH [UIScreen mainscreen].bounds.size.width #define HEIGHT [UIScreen mainscreen].bounds.size.height #define Tagstitlefont [Uifont systemfontofsize:13] @interface tagsframe:nsobject/** Tag name array */@property (nonatomic, Strong) NS
Array *tagsarray;
/** Label Frame Array */@property (nonatomic, strong) Nsmutablearray *tagsframes;
/** All Label Height * * @property (nonatomic, assign) CGFloat tagsheight;
/** label spacing default is 10*/@property (nonatomic, assign) CGFloat tagsmargin;
/** tab Line Spacing default is 10*/@property (nonatomic, assign) CGFloat tagslinespacing;

/** label minimum inner margin default is 10*/@property (nonatomic, assign) CGFloat tagsminpadding; @end 
Calculate the position//label of multiple labels according to the text adaptive width//Line over width evenly assigned to each label//each row of labels around its #import "TagsFrame.h" @implementation Tagsframe-(ID) I
    NIT {//Initialize self = [super init];
        if (self) {_tagsframes = [Nsmutablearray array];
        _tagsminpadding = 10;
        _tagsmargin = 10;
    _tagslinespacing = 10;
return self;

    }-(void) Settagsarray: (Nsarray *) Tagsarray {_tagsarray = Tagsarray;
    CGFloat btnx = _tagsmargin;
    CGFloat BTNW = 0;  CGFloat nextwidth = 0;  The width of the next label cgfloat morewidth = 0;
    Each line more than the width//each row of the last tag index array and each row more than the width of the array nsmutablearray *lastindexs = [Nsmutablearray array];

    Nsmutablearray *morewidths = [Nsmutablearray array]; For (Nsinteger i=0 i<tagsarray.count; i++) {btnw = [self sizewithtext:tagsarray[i] Font:tagstitlefont].width
        + _tagsminpadding * 2; if (I < tagsarray.count-1) {nextwidth = [self sizewithtext:tagsarray[i+1] Font:tagstitlefont].width + _tag
      Sminpadding * 2;  } cgfloat nextbtnx = btnx + btnw + _tagsmargin; If the Next button, at the far right of the label, wraps the line if ((Nextbtnx + nextwidth) > (WIDTH-_tagsmargin)) {///calculation exceeds width mo
            Rewidth = width-nextbtnx;
            [Lastindexs Addobject:[nsnumber numberwithinteger:i]];
            [Morewidths Addobject:[nsnumber Numberwithfloat:morewidth]];
        Btnx = _tagsmargin;
        }else{btnx + = (btnw + _tagsmargin); //If the last one is not in the array, the last one is added to the array if (i = = tagsarray.count-1) {if (![ Lastindexs Containsobject:[nsnumber Numberwithinteger:i]] {[Lastindexs addobject:[nsnumber numberWithInt
                EGER:I]];
            [Morewidths Addobject:[nsnumber numberwithfloat:0]];  }} Nsinteger location = 0;    Intercept the position nsinteger length = 0;    Length of interception cgfloat Averagew = 0;
    The average width of the cgfloat TAGW = 0;

    CGFloat Tagh = 30;
  For (Nsinteger i=0 i<lastindexs.count; i++) {      Nsinteger lastindex = [lastindexs[i] integervalue];
        if (i = = 0) {length = lastindex + 1;
        }else{length = [lastindexs[i] integervalue]-[lastindexs[i-1] integervalue];
        ///intercept an array of each row from the array nsarray *newarr = [Tagsarray subarraywithrange:nsmakerange (location, length)];
        Location = lastindex + 1;
        Averagew = [Morewidths[i] floatvalue]/newarr.count;
        CGFloat tagx = _tagsmargin;

        CGFloat tagy = _tagslinespacing + (_tagslinespacing + tagh) * I; For (Nsinteger j=0 j<newarr.count; j + +) {TAGW = [self sizewithtext:newarr[j] Font:tagstitlefont].width +
            _tagsminpadding * 2 + Averagew;
            CGRect BTNF = CGRectMake (Tagx, Tagy, TAGW, Tagh);
            [_tagsframes Addobject:nsstringfromcgrect (BTNF)];
        Tagx + = (tagw+_tagsmargin);
} _tagsheight = (Tagh + _tagslinespacing) * lastindexs.count + _tagslinespacing; /** * Single-line text data gain width high * * @paramText text * @param font font * * @return wide/high/(Cgsize) Sizewithtext: (NSString *) text font: (Uifont *) font {NSDICTI
    Onary *attrs = @{nsfontattributename:font};
return [text Sizewithattributes:attrs]; } @end

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.