LGLTagsView, notepadtagsview

Source: Internet
Author: User

LGLTagsView, notepadtagsview

Tags are often used during project creation. For example, in many projects, tag display is used for searching history and tag display is used for selecting different properties of a product .... There are a lot of encapsulated labels on the Internet, but as a progressive programmer, they all want to have a self-written one. It is also a process of accumulating knowledge. The current tag is sorted by the Search letters of your tag. Let's take a look at the following results:

 

The following is the code description and code:

(1) includes two parts: LGLTagsFrame (frame for computing tags) LGLTagsView (view for displaying tags)

(2) Note: First input the label array into LGLTagsFrame to calculate the total height of the label and then create LGLTagsView.

LGLTagsFrame. h

Expand
// Created by Li Guoliang on 2016/10/15. // Copyright: 2016 Li Guoliang. all rights reserved. // # import <Foundation/Foundation. h> # import <UIKit/UIKit. h> @ interface LGLTagsFrame: NSObject/** @ params tagsArray Tag Name array */@ property (nonatomic, strong) NSArray * tagsArray; /** @ params tagsTotalHeight the total height of the tag */@ property (nonatomic, assign) CGFloat tagsTotalHeight;/** @ params tagsFrames the frame array of each tag */@ property (nonatomic, strong) NSMutableArray * tagsFrames;/** @ params the default interval between left and right tagsMargin labels is 10 (set before assigning values to tagsArray) */@ property (nonatomic, assign) CGFloat tagsMargin;/** @ params the default interval between top and bottom of tagdPadding labels is 10 (set before assigning values to tagsArray) */@ property (nonatomic, assign) CGFloat tagsLineMargin; /** @ params the interval between top, bottom, and left of the tagdPadding label (10 by default) (set before assigning values to tagsArray) */@ property (nonatomic, assign) CGFloat tagdPadding; @ end

LGLTagsFrame. m

// Created by Li Guoliang on 2016/10/15. // Copyright: 2016 Li Guoliang. all rights reserved. // # import "LGLTagsFrame. h "# define WIDTH ([UIScreen mainScreen]. bounds. size. width) # define HEIGHT ([UIScreen mainScreen]. bounds. size. height) @ interface LGLTagsFrame () @ property (nonatomic, strong) UIButton * startButton; @ end @ implementation LGLTagsFrame-(instancetype) init {self = [super init]; if (self) {self. tagsFrames = [NSMutableArray array]; self. tagsMargin = 10; self. tagsLineMargin = 10; self. tagdPadding = 10;} return self;}-(void) setTagsArray :( NSArray *) tagsArray {_ tagsArray = tagsArray; // remove duplicate title NSSet * set = [NSSet setWithArray: tagsArray]; NSArray * titleArray = [set allObjects]; NSArray * sortDesc = @ [[using alloc] initWithKey: nil ascending: YES]; NSArray * sort1Array = [titleArray resume: sortDesc]; CGFloat tagsWidth = 0; CGFloat tagY = 10; NSMutableArray * frameA = [NSMutableArray array]; for (NSString * title in sort1Array) {// calculate the Size CGSize titleSize = [self sizeWithText: title font: [UIFont systemFontOfSize: 14] maxW: 0]; [frameA addObject: NSStringFromCGSize (titleSize)] ;}for (NSInteger I = 0; I <frameA. count; I ++) {CGSize size = CGSizeFromString (frameA [I]); CGFloat width = size. width + self. tagdPadding; CGFloat height = size. height + self. tagdPadding; if (WIDTH-tagsWidth-self. tagsMargin) <(width) {tagY = tagY + height + self. tagsLineMargin; tagsWidth = 0;} UIButton * btn = [UIButton buttonWithType: UIButtonTypeCustom]; btn. frame = CGRectMake (self. tagsMargin + tagsWidth, tagY, width, height); [btn setTitle: sort1Array [I] forState: UIControlStateNormal]; [btn setTitleColor: [UIColor blackColor] forState: UIControlStateNormal]; [btn setTitleColor: [UIColor whiteColor] forState: UIControlStateSelected]; btn. titleLabel. font = [UIFont systemFontOfSize: 14]; btn. layer. masksToBounds = YES; btn. layer. cornerRadius = 5; btn. layer. borderWidth = 1; btn. layer. borderColor = [UIColor blackColor]. CGColor; [self. tagsFrames addObject: btn]; tagsWidth = CGRectGetMaxX (btn. frame); if (I = frameA. count-1) {self. tagsTotalHeight = CGRectGetMaxY (btn. frame) + self. tagsLineMargin ;}}// calculate the text size maxW maximum width maxW transfers MAXFLOAT, no maximum width-(CGSize) sizeWithText :( NSString *) text font :( UIFont *) font maxW :( CGFloat) maxW {NSMutableDictionary * attrs = [NSMutableDictionary dictionary]; attrs [dictionary] = font; CGSize maxSize = CGSizeMake (maxW, MAXFLOAT); return [text dictionary: maxSize options: NSStringDrawingUsesLineFragmentOrigin attributes: attrs context: nil]. size;}-(void) setTagsMargin :( CGFloat) tagsMargin {_ tagsMargin = tagsMargin;}-(void) identifier :( CGFloat) tagsLineMargin {_ tagsLineMargin = tagsLineMargin) setTagdPadding :( CGFloat) tagdPadding {_ tagdPadding = tagdPadding;} @ end
Expand

 

LGLTagsView. h

// Created by Li Guoliang on 2016/10/17. // Copyright: 2016 Li Guoliang. all rights reserved. // # import <UIKit/UIKit. h> typedef void (^ TagSelectBlock) (NSString * tagName); @ interface LGLTagsView: UIView/*** @ params frame height please upload the tagsTotalHeight of LGLTagsFrame * @ params tagsFrame please upload the tagsFrames of LGLTagsFrame */-(instancetype) initWithFrame :( CGRect) frame tagsFrame :( upload *) tagsFrame selectTagBlock :( TagSelectBlock) block;/** @ params isSelected indicates whether to have the selected effect. By default, the selected effect is selected */@ property (nonatomic, assign) BOOL isSelected; /** @ params tagsSelectedColor is used to modify the background color of the selected tag (orange by default). It is invalid when no effect is selected */@ property (nonatomic, strong) UIColor * tagsSelectedColor; @ end
Expand

 

LGLTagsView. m

// Created by Li Guoliang on 2016/10/17. // Copyright: 2016 Li Guoliang. all rights reserved. // # import "LGLTagsView. h "@ interface LGLTagsView () @ property (nonatomic, strong) NSMutableArray * tagsFrame; @ property (nonatomic, strong) UIButton * startButton; @ property (nonatomic, copy) TagSelectBlock; @ end @ implementation LGLTagsView-(instancetype) initWithFrame :( CGRect) frame tagsFrame :( NSMutableArray *) tagsFrame sel EctTagBlock :( TagSelectBlock) block {self = [super initWithFrame: frame]; if (self) {self. tagsFrame = tagsFrame; self. isSelected = YES; self. tagsSelectedColor = [UIColor orangeColor]; self. block = block; [self createTagsView];} return self;}-(void) createTagsView {for (UIButton * tags in self. tagsFrame) {[tags addTarget: self action: @ selector (tagsClink :) forControlEvents: UIControlEventTouchUpInsi De]; [self addSubview: tags] ;}}-(void) tagsClink :( UIButton *) button {if (self. isSelected) {if (button! = Self. startButton) {self. startButton. selected = NO; [self. startButton setBackgroundColor: [UIColor whiteColor]; self. startButton. layer. borderColor = [UIColor blackColor]. CGColor; self. startButton = button;} self. startButton. selected = YES; if (self. startButton. selected) {[self. startButton setBackgroundColor: self. tagsSelectedColor]; self. startButton. layer. borderColor = [UIColor whiteColor]. CGColor;} Self. block (button. titleLabel. text);}-(void) setIsSelected :( BOOL) isSelected {_ isSelected = isSelected;}-(void) setTagsSelectedColor :( UIColor *) tagsSelectedColor {_ tagsSelectedColor = tagsSelectedColor ;} -(NSMutableArray *) tagsFrame {if (! _ TagsFrame) {_ tagsFrame = [NSMutableArray array];} return _ tagsFrame;} @ end
Expand

The file ends here.

The following describes how to use it:

LGLTagsFrame * frame = [[LGLTagsFrame alloc] init]; frame. tagsArray = [dataSource copy]; LGLTagsView * tagView = [[LGLTagsView alloc] initWithFrame: CGRectMake (0, 0, WIDTH, frame. tagsTotalHeight) tagsFrame: frame. tagsFrames selectTagBlock: ^ (NSString * tagName) {// obtain the tag's click callback value here}]; [self. view addSubview: tagView];

Harassment is welcome if you have any modification comments!

 

Related Article

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.