小知识点3

1.给文字添加阴影

1
2
3
4
5
NSShadow *shadow = [[NSShadow alloc] init];
shadow.shadowBlurRadius = 4.0;
shadow.shadowOffset = CGSizeMake(2, 2);
shadow.shadowColor = commonColor(160, 0, 36, 1);
NSAttributedString *attributedStr = [[NSAttributedString alloc] initWithString: [NSString stringWithFormat:@"%.2f",[self.pointModel.points doubleValue]] attributes:@{NSShadowAttributeName : shadow}];

2.xib添加scrollView

  1. 我们拉一个scrollView到view上,再给scrollView添加上top,leading,bottom,trailing四个约束。这一步是为了确定scrollView的frame.
  2. 我们再拉一个containerView到scrollView上(注意:这个containerView很重要,它是用于确定scrollView的contentSize的),并对其上下左右进行约束,约束完你会发现,约束报错,scrollView的contentSize并不能确定!
  3. 我们给containerView添加两个约束:水平居中的约束和高度约束(垂直滚动)或 垂直居中约束和宽度约束(水平滚动),添加了之后就不再报错了。
  4. 我们在containerView上添加子控件,对最底部的子控件与containerView进行底部约束,然后把containerView的高度约束去掉,也不会报错,这是垂直滚动的做法,水平滚动也是同样的。

3.监听键盘删除按钮的点击
UITextField+Category.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#import <UIKit/UIKit.h>
@protocol WJTextFieldDelegate <UITextFieldDelegate>
@optional
- (void)textFieldDidDeleteBackward:(UITextField *)textField;
@end
@interface UITextField (Category)
@property (weak, nonatomic) id<WJTextFieldDelegate> delegate;
@end
/**
* 监听删除按钮
* object:UITextField
*/
extern NSString * const WJTextFieldDidDeleteBackwardNotification;

UITextField+Category.m

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
#import "UITextField+Category.h"
#import <objc/runtime.h>
NSString * const WJTextFieldDidDeleteBackwardNotification = @"com.whojun.textfield.did.notification";
@implementation UITextField (Category)
+ (void)load {
//交换2个方法中的IMP
Method method1 = class_getInstanceMethod([self class], NSSelectorFromString(@"deleteBackward"));
Method method2 = class_getInstanceMethod([self class], @selector(wj_deleteBackward));
method_exchangeImplementations(method1, method2);
}
- (void)wj_deleteBackward {
[self wj_deleteBackward];
if ([self.delegate respondsToSelector:@selector(textFieldDidDeleteBackward:)])
{
id <WJTextFieldDelegate> delegate = (id<WJTextFieldDelegate>)self.delegate;
[delegate textFieldDidDeleteBackward:self];
}
[[NSNotificationCenter defaultCenter] postNotificationName:WJTextFieldDidDeleteBackwardNotification object:self];
}
@end

4.设置cell自适应高度
设置好cell与子控件之间的约束,最底部的子控件与cell之间的底部约束

1
2
_mainTableView.estimatedRowHeight = 30;
_mainTableView.rowHeight = UITableViewAutomaticDimension;