├── .gitignore ├── LICENSE ├── README.md ├── SKTagView.podspec ├── SKTagView └── Classes │ ├── SKTag.h │ ├── SKTag.m │ ├── SKTagButton.h │ ├── SKTagButton.m │ ├── SKTagView.h │ └── SKTagView.m └── SKTagViewDemo ├── SKTagViewDemo.xcodeproj ├── project.pbxproj └── project.xcworkspace │ └── contents.xcworkspacedata └── SKTagViewDemo ├── AppDelegate.h ├── AppDelegate.m ├── Assets.xcassets └── AppIcon.appiconset │ └── Contents.json ├── Base.lproj ├── LaunchScreen.storyboard └── Main.storyboard ├── Info.plist ├── ViewController.h ├── ViewController.m └── main.m /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | # 3 | # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore 4 | 5 | ## Build generated 6 | build/ 7 | DerivedData/ 8 | 9 | ## Various settings 10 | *.pbxuser 11 | !default.pbxuser 12 | *.mode1v3 13 | !default.mode1v3 14 | *.mode2v3 15 | !default.mode2v3 16 | *.perspectivev3 17 | !default.perspectivev3 18 | xcuserdata/ 19 | 20 | ## Other 21 | *.moved-aside 22 | *.xccheckout 23 | *.xcscmblueprint 24 | 25 | ## Obj-C/Swift specific 26 | *.hmap 27 | *.ipa 28 | *.dSYM.zip 29 | *.dSYM 30 | 31 | # CocoaPods 32 | # 33 | # We recommend against adding the Pods directory to your .gitignore. However 34 | # you should judge for yourself, the pros and cons are mentioned at: 35 | # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control 36 | # 37 | # Pods/ 38 | 39 | # Carthage 40 | # 41 | # Add this line if you want to avoid checking in source code from Carthage dependencies. 42 | # Carthage/Checkouts 43 | 44 | Carthage/Build 45 | 46 | # fastlane 47 | # 48 | # It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the 49 | # screenshots whenever they are needed. 50 | # For more information about the recommended setup visit: 51 | # https://docs.fastlane.tools/best-practices/source-control/#source-control 52 | 53 | fastlane/report.xml 54 | fastlane/Preview.html 55 | fastlane/screenshots 56 | fastlane/test_output 57 | 58 | # Code Injection 59 | # 60 | # After new code Injection tools there's a generated folder /iOSInjectionProject 61 | # https://github.com/johnno1962/injectionforxcode 62 | 63 | iOSInjectionProject/ 64 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 MichaelYu 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SKTagView 2 | 一款可以多选、单选、不选的标签处理工具 3 | 4 | 早年写项目的时候网上找的(原作者不详),后来自己将功能扩展了!以前只是个单选的标签; 5 | 最近整理出来放线上 6 | 7 | 有啥问题大家可以一起交流 8 | 9 | 10 | 11 | 12 | get start 13 | 14 | down: 15 | 16 | https://github.com/michaelyht/SKTagView.git 17 | 18 | pod: 19 | 20 | pod 'SKTagView', :git => "https://github.com/michaelyht/SKTagView", :branch => 'master' 21 | 22 | 23 | 24 | 25 | 26 | ![image](https://raw.githubusercontent.com/michaelyht/Image/master/sktagview_xgt.PNG) 27 | -------------------------------------------------------------------------------- /SKTagView.podspec: -------------------------------------------------------------------------------- 1 | Pod::Spec.new do |s| 2 | 3 | s.name = "SKTagView" 4 | s.version = "0.0.1" 5 | s.summary = "一款可以多选、单选、不选的标签处理工具" 6 | s.description = <<-DESC 7 | 一款可以多选、单选、不选的标签处理工具 8 | 早年写项目的时候网上找的(原作者不详),后来自己将功能扩展了!以前只是个单选的标签; 最近整理出来放线上 9 | DESC 10 | 11 | s.homepage = "https://github.com/michaelyht/SKTagView" 12 | 13 | # ――― Spec License ――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― # 14 | 15 | s.license = { :type => "MIT", :file => "LICENSE" } 16 | 17 | 18 | # ――― Author Metadata ――――――――――――――――――――――――――――――――――――――――――――――――――――――――― # 19 | 20 | s.author = { "michaelyht" => "htlingday@hotmail.com" } 21 | 22 | # ――― Platform Specifics ――――――――――――――――――――――――――――――――――――――――――――――――――――――― # 23 | 24 | s.ios.deployment_target = "8.0" 25 | 26 | 27 | # ――― Source Location ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――― # 28 | 29 | s.source = { :git => "https://github.com/michaelyht/SKTagView.git", :tag => "#{s.version}" } 30 | 31 | 32 | # ――― Source Code ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― # 33 | 34 | s.source_files = "SKTagView/Classes/*.{h,m}" 35 | 36 | #s.exclude_files = "Classes/Exclude" 37 | 38 | # s.public_header_files = "Classes/**/*.h" 39 | 40 | 41 | # ――― Resources ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― # 42 | # 43 | # A list of resources included with the Pod. These are copied into the 44 | # target bundle with a build phase script. Anything else will be cleaned. 45 | # You can preserve files from being cleaned, please don't preserve 46 | # non-essential files like tests, examples and documentation. 47 | # 48 | 49 | # s.resource = "icon.png" 50 | # s.resources = "Resources/*.png" 51 | 52 | # s.preserve_paths = "FilesToSave", "MoreFilesToSave" 53 | 54 | 55 | # ――― Project Linking ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――― # 56 | # 57 | # Link your library with frameworks, or libraries. Libraries do not include 58 | # the lib prefix of their name. 59 | # 60 | 61 | # s.framework = "SomeFramework" 62 | # s.frameworks = "SomeFramework", "AnotherFramework" 63 | 64 | # s.library = "iconv" 65 | # s.libraries = "iconv", "xml2" 66 | 67 | 68 | # ――― Project Settings ――――――――――――――――――――――――――――――――――――――――――――――――――――――――― # 69 | # 70 | # If your library depends on compiler flags you can set them in the xcconfig hash 71 | # where they will only apply to your library. If you depend on other Podspecs 72 | # you can include multiple dependencies to ensure it works. 73 | 74 | s.requires_arc = true 75 | 76 | # s.xcconfig = { "HEADER_SEARCH_PATHS" => "$(SDKROOT)/usr/include/libxml2" } 77 | # s.dependency "JSONKit", "~> 1.4" 78 | 79 | end 80 | -------------------------------------------------------------------------------- /SKTagView/Classes/SKTag.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by Day Ling on 15/1/12. 3 | // Copyright (c) 2015 Day Ling. All rights reserved. 4 | // 5 | 6 | #import 7 | #import 8 | 9 | @interface SKTag : NSObject 10 | 11 | @property (copy, nonatomic, nullable) NSString *text; 12 | @property (copy, nonatomic, nullable) NSAttributedString *attributedText; 13 | @property (strong, nonatomic, nullable) UIColor *textColor; 14 | @property (strong, nonatomic, nullable) UIColor *selectTextColor; 15 | ///backgound color 16 | @property (strong, nonatomic, nullable) UIColor *bgColor; 17 | @property (strong, nonatomic, nullable) UIColor *highlightedBgColor; 18 | @property (strong, nonatomic, nullable) UIColor *selectBgColor; 19 | ///background image 20 | @property (strong, nonatomic, nullable) UIImage *bgImg; 21 | @property (assign, nonatomic) CGFloat cornerRadius; 22 | @property (strong, nonatomic, nullable) UIColor *borderColor; 23 | @property (strong, nonatomic, nullable) UIColor *selectborderColor; 24 | @property (assign, nonatomic) CGFloat borderWidth; 25 | 26 | ///like padding in css 27 | @property (assign, nonatomic) UIEdgeInsets padding; 28 | @property (strong, nonatomic, nullable) UIFont *font; 29 | ///if no font is specified, system font with fontSize is used 30 | @property (assign, nonatomic) CGFloat fontSize; 31 | ///default:YES 32 | @property (assign, nonatomic) BOOL enable; 33 | //是否选中 34 | @property (assign, nonatomic) BOOL isSelect; 35 | 36 | - (nonnull instancetype)initWithText: (nonnull NSString *)text; 37 | + (nonnull instancetype)tagWithText: (nonnull NSString *)text; 38 | 39 | @end 40 | -------------------------------------------------------------------------------- /SKTagView/Classes/SKTag.m: -------------------------------------------------------------------------------- 1 | // 2 | // Created by Day Ling on 15/1/12. 3 | // Copyright (c) 2015 Day Ling. All rights reserved. 4 | // 5 | 6 | #import "SKTag.h" 7 | 8 | static const CGFloat kDefaultFontSize = 13.0; 9 | 10 | @implementation SKTag 11 | 12 | - (instancetype)init { 13 | self = [super init]; 14 | if (self) { 15 | _fontSize = kDefaultFontSize; 16 | _textColor = [UIColor blackColor]; 17 | _bgColor = [UIColor whiteColor]; 18 | _enable = YES; 19 | } 20 | return self; 21 | } 22 | 23 | - (instancetype)initWithText: (NSString *)text { 24 | self = [self init]; 25 | if (self) { 26 | _text = text; 27 | } 28 | return self; 29 | } 30 | 31 | + (instancetype)tagWithText: (NSString *)text { 32 | return [[self alloc] initWithText: text]; 33 | } 34 | 35 | @end 36 | -------------------------------------------------------------------------------- /SKTagView/Classes/SKTagButton.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by Day Ling on 15/1/12. 3 | // Copyright (c) 2015 Day Ling. All rights reserved. 4 | // 5 | 6 | #import 7 | #import 8 | 9 | @class SKTag; 10 | @interface SKTagButton: UIButton 11 | 12 | @property (nonatomic, strong) SKTag * _Nullable skTag; 13 | 14 | + (nonnull instancetype)buttonWithTag:(nonnull SKTag *)tag; 15 | 16 | @end 17 | -------------------------------------------------------------------------------- /SKTagView/Classes/SKTagButton.m: -------------------------------------------------------------------------------- 1 | // 2 | // Created by Day Ling on 15/1/12. 3 | // Copyright (c) 2015 Day Ling. All rights reserved. 4 | // 5 | 6 | #import "SKTagButton.h" 7 | #import "SKTag.h" 8 | 9 | @implementation SKTagButton 10 | 11 | + (instancetype)buttonWithTag:(SKTag *)tag { 12 | SKTagButton *btn = [super buttonWithType:UIButtonTypeCustom]; 13 | btn.skTag = tag; 14 | 15 | if (tag.attributedText) { 16 | [btn setAttributedTitle: tag.attributedText forState: UIControlStateNormal]; 17 | } else { 18 | [btn setTitle: tag.text forState:UIControlStateNormal]; 19 | [btn setTitleColor: tag.textColor forState: UIControlStateNormal]; 20 | btn.titleLabel.font = tag.font ?: [UIFont systemFontOfSize: tag.fontSize]; 21 | } 22 | if (tag.selectTextColor) { 23 | [btn setTitleColor:tag.selectTextColor forState:UIControlStateSelected]; 24 | } 25 | if (tag.selectBgColor) { 26 | UIColor *selectBgColor = tag.selectBgColor; 27 | [btn setBackgroundImage:[self imageWithColor:selectBgColor] forState:UIControlStateSelected]; 28 | } 29 | 30 | btn.selected = tag.isSelect; 31 | btn.backgroundColor = tag.bgColor; 32 | btn.contentEdgeInsets = tag.padding; 33 | btn.titleLabel.lineBreakMode = NSLineBreakByTruncatingTail; 34 | 35 | if (tag.bgImg) { 36 | [btn setBackgroundImage: tag.bgImg forState: UIControlStateNormal]; 37 | } 38 | 39 | 40 | if (btn.selected) { 41 | if (tag.selectborderColor) { 42 | btn.layer.borderColor = tag.selectborderColor.CGColor; 43 | } 44 | 45 | } 46 | 47 | if (!btn.selected) { 48 | if (tag.borderColor) { 49 | btn.layer.borderColor = tag.borderColor.CGColor; 50 | } 51 | 52 | } 53 | 54 | if (tag.borderWidth) { 55 | btn.layer.borderWidth = tag.borderWidth; 56 | } 57 | 58 | btn.userInteractionEnabled = tag.enable; 59 | if (tag.enable) { 60 | UIColor *highlightedBgColor = tag.highlightedBgColor ?: [self darkerColor:btn.backgroundColor]; 61 | [btn setBackgroundImage:[self imageWithColor:highlightedBgColor] forState:UIControlStateHighlighted]; 62 | } 63 | 64 | btn.layer.cornerRadius = tag.cornerRadius; 65 | btn.layer.masksToBounds = YES; 66 | 67 | return btn; 68 | } 69 | 70 | + (UIImage *)imageWithColor:(UIColor *)color { 71 | CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f); 72 | UIGraphicsBeginImageContext(rect.size); 73 | CGContextRef context = UIGraphicsGetCurrentContext(); 74 | 75 | CGContextSetFillColorWithColor(context, [color CGColor]); 76 | CGContextFillRect(context, rect); 77 | 78 | UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 79 | UIGraphicsEndImageContext(); 80 | 81 | return image; 82 | } 83 | 84 | + (UIColor *)darkerColor:(UIColor *)color { 85 | CGFloat h, s, b, a; 86 | if ([color getHue:&h saturation:&s brightness:&b alpha:&a]) 87 | return [UIColor colorWithHue:h 88 | saturation:s 89 | brightness:b * 0.85 90 | alpha:a]; 91 | return color; 92 | } 93 | 94 | @end 95 | -------------------------------------------------------------------------------- /SKTagView/Classes/SKTagView.h: -------------------------------------------------------------------------------- 1 | // 2 | // SKTagView.h 3 | // 4 | // Created by Day Ling on 15/1/12. 5 | // Copyright (c) 2015 Day Ling. All rights reserved. 6 | // 7 | 8 | #import 9 | #import "SKTag.h" 10 | 11 | typedef NS_ENUM(NSInteger, SKTagViewStyle) { 12 | SKTagViewStyleDefault = 0, //不可选 13 | SKTagViewStyleOnlySelectOne, //单选 14 | SKTagViewStyleCanSelectMore //多选 15 | }; 16 | 17 | @interface SKTagView : UIView 18 | 19 | @property (assign, nonatomic) UIEdgeInsets padding; 20 | @property (assign, nonatomic) CGFloat lineSpacing; 21 | @property (assign, nonatomic) CGFloat interitemSpacing; 22 | @property (assign, nonatomic) CGFloat preferredMaxLayoutWidth; 23 | @property (assign, nonatomic) CGFloat regularWidth; //!< 固定宽度 24 | @property (nonatomic, assign) CGFloat regularHeight; //!< 固定高度 25 | @property (nonatomic, assign) SKTagViewStyle tagViewStyle;//是否可以进行多选 26 | @property (assign, nonatomic) BOOL singleLine; 27 | @property (copy, nonatomic, nullable) void (^didTapTagAtIndex)(NSUInteger index , UIButton * _Nonnull bnt); 28 | 29 | - (void)addTag: (nonnull SKTag *)tag; 30 | - (void)insertTag: (nonnull SKTag *)tag atIndex:(NSUInteger)index; 31 | - (void)removeTag: (nonnull SKTag *)tag; 32 | - (void)removeTagAtIndex: (NSUInteger)index; 33 | - (void)removeAllTags; 34 | 35 | @end 36 | 37 | -------------------------------------------------------------------------------- /SKTagView/Classes/SKTagView.m: -------------------------------------------------------------------------------- 1 | // 2 | // SKTagView.m 3 | // 4 | // Created by Day Ling on 15/1/12. 5 | // Copyright (c) 2015 Day Ling. All rights reserved. 6 | // 7 | 8 | #import "SKTagView.h" 9 | #import "SKTagButton.h" 10 | 11 | 12 | @interface SKTagView () 13 | 14 | @property (strong, nonatomic, nullable) NSMutableArray *tags; 15 | @property (assign, nonatomic) BOOL didSetup; 16 | @property (nonatomic,assign) BOOL isIntrinsicWidth; //!<是否宽度固定 17 | @property (nonatomic,assign) BOOL isIntrinsicHeight; //!<是否高度固定 18 | //@property (nonatomic, strong) NSMutableArray *bntArray;//所有的按钮 19 | 20 | @end 21 | 22 | @implementation SKTagView 23 | 24 | // 重写setter给bool赋值 25 | - (void)setRegularWidth:(CGFloat)intrinsicWidth 26 | { 27 | if (_regularWidth != intrinsicWidth) { 28 | _regularWidth = intrinsicWidth; 29 | if (intrinsicWidth == 0) { 30 | self.isIntrinsicWidth = NO; 31 | } 32 | else 33 | { 34 | self.isIntrinsicWidth = YES; 35 | } 36 | } 37 | 38 | } 39 | 40 | - (void)setRegularHeight:(CGFloat)intrinsicHeight 41 | { 42 | if (_regularHeight != intrinsicHeight) { 43 | _regularHeight = intrinsicHeight; 44 | if (intrinsicHeight == 0) 45 | { 46 | self.isIntrinsicHeight = NO; 47 | } 48 | else 49 | { 50 | self.isIntrinsicHeight = YES; 51 | } 52 | } 53 | } 54 | 55 | #pragma mark - Lifecycle 56 | 57 | -(CGSize)intrinsicContentSize { 58 | if (!self.tags.count) { 59 | return CGSizeZero; 60 | } 61 | 62 | NSArray *subviews = self.subviews; 63 | UIView *previousView = nil; 64 | CGFloat topPadding = self.padding.top; 65 | CGFloat bottomPadding = self.padding.bottom; 66 | CGFloat leftPadding = self.padding.left; 67 | CGFloat rightPadding = self.padding.right; 68 | CGFloat itemSpacing = self.interitemSpacing; 69 | CGFloat lineSpacing = self.lineSpacing; 70 | CGFloat currentX = leftPadding; 71 | CGFloat intrinsicHeight = topPadding; 72 | CGFloat intrinsicWidth = leftPadding; 73 | 74 | if (!self.singleLine && self.preferredMaxLayoutWidth > 0) { 75 | NSInteger lineCount = 0; 76 | for (UIView *view in subviews) { 77 | CGSize size = view.intrinsicContentSize; 78 | // 宽度和高度通过参数的0或者非0来进行赋值 79 | CGFloat width = self.isIntrinsicWidth?self.regularWidth:size.width; 80 | CGFloat height = self.isIntrinsicHeight?self.regularHeight:size.height; 81 | if (previousView) { 82 | // CGFloat width = size.width; 83 | currentX += itemSpacing; 84 | if (currentX + width + rightPadding <= self.preferredMaxLayoutWidth) { 85 | currentX += width; 86 | } else { 87 | lineCount ++; 88 | currentX = leftPadding + width; 89 | intrinsicHeight += height; 90 | } 91 | } else { 92 | lineCount ++; 93 | intrinsicHeight += height; 94 | currentX += width; 95 | } 96 | previousView = view; 97 | intrinsicWidth = MAX(intrinsicWidth, currentX + rightPadding); 98 | } 99 | 100 | intrinsicHeight += bottomPadding + lineSpacing * (lineCount - 1); 101 | } else { 102 | for (UIView *view in subviews) { 103 | CGSize size = view.intrinsicContentSize; 104 | intrinsicWidth += self.isIntrinsicWidth?self.regularWidth:size.width; 105 | } 106 | intrinsicWidth += itemSpacing * (subviews.count - 1) + rightPadding; 107 | intrinsicHeight += ((UIView *)subviews.firstObject).intrinsicContentSize.height + bottomPadding; 108 | } 109 | 110 | return CGSizeMake(intrinsicWidth, intrinsicHeight); 111 | } 112 | 113 | - (void)layoutSubviews { 114 | if (!self.singleLine) { 115 | self.preferredMaxLayoutWidth = self.frame.size.width; 116 | } 117 | 118 | [super layoutSubviews]; 119 | 120 | [self layoutTags]; 121 | } 122 | 123 | #pragma mark - Custom accessors 124 | 125 | - (NSMutableArray *)tags { 126 | if(!_tags) { 127 | _tags = [NSMutableArray array]; 128 | } 129 | return _tags; 130 | } 131 | 132 | //- (NSMutableArray *)bntArray{ 133 | // if (!_bntArray) { 134 | // _bntArray = [NSMutableArray array]; 135 | // } 136 | // return _bntArray; 137 | //} 138 | 139 | - (void)setPreferredMaxLayoutWidth: (CGFloat)preferredMaxLayoutWidth { 140 | if (preferredMaxLayoutWidth != _preferredMaxLayoutWidth) { 141 | _preferredMaxLayoutWidth = preferredMaxLayoutWidth; 142 | _didSetup = NO; 143 | [self invalidateIntrinsicContentSize]; 144 | } 145 | } 146 | 147 | #pragma mark - Private 148 | 149 | - (void)layoutTags { 150 | if (self.didSetup || !self.tags.count) { 151 | return; 152 | } 153 | 154 | NSArray *subviews = self.subviews; 155 | UIView *previousView = nil; 156 | CGFloat topPadding = self.padding.top; 157 | CGFloat leftPadding = self.padding.left; 158 | CGFloat rightPadding = self.padding.right; 159 | CGFloat itemSpacing = self.interitemSpacing; 160 | CGFloat lineSpacing = self.lineSpacing; 161 | CGFloat currentX = leftPadding; 162 | 163 | if (!self.singleLine && self.preferredMaxLayoutWidth > 0) { 164 | for (UIView *view in subviews) { 165 | CGSize size = view.intrinsicContentSize; 166 | CGFloat width1 = self.isIntrinsicWidth?self.regularWidth:size.width; 167 | CGFloat height1 = self.isIntrinsicHeight?self.regularHeight:size.height; 168 | if (previousView) { 169 | // CGFloat width = size.width; 170 | currentX += itemSpacing; 171 | if (currentX + width1 + rightPadding <= self.preferredMaxLayoutWidth) { 172 | view.frame = CGRectMake(currentX, CGRectGetMinY(previousView.frame), width1, height1); 173 | currentX += width1; 174 | } else { 175 | CGFloat width = MIN(width1, self.preferredMaxLayoutWidth - leftPadding - rightPadding); 176 | view.frame = CGRectMake(leftPadding, CGRectGetMaxY(previousView.frame) + lineSpacing, width, height1); 177 | currentX = leftPadding + width; 178 | } 179 | } else { 180 | CGFloat width = MIN(width1, self.preferredMaxLayoutWidth - leftPadding - rightPadding); 181 | view.frame = CGRectMake(leftPadding, topPadding, width, height1); 182 | currentX += width; 183 | } 184 | 185 | previousView = view; 186 | } 187 | } else { 188 | for (UIView *view in subviews) { 189 | CGSize size = view.intrinsicContentSize; 190 | view.frame = CGRectMake(currentX, topPadding, self.isIntrinsicWidth?self.regularWidth:size.width, self.isIntrinsicHeight?self.regularHeight:size.height); 191 | currentX += self.isIntrinsicWidth?self.regularWidth:size.width; 192 | 193 | previousView = view; 194 | } 195 | } 196 | 197 | self.didSetup = YES; 198 | } 199 | 200 | #pragma mark - IBActions 201 | 202 | - (void)onTag:(SKTagButton *)btn{ 203 | if (self.tagViewStyle == SKTagViewStyleDefault) { 204 | 205 | } 206 | if(self.tagViewStyle == SKTagViewStyleOnlySelectOne){ 207 | btn.selected = YES; 208 | [self.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { 209 | SKTagButton *bnt = (SKTagButton *)obj; 210 | if (bnt != btn) { 211 | bnt.selected = NO; 212 | if (bnt.skTag.borderColor) { 213 | bnt.layer.borderColor = btn.skTag.borderColor.CGColor; 214 | } 215 | } 216 | }]; 217 | } 218 | if (self.tagViewStyle == SKTagViewStyleCanSelectMore) { 219 | btn.selected = !btn.selected; 220 | } 221 | 222 | if (btn.skTag.borderColor) { 223 | if (!btn.selected) { 224 | btn.layer.borderColor = btn.skTag.borderColor.CGColor; 225 | } 226 | } 227 | 228 | if (btn.skTag.selectborderColor) { 229 | if (btn.selected) { 230 | btn.layer.borderColor = btn.skTag.selectborderColor.CGColor; 231 | } 232 | } 233 | 234 | if (self.didTapTagAtIndex) { 235 | self.didTapTagAtIndex([self.subviews indexOfObject:btn] ,btn); 236 | } 237 | } 238 | 239 | #pragma mark - Public 240 | 241 | - (void)addTag:(SKTag *)tag { 242 | NSParameterAssert(tag); 243 | SKTagButton *btn = [SKTagButton buttonWithTag:tag]; 244 | [btn addTarget:self action:@selector(onTag:) forControlEvents:UIControlEventTouchUpInside]; 245 | [self addSubview: btn]; 246 | [self.tags addObject: tag]; 247 | 248 | self.didSetup = NO; 249 | [self invalidateIntrinsicContentSize]; 250 | } 251 | 252 | - (void)insertTag:(SKTag *)tag atIndex:(NSUInteger)index { 253 | NSParameterAssert(tag); 254 | if (index + 1 > self.tags.count) { 255 | [self addTag:tag]; 256 | } else { 257 | SKTagButton *btn = [SKTagButton buttonWithTag:tag]; 258 | [btn addTarget:self action: @selector(onTag:) forControlEvents:UIControlEventTouchUpInside]; 259 | [self insertSubview:btn atIndex:index]; 260 | [self.tags insertObject:tag atIndex:index]; 261 | 262 | self.didSetup = NO; 263 | [self invalidateIntrinsicContentSize]; 264 | } 265 | } 266 | 267 | - (void)removeTag: (SKTag *)tag { 268 | NSParameterAssert(tag); 269 | NSUInteger index = [self.tags indexOfObject: tag]; 270 | if (NSNotFound == index) { 271 | return; 272 | } 273 | 274 | [self.tags removeObjectAtIndex: index]; 275 | if (self.subviews.count > index) { 276 | [self.subviews[index] removeFromSuperview]; 277 | } 278 | 279 | self.didSetup = NO; 280 | [self invalidateIntrinsicContentSize]; 281 | } 282 | 283 | - (void)removeTagAtIndex: (NSUInteger)index { 284 | if (index + 1 > self.tags.count) { 285 | return; 286 | } 287 | 288 | [self.tags removeObjectAtIndex: index]; 289 | if (self.subviews.count > index) { 290 | [self.subviews[index] removeFromSuperview]; 291 | } 292 | 293 | self.didSetup = NO; 294 | [self invalidateIntrinsicContentSize]; 295 | } 296 | 297 | - (void)removeAllTags { 298 | [self.tags removeAllObjects]; 299 | for (UIView *view in self.subviews) { 300 | [view removeFromSuperview]; 301 | } 302 | 303 | self.didSetup = NO; 304 | [self invalidateIntrinsicContentSize]; 305 | } 306 | 307 | @end 308 | -------------------------------------------------------------------------------- /SKTagViewDemo/SKTagViewDemo.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 48; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 4C030129206CD3B20053E3FE /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 4C030128206CD3B20053E3FE /* AppDelegate.m */; }; 11 | 4C03012C206CD3B20053E3FE /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 4C03012B206CD3B20053E3FE /* ViewController.m */; }; 12 | 4C03012F206CD3B20053E3FE /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 4C03012D206CD3B20053E3FE /* Main.storyboard */; }; 13 | 4C030131206CD3B20053E3FE /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 4C030130206CD3B20053E3FE /* Assets.xcassets */; }; 14 | 4C030134206CD3B20053E3FE /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 4C030132206CD3B20053E3FE /* LaunchScreen.storyboard */; }; 15 | 4C030137206CD3B20053E3FE /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 4C030136206CD3B20053E3FE /* main.m */; }; 16 | 4C030146206CD51A0053E3FE /* SKTagView.m in Sources */ = {isa = PBXBuildFile; fileRef = 4C030141206CD51A0053E3FE /* SKTagView.m */; }; 17 | 4C030147206CD51A0053E3FE /* SKTag.m in Sources */ = {isa = PBXBuildFile; fileRef = 4C030143206CD51A0053E3FE /* SKTag.m */; }; 18 | 4C030148206CD51A0053E3FE /* SKTagButton.m in Sources */ = {isa = PBXBuildFile; fileRef = 4C030145206CD51A0053E3FE /* SKTagButton.m */; }; 19 | 4C03014A206CD9BE0053E3FE /* SKTagView.podspec in Resources */ = {isa = PBXBuildFile; fileRef = 4C030149206CD9BE0053E3FE /* SKTagView.podspec */; }; 20 | 4C03014C206CD9E70053E3FE /* README.md in Resources */ = {isa = PBXBuildFile; fileRef = 4C03014B206CD9E70053E3FE /* README.md */; }; 21 | /* End PBXBuildFile section */ 22 | 23 | /* Begin PBXFileReference section */ 24 | 4C030124206CD3B20053E3FE /* SKTagViewDemo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = SKTagViewDemo.app; sourceTree = BUILT_PRODUCTS_DIR; }; 25 | 4C030127206CD3B20053E3FE /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = ""; }; 26 | 4C030128206CD3B20053E3FE /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = ""; }; 27 | 4C03012A206CD3B20053E3FE /* ViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ViewController.h; sourceTree = ""; }; 28 | 4C03012B206CD3B20053E3FE /* ViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ViewController.m; sourceTree = ""; }; 29 | 4C03012E206CD3B20053E3FE /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 30 | 4C030130206CD3B20053E3FE /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 31 | 4C030133206CD3B20053E3FE /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 32 | 4C030135206CD3B20053E3FE /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 33 | 4C030136206CD3B20053E3FE /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; 34 | 4C030140206CD51A0053E3FE /* SKTagView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SKTagView.h; sourceTree = ""; }; 35 | 4C030141206CD51A0053E3FE /* SKTagView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SKTagView.m; sourceTree = ""; }; 36 | 4C030142206CD51A0053E3FE /* SKTag.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SKTag.h; sourceTree = ""; }; 37 | 4C030143206CD51A0053E3FE /* SKTag.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SKTag.m; sourceTree = ""; }; 38 | 4C030144206CD51A0053E3FE /* SKTagButton.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SKTagButton.h; sourceTree = ""; }; 39 | 4C030145206CD51A0053E3FE /* SKTagButton.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SKTagButton.m; sourceTree = ""; }; 40 | 4C030149206CD9BE0053E3FE /* SKTagView.podspec */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; name = SKTagView.podspec; path = ../SKTagView.podspec; sourceTree = ""; }; 41 | 4C03014B206CD9E70053E3FE /* README.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; name = README.md; path = ../../README.md; sourceTree = ""; }; 42 | /* End PBXFileReference section */ 43 | 44 | /* Begin PBXFrameworksBuildPhase section */ 45 | 4C030121206CD3B20053E3FE /* Frameworks */ = { 46 | isa = PBXFrameworksBuildPhase; 47 | buildActionMask = 2147483647; 48 | files = ( 49 | ); 50 | runOnlyForDeploymentPostprocessing = 0; 51 | }; 52 | /* End PBXFrameworksBuildPhase section */ 53 | 54 | /* Begin PBXGroup section */ 55 | 4C03011B206CD3B20053E3FE = { 56 | isa = PBXGroup; 57 | children = ( 58 | 4C030126206CD3B20053E3FE /* SKTagViewDemo */, 59 | 4C030125206CD3B20053E3FE /* Products */, 60 | ); 61 | sourceTree = ""; 62 | }; 63 | 4C030125206CD3B20053E3FE /* Products */ = { 64 | isa = PBXGroup; 65 | children = ( 66 | 4C030124206CD3B20053E3FE /* SKTagViewDemo.app */, 67 | ); 68 | name = Products; 69 | sourceTree = ""; 70 | }; 71 | 4C030126206CD3B20053E3FE /* SKTagViewDemo */ = { 72 | isa = PBXGroup; 73 | children = ( 74 | 4C03013D206CD3C20053E3FE /* SKTagView */, 75 | 4C030127206CD3B20053E3FE /* AppDelegate.h */, 76 | 4C030128206CD3B20053E3FE /* AppDelegate.m */, 77 | 4C03012A206CD3B20053E3FE /* ViewController.h */, 78 | 4C03012B206CD3B20053E3FE /* ViewController.m */, 79 | 4C03012D206CD3B20053E3FE /* Main.storyboard */, 80 | 4C030130206CD3B20053E3FE /* Assets.xcassets */, 81 | 4C030132206CD3B20053E3FE /* LaunchScreen.storyboard */, 82 | 4C03014B206CD9E70053E3FE /* README.md */, 83 | 4C030135206CD3B20053E3FE /* Info.plist */, 84 | 4C030136206CD3B20053E3FE /* main.m */, 85 | ); 86 | path = SKTagViewDemo; 87 | sourceTree = ""; 88 | }; 89 | 4C03013D206CD3C20053E3FE /* SKTagView */ = { 90 | isa = PBXGroup; 91 | children = ( 92 | 4C030149206CD9BE0053E3FE /* SKTagView.podspec */, 93 | 4C03013E206CD3C20053E3FE /* Assets */, 94 | 4C03013F206CD3C20053E3FE /* Classes */, 95 | ); 96 | name = SKTagView; 97 | path = ../../SKTagView; 98 | sourceTree = ""; 99 | }; 100 | 4C03013E206CD3C20053E3FE /* Assets */ = { 101 | isa = PBXGroup; 102 | children = ( 103 | ); 104 | path = Assets; 105 | sourceTree = ""; 106 | }; 107 | 4C03013F206CD3C20053E3FE /* Classes */ = { 108 | isa = PBXGroup; 109 | children = ( 110 | 4C030140206CD51A0053E3FE /* SKTagView.h */, 111 | 4C030141206CD51A0053E3FE /* SKTagView.m */, 112 | 4C030142206CD51A0053E3FE /* SKTag.h */, 113 | 4C030143206CD51A0053E3FE /* SKTag.m */, 114 | 4C030144206CD51A0053E3FE /* SKTagButton.h */, 115 | 4C030145206CD51A0053E3FE /* SKTagButton.m */, 116 | ); 117 | path = Classes; 118 | sourceTree = ""; 119 | }; 120 | /* End PBXGroup section */ 121 | 122 | /* Begin PBXNativeTarget section */ 123 | 4C030123206CD3B20053E3FE /* SKTagViewDemo */ = { 124 | isa = PBXNativeTarget; 125 | buildConfigurationList = 4C03013A206CD3B20053E3FE /* Build configuration list for PBXNativeTarget "SKTagViewDemo" */; 126 | buildPhases = ( 127 | 4C030120206CD3B20053E3FE /* Sources */, 128 | 4C030121206CD3B20053E3FE /* Frameworks */, 129 | 4C030122206CD3B20053E3FE /* Resources */, 130 | ); 131 | buildRules = ( 132 | ); 133 | dependencies = ( 134 | ); 135 | name = SKTagViewDemo; 136 | productName = SKTagViewDemo; 137 | productReference = 4C030124206CD3B20053E3FE /* SKTagViewDemo.app */; 138 | productType = "com.apple.product-type.application"; 139 | }; 140 | /* End PBXNativeTarget section */ 141 | 142 | /* Begin PBXProject section */ 143 | 4C03011C206CD3B20053E3FE /* Project object */ = { 144 | isa = PBXProject; 145 | attributes = { 146 | LastUpgradeCheck = 0910; 147 | ORGANIZATIONNAME = michaelyht; 148 | TargetAttributes = { 149 | 4C030123206CD3B20053E3FE = { 150 | CreatedOnToolsVersion = 9.1; 151 | ProvisioningStyle = Automatic; 152 | }; 153 | }; 154 | }; 155 | buildConfigurationList = 4C03011F206CD3B20053E3FE /* Build configuration list for PBXProject "SKTagViewDemo" */; 156 | compatibilityVersion = "Xcode 8.0"; 157 | developmentRegion = en; 158 | hasScannedForEncodings = 0; 159 | knownRegions = ( 160 | en, 161 | Base, 162 | ); 163 | mainGroup = 4C03011B206CD3B20053E3FE; 164 | productRefGroup = 4C030125206CD3B20053E3FE /* Products */; 165 | projectDirPath = ""; 166 | projectRoot = ""; 167 | targets = ( 168 | 4C030123206CD3B20053E3FE /* SKTagViewDemo */, 169 | ); 170 | }; 171 | /* End PBXProject section */ 172 | 173 | /* Begin PBXResourcesBuildPhase section */ 174 | 4C030122206CD3B20053E3FE /* Resources */ = { 175 | isa = PBXResourcesBuildPhase; 176 | buildActionMask = 2147483647; 177 | files = ( 178 | 4C030134206CD3B20053E3FE /* LaunchScreen.storyboard in Resources */, 179 | 4C030131206CD3B20053E3FE /* Assets.xcassets in Resources */, 180 | 4C03014C206CD9E70053E3FE /* README.md in Resources */, 181 | 4C03014A206CD9BE0053E3FE /* SKTagView.podspec in Resources */, 182 | 4C03012F206CD3B20053E3FE /* Main.storyboard in Resources */, 183 | ); 184 | runOnlyForDeploymentPostprocessing = 0; 185 | }; 186 | /* End PBXResourcesBuildPhase section */ 187 | 188 | /* Begin PBXSourcesBuildPhase section */ 189 | 4C030120206CD3B20053E3FE /* Sources */ = { 190 | isa = PBXSourcesBuildPhase; 191 | buildActionMask = 2147483647; 192 | files = ( 193 | 4C030148206CD51A0053E3FE /* SKTagButton.m in Sources */, 194 | 4C030147206CD51A0053E3FE /* SKTag.m in Sources */, 195 | 4C03012C206CD3B20053E3FE /* ViewController.m in Sources */, 196 | 4C030137206CD3B20053E3FE /* main.m in Sources */, 197 | 4C030129206CD3B20053E3FE /* AppDelegate.m in Sources */, 198 | 4C030146206CD51A0053E3FE /* SKTagView.m in Sources */, 199 | ); 200 | runOnlyForDeploymentPostprocessing = 0; 201 | }; 202 | /* End PBXSourcesBuildPhase section */ 203 | 204 | /* Begin PBXVariantGroup section */ 205 | 4C03012D206CD3B20053E3FE /* Main.storyboard */ = { 206 | isa = PBXVariantGroup; 207 | children = ( 208 | 4C03012E206CD3B20053E3FE /* Base */, 209 | ); 210 | name = Main.storyboard; 211 | sourceTree = ""; 212 | }; 213 | 4C030132206CD3B20053E3FE /* LaunchScreen.storyboard */ = { 214 | isa = PBXVariantGroup; 215 | children = ( 216 | 4C030133206CD3B20053E3FE /* Base */, 217 | ); 218 | name = LaunchScreen.storyboard; 219 | sourceTree = ""; 220 | }; 221 | /* End PBXVariantGroup section */ 222 | 223 | /* Begin XCBuildConfiguration section */ 224 | 4C030138206CD3B20053E3FE /* Debug */ = { 225 | isa = XCBuildConfiguration; 226 | buildSettings = { 227 | ALWAYS_SEARCH_USER_PATHS = NO; 228 | CLANG_ANALYZER_NONNULL = YES; 229 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 230 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 231 | CLANG_CXX_LIBRARY = "libc++"; 232 | CLANG_ENABLE_MODULES = YES; 233 | CLANG_ENABLE_OBJC_ARC = YES; 234 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 235 | CLANG_WARN_BOOL_CONVERSION = YES; 236 | CLANG_WARN_COMMA = YES; 237 | CLANG_WARN_CONSTANT_CONVERSION = YES; 238 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 239 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 240 | CLANG_WARN_EMPTY_BODY = YES; 241 | CLANG_WARN_ENUM_CONVERSION = YES; 242 | CLANG_WARN_INFINITE_RECURSION = YES; 243 | CLANG_WARN_INT_CONVERSION = YES; 244 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 245 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 246 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 247 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 248 | CLANG_WARN_STRICT_PROTOTYPES = YES; 249 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 250 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 251 | CLANG_WARN_UNREACHABLE_CODE = YES; 252 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 253 | CODE_SIGN_IDENTITY = "iPhone Developer"; 254 | COPY_PHASE_STRIP = NO; 255 | DEBUG_INFORMATION_FORMAT = dwarf; 256 | ENABLE_STRICT_OBJC_MSGSEND = YES; 257 | ENABLE_TESTABILITY = YES; 258 | GCC_C_LANGUAGE_STANDARD = gnu11; 259 | GCC_DYNAMIC_NO_PIC = NO; 260 | GCC_NO_COMMON_BLOCKS = YES; 261 | GCC_OPTIMIZATION_LEVEL = 0; 262 | GCC_PREPROCESSOR_DEFINITIONS = ( 263 | "DEBUG=1", 264 | "$(inherited)", 265 | ); 266 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 267 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 268 | GCC_WARN_UNDECLARED_SELECTOR = YES; 269 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 270 | GCC_WARN_UNUSED_FUNCTION = YES; 271 | GCC_WARN_UNUSED_VARIABLE = YES; 272 | IPHONEOS_DEPLOYMENT_TARGET = 11.1; 273 | MTL_ENABLE_DEBUG_INFO = YES; 274 | ONLY_ACTIVE_ARCH = YES; 275 | SDKROOT = iphoneos; 276 | }; 277 | name = Debug; 278 | }; 279 | 4C030139206CD3B20053E3FE /* Release */ = { 280 | isa = XCBuildConfiguration; 281 | buildSettings = { 282 | ALWAYS_SEARCH_USER_PATHS = NO; 283 | CLANG_ANALYZER_NONNULL = YES; 284 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 285 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 286 | CLANG_CXX_LIBRARY = "libc++"; 287 | CLANG_ENABLE_MODULES = YES; 288 | CLANG_ENABLE_OBJC_ARC = YES; 289 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 290 | CLANG_WARN_BOOL_CONVERSION = YES; 291 | CLANG_WARN_COMMA = YES; 292 | CLANG_WARN_CONSTANT_CONVERSION = YES; 293 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 294 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 295 | CLANG_WARN_EMPTY_BODY = YES; 296 | CLANG_WARN_ENUM_CONVERSION = YES; 297 | CLANG_WARN_INFINITE_RECURSION = YES; 298 | CLANG_WARN_INT_CONVERSION = YES; 299 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 300 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 301 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 302 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 303 | CLANG_WARN_STRICT_PROTOTYPES = YES; 304 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 305 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 306 | CLANG_WARN_UNREACHABLE_CODE = YES; 307 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 308 | CODE_SIGN_IDENTITY = "iPhone Developer"; 309 | COPY_PHASE_STRIP = NO; 310 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 311 | ENABLE_NS_ASSERTIONS = NO; 312 | ENABLE_STRICT_OBJC_MSGSEND = YES; 313 | GCC_C_LANGUAGE_STANDARD = gnu11; 314 | GCC_NO_COMMON_BLOCKS = YES; 315 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 316 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 317 | GCC_WARN_UNDECLARED_SELECTOR = YES; 318 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 319 | GCC_WARN_UNUSED_FUNCTION = YES; 320 | GCC_WARN_UNUSED_VARIABLE = YES; 321 | IPHONEOS_DEPLOYMENT_TARGET = 11.1; 322 | MTL_ENABLE_DEBUG_INFO = NO; 323 | SDKROOT = iphoneos; 324 | VALIDATE_PRODUCT = YES; 325 | }; 326 | name = Release; 327 | }; 328 | 4C03013B206CD3B20053E3FE /* Debug */ = { 329 | isa = XCBuildConfiguration; 330 | buildSettings = { 331 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 332 | CODE_SIGN_STYLE = Automatic; 333 | DEVELOPMENT_TEAM = 28A5V88VN2; 334 | INFOPLIST_FILE = SKTagViewDemo/Info.plist; 335 | IPHONEOS_DEPLOYMENT_TARGET = 8.0; 336 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 337 | PRODUCT_BUNDLE_IDENTIFIER = com.michaelyht.SKTagViewDemo; 338 | PRODUCT_NAME = "$(TARGET_NAME)"; 339 | TARGETED_DEVICE_FAMILY = "1,2"; 340 | }; 341 | name = Debug; 342 | }; 343 | 4C03013C206CD3B20053E3FE /* Release */ = { 344 | isa = XCBuildConfiguration; 345 | buildSettings = { 346 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 347 | CODE_SIGN_STYLE = Automatic; 348 | DEVELOPMENT_TEAM = 28A5V88VN2; 349 | INFOPLIST_FILE = SKTagViewDemo/Info.plist; 350 | IPHONEOS_DEPLOYMENT_TARGET = 8.0; 351 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 352 | PRODUCT_BUNDLE_IDENTIFIER = com.michaelyht.SKTagViewDemo; 353 | PRODUCT_NAME = "$(TARGET_NAME)"; 354 | TARGETED_DEVICE_FAMILY = "1,2"; 355 | }; 356 | name = Release; 357 | }; 358 | /* End XCBuildConfiguration section */ 359 | 360 | /* Begin XCConfigurationList section */ 361 | 4C03011F206CD3B20053E3FE /* Build configuration list for PBXProject "SKTagViewDemo" */ = { 362 | isa = XCConfigurationList; 363 | buildConfigurations = ( 364 | 4C030138206CD3B20053E3FE /* Debug */, 365 | 4C030139206CD3B20053E3FE /* Release */, 366 | ); 367 | defaultConfigurationIsVisible = 0; 368 | defaultConfigurationName = Release; 369 | }; 370 | 4C03013A206CD3B20053E3FE /* Build configuration list for PBXNativeTarget "SKTagViewDemo" */ = { 371 | isa = XCConfigurationList; 372 | buildConfigurations = ( 373 | 4C03013B206CD3B20053E3FE /* Debug */, 374 | 4C03013C206CD3B20053E3FE /* Release */, 375 | ); 376 | defaultConfigurationIsVisible = 0; 377 | defaultConfigurationName = Release; 378 | }; 379 | /* End XCConfigurationList section */ 380 | }; 381 | rootObject = 4C03011C206CD3B20053E3FE /* Project object */; 382 | } 383 | -------------------------------------------------------------------------------- /SKTagViewDemo/SKTagViewDemo.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /SKTagViewDemo/SKTagViewDemo/AppDelegate.h: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.h 3 | // SKTagViewDemo 4 | // 5 | // Created by Michael on 2018/3/29. 6 | // Copyright © 2018年 michaelyht. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface AppDelegate : UIResponder 12 | 13 | @property (strong, nonatomic) UIWindow *window; 14 | 15 | 16 | @end 17 | 18 | -------------------------------------------------------------------------------- /SKTagViewDemo/SKTagViewDemo/AppDelegate.m: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.m 3 | // SKTagViewDemo 4 | // 5 | // Created by Michael on 2018/3/29. 6 | // Copyright © 2018年 michaelyht. All rights reserved. 7 | // 8 | 9 | #import "AppDelegate.h" 10 | 11 | @interface AppDelegate () 12 | 13 | @end 14 | 15 | @implementation AppDelegate 16 | 17 | 18 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 19 | // Override point for customization after application launch. 20 | return YES; 21 | } 22 | 23 | 24 | - (void)applicationWillResignActive:(UIApplication *)application { 25 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 26 | // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game. 27 | } 28 | 29 | 30 | - (void)applicationDidEnterBackground:(UIApplication *)application { 31 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 32 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 33 | } 34 | 35 | 36 | - (void)applicationWillEnterForeground:(UIApplication *)application { 37 | // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. 38 | } 39 | 40 | 41 | - (void)applicationDidBecomeActive:(UIApplication *)application { 42 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 43 | } 44 | 45 | 46 | - (void)applicationWillTerminate:(UIApplication *)application { 47 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 48 | } 49 | 50 | 51 | @end 52 | -------------------------------------------------------------------------------- /SKTagViewDemo/SKTagViewDemo/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "20x20", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "20x20", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "29x29", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "29x29", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "40x40", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "40x40", 31 | "scale" : "3x" 32 | }, 33 | { 34 | "idiom" : "iphone", 35 | "size" : "60x60", 36 | "scale" : "2x" 37 | }, 38 | { 39 | "idiom" : "iphone", 40 | "size" : "60x60", 41 | "scale" : "3x" 42 | }, 43 | { 44 | "idiom" : "ipad", 45 | "size" : "20x20", 46 | "scale" : "1x" 47 | }, 48 | { 49 | "idiom" : "ipad", 50 | "size" : "20x20", 51 | "scale" : "2x" 52 | }, 53 | { 54 | "idiom" : "ipad", 55 | "size" : "29x29", 56 | "scale" : "1x" 57 | }, 58 | { 59 | "idiom" : "ipad", 60 | "size" : "29x29", 61 | "scale" : "2x" 62 | }, 63 | { 64 | "idiom" : "ipad", 65 | "size" : "40x40", 66 | "scale" : "1x" 67 | }, 68 | { 69 | "idiom" : "ipad", 70 | "size" : "40x40", 71 | "scale" : "2x" 72 | }, 73 | { 74 | "idiom" : "ipad", 75 | "size" : "76x76", 76 | "scale" : "1x" 77 | }, 78 | { 79 | "idiom" : "ipad", 80 | "size" : "76x76", 81 | "scale" : "2x" 82 | }, 83 | { 84 | "idiom" : "ipad", 85 | "size" : "83.5x83.5", 86 | "scale" : "2x" 87 | } 88 | ], 89 | "info" : { 90 | "version" : 1, 91 | "author" : "xcode" 92 | } 93 | } -------------------------------------------------------------------------------- /SKTagViewDemo/SKTagViewDemo/Base.lproj/LaunchScreen.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /SKTagViewDemo/SKTagViewDemo/Base.lproj/Main.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | -------------------------------------------------------------------------------- /SKTagViewDemo/SKTagViewDemo/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | $(DEVELOPMENT_LANGUAGE) 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | APPL 17 | CFBundleShortVersionString 18 | 0.0.1 19 | CFBundleVersion 20 | 1 21 | LSRequiresIPhoneOS 22 | 23 | UILaunchStoryboardName 24 | LaunchScreen 25 | UIMainStoryboardFile 26 | Main 27 | UIRequiredDeviceCapabilities 28 | 29 | armv7 30 | 31 | UISupportedInterfaceOrientations 32 | 33 | UIInterfaceOrientationPortrait 34 | UIInterfaceOrientationLandscapeLeft 35 | UIInterfaceOrientationLandscapeRight 36 | 37 | UISupportedInterfaceOrientations~ipad 38 | 39 | UIInterfaceOrientationPortrait 40 | UIInterfaceOrientationPortraitUpsideDown 41 | UIInterfaceOrientationLandscapeLeft 42 | UIInterfaceOrientationLandscapeRight 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /SKTagViewDemo/SKTagViewDemo/ViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.h 3 | // SKTagViewDemo 4 | // 5 | // Created by Michael on 2018/3/29. 6 | // Copyright © 2018年 michaelyht. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface ViewController : UIViewController 12 | 13 | 14 | @end 15 | 16 | -------------------------------------------------------------------------------- /SKTagViewDemo/SKTagViewDemo/ViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.m 3 | // SKTagViewDemo 4 | // 5 | // Created by Michael on 2018/3/29. 6 | // Copyright © 2018年 michaelyht. All rights reserved. 7 | // 8 | 9 | #import "ViewController.h" 10 | 11 | #import "SKTagView.h" 12 | 13 | @interface ViewController (){ 14 | 15 | __weak IBOutlet SKTagView *_sktagViewSM;//多选 16 | __weak IBOutlet SKTagView *_sktagViewSO;//单选 17 | __weak IBOutlet SKTagView *_sktagViewNo;//不选 18 | 19 | NSArray *_dataArray; 20 | } 21 | 22 | @end 23 | 24 | @implementation ViewController 25 | #pragma mark - life Cycle 26 | - (void)viewDidLoad { 27 | [super viewDidLoad]; 28 | // Do any additional setup after loading the view, typically from a nib. 29 | _dataArray = @[@"水电费",@"是",@"稍等",@"稍等",@"似懂非懂舒服",@"是",@"撒的方式地方",@"水电费",@"刚好符合",@"突然",@"宝宝",@"opofsd"]; 30 | 31 | //init tagview 32 | [self initNoWithType:SKTagViewStyleDefault skView:_sktagViewNo]; 33 | [self initNoWithType:SKTagViewStyleOnlySelectOne skView:_sktagViewSO]; 34 | [self initNoWithType:SKTagViewStyleCanSelectMore skView:_sktagViewSM]; 35 | } 36 | 37 | #pragma mark - private mehtods 38 | 39 | - (void)initNoWithType:(SKTagViewStyle)type skView:(SKTagView *)skTagView{ 40 | 41 | skTagView.preferredMaxLayoutWidth = CGRectGetWidth(skTagView.frame); 42 | skTagView.padding = UIEdgeInsetsMake(12.f, 15.f, 12.f, 15.f); 43 | skTagView.interitemSpacing = 15; 44 | skTagView.lineSpacing = 16; 45 | skTagView.regularHeight = 31.f; 46 | skTagView.tagViewStyle = SKTagViewStyleCanSelectMore;//多选 47 | [skTagView removeAllTags]; 48 | skTagView.tagViewStyle = type; 49 | //Add Tagse 50 | [_dataArray enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { 51 | SKTag *tag = [SKTag tagWithText:obj]; 52 | tag.textColor = [UIColor grayColor]; 53 | tag.selectTextColor = [UIColor whiteColor]; 54 | tag.fontSize = 14.f; 55 | tag.padding = UIEdgeInsetsMake(5.f, 20.f, 5.f, 20.f); 56 | 57 | tag.bgColor = [UIColor clearColor]; 58 | tag.selectBgColor = [UIColor colorWithRed:0.2 green:0.3 blue:0.5 alpha:0.5]; 59 | 60 | tag.borderColor = [UIColor grayColor]; 61 | tag.selectborderColor = [UIColor clearColor]; 62 | tag.borderWidth = 1.f; 63 | tag.cornerRadius = 15.5; 64 | 65 | [skTagView addTag:tag]; 66 | }]; 67 | 68 | skTagView.didTapTagAtIndex = ^(NSUInteger index , UIButton * _Nonnull bnt){ 69 | NSLog(@"%ld",index); 70 | }; 71 | } 72 | 73 | #pragma mark - other ... 74 | - (void)didReceiveMemoryWarning { 75 | [super didReceiveMemoryWarning]; 76 | // Dispose of any resources that can be recreated. 77 | } 78 | 79 | 80 | @end 81 | -------------------------------------------------------------------------------- /SKTagViewDemo/SKTagViewDemo/main.m: -------------------------------------------------------------------------------- 1 | // 2 | // main.m 3 | // SKTagViewDemo 4 | // 5 | // Created by Michael on 2018/3/29. 6 | // Copyright © 2018年 michaelyht. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "AppDelegate.h" 11 | 12 | int main(int argc, char * argv[]) { 13 | @autoreleasepool { 14 | return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); 15 | } 16 | } 17 | --------------------------------------------------------------------------------