Add emoji scrollview

This commit is contained in:
isaced 2014-12-25 16:31:19 +08:00
parent 5b431b798f
commit cd800dae9e
1 changed files with 71 additions and 8 deletions

View File

@ -8,14 +8,77 @@
#import "ISEmojiView.h"
@implementation ISEmojiView
static const CGFloat EmojiWidth = 50;
static const CGFloat EmojiHeight = 50;
static const CGFloat EmojiFontSize = 32;
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
@interface ISEmojiView()<UIScrollViewDelegate>
@property (nonatomic, strong) NSArray *emojis;
@property (nonatomic, strong) UIScrollView *scrollView;
@end
@implementation ISEmojiView
-(instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
// init emojis
NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"ISEmojiList" ofType:@"plist"];
self.emojis = [NSArray arrayWithContentsOfFile:plistPath];
//
NSInteger rowNum = (CGRectGetWidth(frame) / EmojiWidth);
NSInteger colNum = (CGRectGetHeight(frame) / EmojiHeight);
NSInteger numOfPage = ceil((float)[self.emojis count] / (float)(rowNum * colNum));
// init scrollview
self.scrollView = [[UIScrollView alloc] initWithFrame:frame];
self.scrollView.pagingEnabled = YES;
self.scrollView.showsHorizontalScrollIndicator = NO;
self.scrollView.showsVerticalScrollIndicator = NO;
self.scrollView.delegate = self;
self.scrollView.contentSize = CGSizeMake(CGRectGetWidth(frame) * numOfPage,
CGRectGetHeight(frame));
[self addSubview:self.scrollView];
// add emojis
NSInteger row = 0;
NSInteger column = 0;
NSInteger page = 0;
for (int i = 0; i < [self.emojis count]; i++) {
NSString *emoji = self.emojis[i];
UIButton *emojiButton = [UIButton buttonWithType:UIButtonTypeCustom];
emojiButton.titleLabel.font = [UIFont fontWithName:@"Apple color emoji" size:EmojiFontSize];
[emojiButton setTitle:emoji forState:UIControlStateNormal];
// Pagination
if (i % (rowNum * colNum) == 0) {
page ++; // Increase the number of pages
row = 0; // the number of lines is 0
column = 0; // the number of columns is 0
}else if (i % rowNum == 0) {
// NewLine
row += 1; // Increase the number of lines
column = 0; // The number of columns is 0
}
emojiButton.frame = CGRectMake(((page-1) * frame.size.width) + (column * EmojiWidth),
row * EmojiHeight,
EmojiWidth,
EmojiHeight);
column++;
[self.scrollView addSubview:emojiButton];
}
}
return self;
}
@end