第一章
1 #import "ViewController.h" 2 3 @interface ViewController () 4 5 @end 6 7 @implementation ViewController 8 9 10 - (IBAction)buttonPressed:(UIButton *)sender { 11 //取出按钮的title 12 NSString *title = [sender titleForState:UIControlStateNormal]; 13 14 //用取出的title格式化为字符串,并赋值给新的NSString对象 15 NSString *plainText = [NSString stringWithFormat:@"pressing %@ button",title]; 16 17 // _statusLabel.text = plainText; //将格式化的字符串赋给标签的text属性 18 19 //可变的字符串属性类,创建对象 20 NSMutableAttributedString *textStyle = [[NSMutableAttributedString alloc]initWithString:plainText]; 21 22 //NSDictionary字典新语法 @{}用于保存字符串属性.. NSArray 数组新语法是 @[] 23 NSDictionary *dict = 24 @{ 25 NSFontAttributeName:[UIFont boldSystemFontOfSize:_statusLabel.font.pointSize] 26 }; 27 28 //取出plainText字符串中待改变的子字符串 即 title 也可以使用@""取出其他字符串 29 NSRange nameRange = [plainText rangeOfString:title]; 30 31 //设置字符串属性. 32 [textStyle setAttributes:dict range:nameRange]; 33 34 _statusLabel.attributedText = textStyle; 35 36 37 } 38 @end
时间: 2024-10-13 00:27:11