標籤:style blog io color os ar 使用 for sp
第一章
1 #import "ViewController.h" 2 3 @interface ViewController () 4 5 @end 6 7 @implementation ViewController 8 9 10 - (IBAction)buttonPressed:(UIButton *)sender {11 //取出按鈕的title12 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
精通ios開發第六版,第一章,文章中的例子及習題.