- 一般以set開頭的方法是賦值操作,多次賦值一般會(huì)覆蓋上一次的操作
- 一般以add開頭的方法是添加操作,多次添加一般會(huì)累加
- 注意:這里的set開頭的方法不包括set方法
以富文本屬性作為示例:
#pragma mark ---------- test1-------------
UILabel *label = [[UILabel alloc] init];
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"testtest"];
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
dict[NSFontAttributeName] = [UIFont systemFontOfSize:15];
[string setAttributes:dict range:NSMakeRange(0, 2)];
NSMutableDictionary *dict2 = [NSMutableDictionary dictionary];
dict2[NSForegroundColorAttributeName] = [UIColor blueColor];
dict2[NSUnderlineStyleAttributeName] = @YES;
[string setAttributes:dict2 range:NSMakeRange(0, 3)];
用setAttributes設(shè)置結(jié)果是第二次的操作直接把第一次的操作給覆蓋,雖然它們?cè)O(shè)置的不是同一個(gè)內(nèi)容;
#pragma mark -------- test2-----------------------
UILabel *labelTest = [[UILabel alloc] init];
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"1234567"];
NSMutableDictionary *dictM = [NSMutableDictionary dictionary];
dictM[NSFontAttributeName] = [UIFont systemFontOfSize:18];
[attributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15] range:NSMakeRange(0, 3)];
[attributedString addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:18] range:NSMakeRange(2, 3)];
[attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor]range:NSMakeRange(0, 4)];
labelTest.attributedText = attributedString;
用addAttribute
方法,結(jié)果設(shè)置的內(nèi)容都會(huì)起作用;
Objective-C
中很多這樣類似的方法規(guī)律,再比如UIButton
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
// 會(huì)累加
[btn addTarget:self action:@selector(clickBtn) forControlEvents:UIControlEventTouchUpOutside];
[btn addTarget:self action:@selector(clickBtn) forControlEvents:UIControlEventTouchDownRepeat];
// 會(huì)覆蓋
[btn setTitle:@"111" forState:UIControlStateNormal];
[btn setTitle:@"222" forState:UIControlStateNormal];