Skip to content

Instantly share code, notes, and snippets.

@ifournight
Last active December 17, 2015 04:49
Show Gist options
  • Save ifournight/5553091 to your computer and use it in GitHub Desktop.
Save ifournight/5553091 to your computer and use it in GitHub Desktop.
#import <Foundation/Foundation.h>
#import "SDocumentCell.h"
#import "SDocumentTag.h"
// Encapsulate all needed datas of Document Cell.
@interface SDocumentCellData : NSObject
// Main label's attributedText.
@property (nonatomic, strong) NSAttributedString *mainAttributedText;
// Description label's attributedText.
@property (nonatomic, strong) NSAttributedString *descriptionAttributedText;
// Main label's sizeThatFit: size.
@property (nonatomic, assign) CGSize mainFitSize;
// Description label's sizeThatFit: size.
@property (nonatomic, assign) CGSize descriptionFitSize;
// Cell's SDocumentTags.
@property (nonatomic, strong) NSArray *tags;
// Cell's image name.
@property (nonatomic, copy) NSString *imageName;
// Accessory type.
@property (nonatomic, assign) UITableViewCellAccessoryType accessoryType;
// Cell's height.
@property (nonatomic, assign) CGFloat cellHeight;
@end
#import "SDocumentCellData.h"
@implementation SDocumentCellData
- (id)init
{
self = [super init];
if (self) {
}
return;
}
@end
#import <Foundation.h>
#import "SDocumentTagView.h"
@interface SDocumentTag : NSObject
@property (nonatomic, strong) NSAttributedString *tagName;
@property (nonatomic, assign) SDocumentTagType type;
- (id)initWithTagName:(NSAttributedString *)tagName type:(SDocumentTagType)type;
@end
#import "SDocumentTag.h"
@implementation SDocumentTag
- (id)init
{
self = [super init];
if (self) {
}
return self;
}
- (id)initWithTagName:(NSAttributedString *)tagName type:(SDocumentTagType)type
{
self = [self init];
if (self) {
_tagName = tagName;
_type = type;
}
}
@end
#import <UIKit/UIKit.h>
#import "SDocumentTag.h"
typedef NS_ENUM(NSInteger, SDocumentTagType) {
SDocumentTagTypeContainer,
SDocumentTagTypeDeprecated
}
@interface SDocumentTagView : UIView
- (id)initWithTags:(NSArray *)tags;
@end
#import "SDocumentTagView.h"
const NSString *kTagTypeContainerBorderImageName = @"Tag Container Border.png";
const NSString *kTagTypeDeprecatedBorderImageName = @"Tag Deprecated Border.png"
const CGFloat *kTagBorderInset = 5.0;
const CGFloat *kTagBorderSpace = 10.0;
const CGFloat *kTagCornerRadius = 6.0;
@implementation SDocumentTagView
- (id)initWithTags:(NSArray *)tags
{
self = [super init];
if (self) {
CGFloat tagViewWidth = 0.0;
CGFloat tagViewHeight = 0.0;
CGFloat x = 0.0;
for (SDocumentTag *tag in tags) {
UILabel *tagLabel = [UILabel alloc] init];
tagLabel.backgroundColor = [UIColor clearColor];
tagLabel.attributedText = tag.tagName;
[tagLabel sizeToFit];
CGSize tagSize = tagLabel.bounds.size;
NSString *borderImageName = nil;
if (tag.type == SDocumentTagTypeContainer) {
borderImageName = kTagTypeContainerBorderImageName;
} else if (tag.type == SDocumentTagTypeDeprecated) {
borderImageName = kTagTypeDeprecatedBorderImageName;
}
UIImage *border = [[UIImage imageNamed:borderImageName] resizableImageWithCapInsets:UIEdgeInsetsMake(kTagCornerRadius, kTagCornerRadius, kTagCornerRadius, kTagCornerRadius)];
tagLabel.frame = CGRectMake(x + kTagBorderInset, kTagBorderInset, tagSize.width, tagSize.height);
border.frame = CGRectMake(x, 0, tagSize.width + 2 * kTagBorderInset, tagSize.height + 2 * kTagBorderInset);
[self addSubview:border];
[self addSubview:tagLabel];
x += border.bounds.size.width + kTagBorderSpace;
tagViewHeight = border.bounds.size.height;
}
tagViewWidth = x - kTagBorderSpace;
self.frame = CGRectMake(0, 0, tagViewWidth, tagViewHeight);
self.backgroundColor = [UIColor clearColor];
self.shouldRasterize = YES;
}
return self;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment