iOS development note dynamically generates a label based on the length of the incoming string and is arranged according to the screen width

Source: Internet
Author: User

In the development work encountered a very rare problem for me, engaged for two days, all collapsed, and finally cheated the great God to the demo did come out the effect is this

First a colleague in the background passes a string. Refuse to pass an array, then we must learn to split!

NSString *tagstr = [Dictionary valueforkey:@ "tags"];

Nsarray *statusarray = [Tagstr componentsseparatedbystring:@ ","];

This is the method of dividing the string by the symbol, then we can dynamically generate the tag based on the content;
Here are the macros used:

#define Font_size 13.0f

#define Horizontal_padding 7.0f

#define Vertical_padding 3.0f

#define Corner_radius 10.0f

#define Label_margin 5.0f

#define Bottom_margin 5.0f

#define Background_color Uicolorfromrgb (0x04, 0x9f, 0xf1)

[Uicolor colorwithred:0.93 green:0.93 blue:0.93 alpha:1.00]

#define Text_color [Uicolor Whitecolor]

#define Text_shadow_color [Uicolor Whitecolor]

#define Text_shadow_offset Cgsizemake (0.0f, 1.0f)

#define Border_color [Uicolor Lightgraycolor]. Cgcolor

#define Border_width 1.0f

This is the code:

if (Tagstr.length > 0)//Determine if a value is generated

{

float Totalheight = 0;//

CGRect previousframe = Cgrectzero;

BOOL gotpreviousframe = no;//Check if there is a previous label

For (NSString *text in Statusarray)

{

Cgsize textSize = [text Sizewithfont:[uifont systemfontofsize:font_size] Constrainedtosize:cgsizemake ( Self.frame.size.width, linebreakmode:uilinebreakmodewordwrap];//) get string lengths dynamically

Textsize.width + = horizontal_padding*2;

Textsize.height + = vertical_padding*2;

UILabel *label = nil;

if (!gotpreviousframe)

{

label = [[UILabel alloc] Initwithframe:cgrectmake (0, 5, textsize.width, Textsize.height)];

Totalheight = Textsize.height;

}

Else

{

CGRect newrect = Cgrectzero;

if (previousframe.origin.x + previousFrame.size.width + textsize.width + label_margin > Self.frame.size.width) {

Newrect.origin = Cgpointmake (0, PREVIOUSFRAME.ORIGIN.Y + textsize.height + bottom_margin);

Totalheight + = Textsize.height + Bottom_margin;

}

Else

{

Newrect.origin = Cgpointmake (previousframe.origin.x + previousFrame.size.width + label_margin, PREVIOUSFRAME.ORIGIN.Y );

}

Newrect.size = textSize;

label = [[UILabel alloc] initwithframe:newrect];

}

Previousframe = Label.frame;

Gotpreviousframe = YES;

[Label Setfont:[uifont Systemfontofsize:font_size];

if (!lblbackgroundcolor)

{

[Label Setbackgroundcolor:background_color];

} else

{

[Label Setbackgroundcolor:lblbackgroundcolor];

}

[Label Settextcolor:text_color];

[Label Settext:text];

[Label Settextalignment:uitextalignmentcenter];

[Label Setshadowcolor:text_shadow_color];

[Label Setshadowoffset:text_shadow_offset];

[Label.layer Setmaskstobounds:yes];

[Label.layer Setcornerradius:corner_radius];

[Label.layer Setbordercolor:border_color];

[Label.layer Setborderwidth:border_width];

[_corptagview Addsubview:label];

}

Sizefit = Cgsizemake (self.frame.size.width, totalheight + 1.0f);//This I have never known what effect, copied O (∩_∩) o haha ~

}

iOS development note dynamically generates a label based on the length of the incoming string and is arranged according to the screen width

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.