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

修改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的becomeFirstResponderresignFirstResponder來監(jiān)聽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的becomeFirstResponderresignFirstResponder方法
/**
 *  調(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];
});
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末红且,一起剝皮案震驚了整個濱河市殴瘦,隨后出現(xiàn)的幾起案子债鸡,更是在濱河造成了極大的恐慌阔涉,老刑警劉巖慢宗,帶你破解...
    沈念sama閱讀 222,946評論 6 518
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件惨撇,死亡現(xiàn)場離奇詭異况毅,居然都是意外死亡导犹,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,336評論 3 399
  • 文/潘曉璐 我一進店門桨仿,熙熙樓的掌柜王于貴愁眉苦臉地迎上來睛低,“玉大人,你說我怎么就攤上這事服傍∏祝” “怎么了?”我有些...
    開封第一講書人閱讀 169,716評論 0 364
  • 文/不壞的土叔 我叫張陵吹零,是天一觀的道長罩抗。 經(jīng)常有香客問我,道長灿椅,這世上最難降的妖魔是什么套蒂? 我笑而不...
    開封第一講書人閱讀 60,222評論 1 300
  • 正文 為了忘掉前任,我火速辦了婚禮阱扬,結(jié)果婚禮上泣懊,老公的妹妹穿的比我還像新娘。我一直安慰自己麻惶,他們只是感情好馍刮,可當我...
    茶點故事閱讀 69,223評論 6 398
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著窃蹋,像睡著了一般卡啰。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上警没,一...
    開封第一講書人閱讀 52,807評論 1 314
  • 那天匈辱,我揣著相機與錄音,去河邊找鬼杀迹。 笑死亡脸,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播浅碾,決...
    沈念sama閱讀 41,235評論 3 424
  • 文/蒼蘭香墨 我猛地睜開眼大州,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了垂谢?” 一聲冷哼從身側(cè)響起厦画,我...
    開封第一講書人閱讀 40,189評論 0 277
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎滥朱,沒想到半個月后根暑,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,712評論 1 320
  • 正文 獨居荒郊野嶺守林人離奇死亡徙邻,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,775評論 3 343
  • 正文 我和宋清朗相戀三年排嫌,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片鹃栽。...
    茶點故事閱讀 40,926評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡躏率,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出民鼓,到底是詐尸還是另有隱情,我是刑警寧澤蓬抄,帶...
    沈念sama閱讀 36,580評論 5 351
  • 正文 年R本政府宣布丰嘉,位于F島的核電站,受9級特大地震影響嚷缭,放射性物質(zhì)發(fā)生泄漏饮亏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 42,259評論 3 336
  • 文/蒙蒙 一阅爽、第九天 我趴在偏房一處隱蔽的房頂上張望路幸。 院中可真熱鬧,春花似錦付翁、人聲如沸简肴。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,750評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽砰识。三九已至,卻和暖如春佣渴,著一層夾襖步出監(jiān)牢的瞬間辫狼,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,867評論 1 274
  • 我被黑心中介騙來泰國打工辛润, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留膨处,地道東北人。 一個月前我還...
    沈念sama閱讀 49,368評論 3 379
  • 正文 我出身青樓,卻偏偏與公主長得像真椿,于是被迫代替她去往敵國和親鹃答。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 45,930評論 2 361

推薦閱讀更多精彩內(nèi)容