《精通 iOS 开发》第 10 章学习笔记

自定义单元

1.创建标签:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
- (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;
}

2.重写text属性:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
- (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;
newContentFrame.size = textSize;
self.label.frame = newLabelFrame;
self.contentView.frame = newContentFrame;
}

3.获取字体尺寸:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
+ (UIFont *)defaultFont {
return [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
}

+ (CGSize)sizeForContentString:(NSString *)string {
CGSize maxSize = CGSizeMake(300, 1000);

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;
}

配置视图控制器

1.初始化:

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
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.sections = @[
@{
@"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, 20);

[self.collectionView registerClass:[BIDHeaderCell class]
forSupplementaryViewOfKind:UICollectionElementKindSectionHeader
withReuseIdentifier:@"HEADER"];

flow.headerReferenceSize = CGSizeMake(100, 25);
}

2.实现流式布局:

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
- (NSArray *)wordsInSection:(NSInteger)setion {
NSString *content = self.sections[setion][@"content"];
NSCharacterSet *space = [NSCharacterSet whitespaceAndNewlineCharacterSet];
NSArray *words = [content componentsSeparatedByCharactersInSet:space];
return words;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView
numberOfItemsInSection:(NSInteger)section {
NSArray *words = [self wordsInSection:section];
return [words count];
}

- (NSInteger)numberOfSectionsInCollectionView:
(UICollectionView *)collectionView {
return [self.sections count];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath {
NSArray *words = [self wordsInSection: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 wordsInSection: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.sections[indexPath.row][@"header"];
return cell;
}
return nil;
}