利用UICollectionView
实现如下效果:

- 自定义
BIDContentCell
继承自UICollectionViewCell
:
#import <UIKit/UIKit.h>
@interface BIDContentCell : UICollectionViewCell
@property (strong, nonatomic) UILabel *label;
@property (copy, nonatomic) NSString *text;
+ (CGSize)sizeForContentString: (NSString *)string;
@end
#import "BIDContentCell.h"
@implementation BIDContentCell
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.label = [[UILabel alloc] initWithFrame:self.contentView.bounds];
self.label.opaque = NO;
self.label.backgroundColor = [UIColor colorWithRed:0.8 green:0.9 blue:1.0 alpha:1.0];
self.label.textColor = [UIColor blackColor];
self.label.textAlignment = NSTextAlignmentCenter;
self.label.font = [[self class] defaultFont];
[self.contentView addSubview:self.label];
}
return self;
}
+ (UIFont *)defaultFont {
return [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
}
+ (CGSize)sizeForContentString:(NSString *)string {
CGSize maxSize = CGSizeMake(300, 100);
NSStringDrawingOptions opts = NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading;
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
[style setLineBreakMode:NSLineBreakByCharWrapping];
NSDictionary *attributes = @{
NSFontAttributeName : [self defaultFont],
NSParagraphStyleAttributeName : style
};
CGRect rect = [string boundingRectWithSize:maxSize options:opts attributes:attributes context:nil];
return rect.size;
}
- (NSString *)text {
return self.label.text;
}
- (void)setText:(NSString *)text {
self.label.text = text;
CGRect newLabelFrame = self.label.frame;
CGRect newContentFrame = self.contentView.frame;
CGSize textSize = [[self class] sizeForContentString:text];
newLabelFrame.size = textSize;
self.label.frame = newLabelFrame;
self.contentView.frame = newContentFrame;
}
@end
+ (CGSize)sizeForContentString: (NSString *)string;
这个方法返回每个word
所占的Size
-
BIDHeaderCell
继承自BIDContentCell
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.label.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.8 alpha:1.0];
self.label.textColor = [UIColor blackColor];
}
return self;
}
+ (UIFont *)defaultFont {
return [UIFont preferredFontForTextStyle:UIFontTextStyleHeadline];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.section = @[
@{
@"header" : @"First Witch",
@"content" : @"Hey, when will the three of us meet up later?"
},
@{
@"header" : @"Second Witch",
@"content" : @"When everything's straightened out."
},
@{
@"header" : @"Third Witch",
@"content" : @"That'll be just before sunset."
},
@{ @"header" : @"First Witch",
@"content" : @"Where?" },
@{ @"header" : @"Second Witch",
@"content" : @"The dirt patch." },
@{ @"header" : @"Third Witch",
@"content" : @"I guess we'll see Mac there." }
];
[self.collectionView registerClass:[BIDContentCell class] forCellWithReuseIdentifier:@"CONTENT"];
self.collectionView.backgroundColor = [UIColor whiteColor];
UIEdgeInsets contentInset = self.collectionView.contentInset;
contentInset.top = 20;
[self.collectionView setContentInset:contentInset];
UICollectionViewLayout *layout = self.collectionView.collectionViewLayout;
UICollectionViewFlowLayout *flow = (UICollectionViewFlowLayout *)layout;
flow.sectionInset = UIEdgeInsetsMake(10, 20, 30, 40);
[self.collectionView registerClass:[BIDHeaderCell class]
forSupplementaryViewOfKind:UICollectionElementKindSectionHeader
withReuseIdentifier:@"HEADER"];
flow.headerReferenceSize = CGSizeMake(100, 25);
}
- (NSArray *)wordsInSetction:(NSInteger)section {
NSString *content = self.section[section][@"content"];
NSCharacterSet *space = [NSCharacterSet whitespaceAndNewlineCharacterSet];
NSArray *words = [content componentsSeparatedByCharactersInSet:space];
return words;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return [self.section count];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
NSArray *words = [self wordsInSetction:section];
return [words count];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
NSArray *words = [self wordsInSetction:indexPath.section];
BIDContentCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:@"CONTENT"forIndexPath:indexPath];
cell.text = words[indexPath.row];
return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout*)collectionViewLayout
sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
NSArray *words = [self wordsInSetction:indexPath.section];
CGSize size = [BIDContentCell sizeForContentString:words[indexPath.row]];
return size;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView
viewForSupplementaryElementOfKind:(NSString *)kind
atIndexPath:(NSIndexPath *)indexPath {
if ([kind isEqual:UICollectionElementKindSectionHeader]) {
BIDHeaderCell *cell = [self.collectionView
dequeueReusableSupplementaryViewOfKind:kind
withReuseIdentifier:@"HEADER"
forIndexPath:indexPath];
cell.text = self.section[indexPath.section][@"header"];
return cell;
}
return nil;
}
网友评论