十一論劍之iOS項目實戰(四)

修改UITextField的光標顏色

textField.tintColor = [UIColor whiteColor];

UITextField占位文字相關的設置

// 設置占位文字內容
@property(nullable, nonatomic,copy)   NSString               *placeholder;
// 設置帶有屬性的占位文字, 優先級 > placeholder
@property(nullable, nonatomic,copy)   NSAttributedString     *attributedPlaceholder;

NSAttributedString

  • 帶有屬性的字符串, 富文本
  • 由2部分組成
    • 文字內容 : 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
  • 常見方法
// 設置range范圍的屬性, 重復設置同一個范圍的屬性, 最后一次設置才是有效的(之前的設置會被覆蓋掉)
- (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];
// 設置屬性文字
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;
// 其他設置
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唯一的一個子控件)
  • 設置A距離UIScrollView上下左右間距都為0
  • 往A中再添加其他子控件
Snip20151109_228.png
  • 上下滾動(垂直滾動)
    • 設置A的高度(這個高度就是UIScrollView的內容高度: contentSize.height)
Snip20151109_202.png
- 設置A在UIScrollView中左右居中(水平居中)
Snip20151109_203.png
  • 左右滾動(水平滾動)
    • 設置A的寬度(這個寬度就是UIScrollView的內容寬度: contentSize.width)
Snip20151109_231.png
- 設置A在UIScrollView中上下居中(垂直居中)
Snip20151109_230.png
  • 上下左右滾動(水平垂直滾動)
    • 設置A的寬度(這個寬度就是UIScrollView的內容寬度: contentSize.width)
    • 設置A的高度(這個高度就是UIScrollView的內容高度: contentSize.height)
Snip20151109_232.png
Snip20151109_229.png

修改UITextField占位文字的顏色

  • 使用attributedPlaceholder
@property(nullable, nonatomic,copy)   NSAttributedString     *attributedPlaceholder;
  • 重寫- (void)drawPlaceholderInRect:(CGRect)rect;
- (void)drawPlaceholderInRect:(CGRect)rect;
  • 修改內部占位文字Label的文字顏色
[textField setValue:[UIColor grayColor] forKeyPath:@"placeholderLabel.textColor"];

如何監聽一個控件內部的事件

  • 如果繼承自UIControl
- (void)addTarget:(nullable id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;
  • 代理

  • 通知

  • 利用內部的某些機制

    • 比如重寫UITextField的becomeFirstResponderresignFirstResponder來監聽UITextField的獲得焦點和失去焦點事件

assign和weak的區別

  • 本質區別
    • 速度比較: __unsafe_unretained > __weak
@property (nonatomic, assign) WKDog *dog;  // WKDog *__unsafe_unretained _dog;

__unsafe_unretained的特點:
1.不是強引用, 不能保住OC對象的命
2.如果引用的OC對象銷毀了, 指針并不會被自動清空, 依然指向銷毀的對象(很容易產生野指針錯誤: EXC_BAD_ACCESS)

@property (nonatomic, weak) WKDog *dog;  // WKDog * _Nullable __weak _dog;

__weak的特點:
1.不是強引用, 不能保住OC對象的命
2.如果引用的OC對象銷毀了, 指針會被自動清空(變為nil), 不再指向銷毀的對象(永遠不會產生野指針錯誤)
  • 用途
    • assign一般用在基本數據類型上面, 比如int\double等
    • weak一般用在代理對象上面, 或者用來解決循環強引用的問題

監聽UITextField的獲得焦點和失去焦點事件

  • addTarget
[textField addTarget:target action:@selector(editingDidBegin) forControlEvents:UIControlEventEditingDidBegin];
[textField addTarget:target action:@selector(editingDidEnd) forControlEvents:UIControlEventEditingDidEnd];

UIControlEventEditingDidBegin
1.開始編輯
2.獲得焦點
3.彈出鍵盤

UIControlEventEditingDidEnd
1.結束編輯
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的becomeFirstResponderresignFirstResponder方法
/**
 *  調用時刻 : 成為第一響應者(開始編輯\彈出鍵盤\獲得焦點)
 */
- (BOOL)becomeFirstResponder
{

    return [super becomeFirstResponder];
}

/**
 *  調用時刻 : 不做第一響應者(結束編輯\退出鍵盤\失去焦點)
 */
- (BOOL)resignFirstResponder
{

    return [super resignFirstResponder];
}

枚舉值的某個規律

  • 凡是使用了1 << n格式的枚舉值, 都可以使用|進行組合使用
UIControlEventEditingDidBegin                                   = 1 << 16,
UIControlEventEditingChanged                                    = 1 << 17,
UIControlEventEditingDidEnd                                     = 1 << 18,
UIControlEventEditingDidEndOnExit                               = 1 << 19,

[textField addTarget:self action:@selector(test) forControlEvents:UIControlEventEditingDidBegin | UIControlEventEditingChanged];

通知相關的補充

使用block監聽通知

// object對象發出了名字為name的通知, 就在queue隊列中執行block
self.observer = [[NSNotificationCenter defaultCenter] addObserverForName:UITextFieldTextDidBeginEditingNotification object:self queue:[[NSOperationQueue alloc] init] usingBlock:^(NSNotification * _Nonnull note) {
    // 一旦監聽到通知, 就會執行這個block中的代碼
}];

// 最后需要移除監聽
[[NSNotificationCenter defaultCenter] removeObserver:self.observer];

一次性通知(監聽1次后就不再監聽)

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), ^{
    // 因為是在子線程注冊了通知監聽器, 所以beginEditing和endEditing會在子線程中執行
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(beginEditing) name:UITextFieldTextDidBeginEditingNotification object:self];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(endEditing) name:UITextFieldTextDidEndEditingNotification object:self];
});
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容