工作笔记

1、根据 text 和字体大小返回 size

1
CGSize topTilteSize = [KGCommonTools sizeForString:topTilte.text font:topTilte.font];

2、ceil(xx)函数

函数名: ceil

用 法: double ceil(double x);

功 能: 返回大于或者等于指定表达式的最小整数

头文件:math.h


3、UIButton 的一个分类,扩大 button 本身的点击响应范围

1
2
3
4
5
6
7
8
9
10
#import <UIKit/UIKit.h>
#import <objc/runtime.h>

@interface UIButton (EnlargeEdge)

- (void)setEnlargeEdge:(CGFloat)size;

- (void)setEnlargeEdgeWithTop:(CGFloat)top bottom:(CGFloat)bottom left:(CGFloat)left right:(CGFloat)right;

@end
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
#import "UIButton+EnlargeEdge.h"

@implementation UIButton (EnlargeEdge)

static char topNameKey;
static char leftNameKey;
static char rightNameKey;
static char bottomNameKey;

- (void)setEnlargeEdge:(CGFloat)size
{
objc_setAssociatedObject(self, &topNameKey, [NSNumber numberWithFloat:size], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
objc_setAssociatedObject(self, &leftNameKey, [NSNumber numberWithFloat:size], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
objc_setAssociatedObject(self, &rightNameKey, [NSNumber numberWithFloat:size], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
objc_setAssociatedObject(self, &bottomNameKey, [NSNumber numberWithFloat:size], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (void)setEnlargeEdgeWithTop:(CGFloat)top bottom:(CGFloat)bottom left:(CGFloat)left right:(CGFloat)right
{
objc_setAssociatedObject(self, &topNameKey, [NSNumber numberWithFloat:top], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
objc_setAssociatedObject(self, &leftNameKey, [NSNumber numberWithFloat:left], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
objc_setAssociatedObject(self, &rightNameKey, [NSNumber numberWithFloat:right], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
objc_setAssociatedObject(self, &bottomNameKey, [NSNumber numberWithFloat:bottom], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (CGRect)enlargedRect
{
NSNumber *topEdge = objc_getAssociatedObject(self, &topNameKey);
NSNumber *leftEdge = objc_getAssociatedObject(self, &leftNameKey);
NSNumber *rightEdge = objc_getAssociatedObject(self, &rightNameKey);
NSNumber *bottomEdge = objc_getAssociatedObject(self, &bottomNameKey);

if (topEdge && bottomEdge && leftEdge && rightEdge) {
return CGRectMake(self.bounds.origin.x - leftEdge.floatValue,
self.bounds.origin.y - topEdge.floatValue,
self.bounds.size.width + leftEdge.floatValue + rightEdge.floatValue,
self.bounds.size.height + topEdge.floatValue + bottomEdge.floatValue);
} else {
return self.bounds;
}
}

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
CGRect rect = [self enlargedRect];
if (CGRectEqualToRect(rect, self.bounds)) {
return [super pointInside:point withEvent:event];
}
return CGRectContainsPoint(rect, point) ? YES : NO;
}

//Should Not Override hitTest: directly!

//- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
//{
// CGRect rect = [self enlargedRect];
// if (CGRectEqualToRect(rect, self.bounds))
// {
// return [super hitTest:point withEvent:event];
// }
// return CGRectContainsPoint(rect, point) ? self : nil;
//}


@end

使用:

1
[cell.downloadBtn setEnlargeEdgeWithTop:15 bottom:15 left:7.5 right:15]; // 扩大点击范围

参考链接:

iOS事件响应链中Hit-Test View的应用

4、解决阴影和圆角不能同时共存的问题

参考:

如何让阴影和圆角同时实现

5.