修改UITextField的光標顏色
textField.tintColor = [UIColor whiteColor];
UITextField占位文字相關(guān)的設(shè)置
// 設(shè)置占位文字內(nèi)容
@property(nullable, nonatomic,copy) NSString *placeholder;
// 設(shè)置帶有屬性的占位文字, 優(yōu)先級 > placeholder
@property(nullable, nonatomic,copy) NSAttributedString *attributedPlaceholder;
NSAttributedString
- 帶有屬性的字符串, 富文本
- 由2部分組成
- 文字內(nèi)容 : NSString *
- 文字屬性 : NSDictionary *
- 文字顏色 - NSForegroundColorAttributeName
- 字體大小 - NSFontAttributeName
- 下劃線 - NSUnderlineStyleAttributeName
- 背景色 - NSBackgroundColorAttributeName
- 初始化
NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
attributes[NSForegroundColorAttributeName] = [UIColor yellowColor];
attributes[NSBackgroundColorAttributeName] = [UIColor redColor];
attributes[NSUnderlineStyleAttributeName] = @YES;
NSAttributedString *string = [[NSAttributedString alloc] initWithString:@"123" attributes:attributes];
- 使用場合
- UILabel - attributedText
- UITextField - attributedPlaceholder
NSMutableAttributedString
- 繼承自NSAttributedString
- 常見方法
// 設(shè)置range范圍的屬性, 重復(fù)設(shè)置同一個范圍的屬性, 最后一次設(shè)置才是有效的(之前的設(shè)置會被覆蓋掉)
- (void)setAttributes:(nullable NSDictionary<NSString *, id> *)attrs range:(NSRange)range;
// 添加range范圍的屬性, 同一個范圍, 可以不斷累加屬性
- (void)addAttribute:(NSString *)name value:(id)value range:(NSRange)range;
- (void)addAttributes:(NSDictionary<NSString *, id> *)attrs range:(NSRange)range;
- 圖文混排
UILabel *label = [[UILabel alloc] init];
label.frame = CGRectMake(100, 100, 200, 25);
label.backgroundColor = [UIColor redColor];
label.font = [UIFont systemFontOfSize:14];
[self.view addSubview:label];
// 圖文混排
NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] init];
// 1 - 你好
NSAttributedString *first = [[NSAttributedString alloc] initWithString:@"你好"];
[attributedText appendAttributedString:first];
// 2 - 圖片
// 帶有圖片的附件對象
NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
attachment.image = [UIImage imageNamed:@"header_cry_icon"];
CGFloat lineH = label.font.lineHeight;
attachment.bounds = CGRectMake(0, - ((label.wk_height - lineH) * 0.5 - 1), lineH, lineH);
// 將附件對象包裝成一個屬性文字
NSAttributedString *second = [NSAttributedString attributedStringWithAttachment:attachment];
[attributedText appendAttributedString:second];
// 3 - 哈哈哈
NSAttributedString *third = [[NSAttributedString alloc] initWithString:@"哈哈哈"];
[attributedText appendAttributedString:third];
label.attributedText = attributedText;
- 一個Label顯示多行不同字體的文字
UILabel *label = [[UILabel alloc] init];
// 設(shè)置屬性文字
NSString *text = @"你好\n哈哈哈";
NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:text];
[attributedText addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:10] range:NSMakeRange(0, text.length)];
[attributedText addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:13] range:NSMakeRange(3, 3)];
label.attributedText = attributedText;
// 其他設(shè)置
label.numberOfLines = 0;
label.textAlignment = NSTextAlignmentCenter;
label.frame = CGRectMake(0, 0, 100, 40);
[self.view addSubview:label];
self.navigationItem.titleView = label;
在storyboard\xib中給UIScrollView子控件添加約束
- 給添加一個UIView類型的子控件A(這將是UIScrollView唯一的一個子控件)
- 設(shè)置A距離UIScrollView上下左右間距都為0
- 往A中再添加其他子控件
Snip20151109_228.png
- 上下滾動(垂直滾動)
- 設(shè)置A的高度(這個高度就是UIScrollView的內(nèi)容高度: contentSize.height)
Snip20151109_202.png
- 設(shè)置A在UIScrollView中左右居中(水平居中)
Snip20151109_203.png
- 左右滾動(水平滾動)
- 設(shè)置A的寬度(這個寬度就是UIScrollView的內(nèi)容寬度: contentSize.width)
Snip20151109_231.png
- 設(shè)置A在UIScrollView中上下居中(垂直居中)
Snip20151109_230.png
- 上下左右滾動(水平垂直滾動)
- 設(shè)置A的寬度(這個寬度就是UIScrollView的內(nèi)容寬度: contentSize.width)
- 設(shè)置A的高度(這個高度就是UIScrollView的內(nèi)容高度: contentSize.height)
Snip20151109_232.png
Snip20151109_229.png
修改UITextField占位文字的顏色
- 使用attributedPlaceholder
@property(nullable, nonatomic,copy) NSAttributedString *attributedPlaceholder;
- 重寫- (void)drawPlaceholderInRect:(CGRect)rect;
- (void)drawPlaceholderInRect:(CGRect)rect;
- 修改內(nèi)部占位文字Label的文字顏色
[textField setValue:[UIColor grayColor] forKeyPath:@"placeholderLabel.textColor"];
如何監(jiān)聽一個控件內(nèi)部的事件
- 如果繼承自UIControl
- (void)addTarget:(nullable id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;
代理
通知
-
利用內(nèi)部的某些機制
- 比如重寫UITextField的
becomeFirstResponder
和resignFirstResponder
來監(jiān)聽UITextField的獲得焦點和失去焦點事件
- 比如重寫UITextField的
assign和weak的區(qū)別
- 本質(zhì)區(qū)別
- 速度比較: __unsafe_unretained > __weak
@property (nonatomic, assign) WKDog *dog; // WKDog *__unsafe_unretained _dog;
__unsafe_unretained的特點:
1.不是強引用, 不能保住OC對象的命
2.如果引用的OC對象銷毀了, 指針并不會被自動清空, 依然指向銷毀的對象(很容易產(chǎn)生野指針錯誤: EXC_BAD_ACCESS)
@property (nonatomic, weak) WKDog *dog; // WKDog * _Nullable __weak _dog;
__weak的特點:
1.不是強引用, 不能保住OC對象的命
2.如果引用的OC對象銷毀了, 指針會被自動清空(變?yōu)閚il), 不再指向銷毀的對象(永遠不會產(chǎn)生野指針錯誤)
- 用途
- assign一般用在基本數(shù)據(jù)類型上面, 比如int\double等
- weak一般用在代理對象上面, 或者用來解決循環(huán)強引用的問題
監(jiān)聽UITextField的獲得焦點和失去焦點事件
- addTarget
[textField addTarget:target action:@selector(editingDidBegin) forControlEvents:UIControlEventEditingDidBegin];
[textField addTarget:target action:@selector(editingDidEnd) forControlEvents:UIControlEventEditingDidEnd];
UIControlEventEditingDidBegin
1.開始編輯
2.獲得焦點
3.彈出鍵盤
UIControlEventEditingDidEnd
1.結(jié)束編輯
2.失去焦點
3.退下鍵盤
- delegate
textField.delegate = self;
#pragma mark - <UITextFieldDelegate>
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
}
- 通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(beginEditing) name:UITextFieldTextDidBeginEditingNotification object:textField];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(endEditing) name:UITextFieldTextDidEndEditingNotification object:textField];
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)beginEditing
{
}
- (void)endEditing
{
}
- 重寫UITextField的
becomeFirstResponder
和resignFirstResponder
方法
/**
* 調(diào)用時刻 : 成為第一響應(yīng)者(開始編輯\彈出鍵盤\獲得焦點)
*/
- (BOOL)becomeFirstResponder
{
return [super becomeFirstResponder];
}
/**
* 調(diào)用時刻 : 不做第一響應(yīng)者(結(jié)束編輯\退出鍵盤\失去焦點)
*/
- (BOOL)resignFirstResponder
{
return [super resignFirstResponder];
}
枚舉值的某個規(guī)律
- 凡是使用了1 << n格式的枚舉值, 都可以使用|進行組合使用
UIControlEventEditingDidBegin = 1 << 16,
UIControlEventEditingChanged = 1 << 17,
UIControlEventEditingDidEnd = 1 << 18,
UIControlEventEditingDidEndOnExit = 1 << 19,
[textField addTarget:self action:@selector(test) forControlEvents:UIControlEventEditingDidBegin | UIControlEventEditingChanged];
通知相關(guān)的補充
使用block監(jiān)聽通知
// object對象發(fā)出了名字為name的通知, 就在queue隊列中執(zhí)行block
self.observer = [[NSNotificationCenter defaultCenter] addObserverForName:UITextFieldTextDidBeginEditingNotification object:self queue:[[NSOperationQueue alloc] init] usingBlock:^(NSNotification * _Nonnull note) {
// 一旦監(jiān)聽到通知, 就會執(zhí)行這個block中的代碼
}];
// 最后需要移除監(jiān)聽
[[NSNotificationCenter defaultCenter] removeObserver:self.observer];
一次性通知(監(jiān)聽1次后就不再監(jiān)聽)
id observer = [[NSNotificationCenter defaultCenter] addObserverForName:UITextFieldTextDidBeginEditingNotification object:self queue:[[NSOperationQueue alloc] init] usingBlock:^(NSNotification * _Nonnull note) {
// 移除通知
[[NSNotificationCenter defaultCenter] removeObserver:observer];
}];
其他
dispatch_async(dispatch_get_global_queue(0, 0), ^{
// 因為是在子線程注冊了通知監(jiān)聽器, 所以beginEditing和endEditing會在子線程中執(zhí)行
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(beginEditing) name:UITextFieldTextDidBeginEditingNotification object:self];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(endEditing) name:UITextFieldTextDidEndEditingNotification object:self];
});