2013年5月15日 星期三

NSMutableAttributedString

UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(85 , 85, 150, 150)];

NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"Hello World, Programming In Objective-C"];
NSRange range = NSMakeRange(0,string.length);

UIFont *markerFeltWide = [UIFont fontWithName:@"MarkerFelt-Wide" size:50.0f];
 
//字型
[string addAttribute:NSFontAttributeName value:markerFeltWide range:range];
 
//前景顏色
[string addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:range];
 
//背景顏色
[string addAttribute:NSBackgroundColorAttributeName value:[UIColor grayColor] range:range];

//底線
[string addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInt:1] range:range];
 
//字間距
[string addAttribute:NSKernAttributeName value:[NSNumber numberWithInt:0] range:range];
 
//陰影
NSShadow *shadowDic=[[NSShadow alloc] init];
[shadowDic setShadowBlurRadius:3.0]; //0 ~ ? 清晰~模糊
[shadowDic setShadowColor:[UIColor blackColor]];
[shadowDic setShadowOffset:CGSizeMake(3, 3)];
[string addAttribute:NSShadowAttributeName value:shadowDic range:range];
 
//描邊顏色
[string addAttribute:NSStrokeColorAttributeName value:[UIColor greenColor] range:range];
 
//描邊線條粗細 正數描邊 負數描邊加填滿
[string addAttribute:NSStrokeWidthAttributeName value:[NSNumber numberWithInt:-3.0] range:range];
 
[lbl setAttributedText:string];
[self.view addSubview:lbl];

這些property設定的好, 可以做出漂亮的效果



它是使用Key-Vaule, 所以也可以將一個Dict包含要設定的K-V然後指定給NSMutableAttributedString並給定string range
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(20 , 20, 280, 280)];
 [lbl setBackgroundColor:[UIColor grayColor]];
 [lbl setNumberOfLines:0];

 NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"Hello World, Programming In Objective-C"];
 
 NSDictionary *dict = [NSDictionary dictionaryWithObjects:@[[UIFont fontWithName:@"Helvetica-Bold" size:45.0f], [UIColor greenColor], [UIColor redColor], [NSNumber numberWithFloat:-3.0]]
              forKeys:@[NSFontAttributeName, NSForegroundColorAttributeName, NSStrokeColorAttributeName, NSStrokeWidthAttributeName]];
 [string addAttributes:dict range:NSMakeRange(0,string.length)];
 
 [lbl setAttributedText:string];
 [self.view addSubview:lbl];

也可以使用dic, key為要顯示的string, value為Attribute
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(20 , 20, 280, 280)];
 [lbl setBackgroundColor:[UIColor grayColor]];
 [lbl setNumberOfLines:0];

 //an NSDictionary of NSString => UIColor pairs
 NSDictionary * wordToColorMapping = @{ @"Hello World, " :[UIColor redColor],
           @"Programming In " :[UIColor greenColor],
           @"Objective-C"  :[UIColor blueColor]};
 
 NSMutableAttributedString * string = [[NSMutableAttributedString alloc] initWithString:@""];
 for (NSString * word in wordToColorMapping) {
  UIColor * color = wordToColorMapping[word];
  NSDictionary * attributes = @{NSForegroundColorAttributeName: color};
  
  NSAttributedString * subString = [[NSAttributedString alloc] initWithString:word attributes:attributes];
  [string appendAttributedString:subString];
 }
 
 [lbl setAttributedText:string];
 [self.view addSubview:lbl];


或許也可以用兩層的Dict, 第一層的Key值當作要顯示的String, 然後Value值是Attribute的屬性Dict


回答別人問題多學會一樣東西, 上下標, 需要CoreText Framework
 UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(10 , 10, 300, 300)];
 [lbl setFont:[UIFont boldSystemFontOfSize:30.0]];
 [lbl setTextAlignment:NSTextAlignmentCenter];
 
 NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"H2O"];
 NSRange range = NSMakeRange(1, 1);
 UIFont *smallFont = [UIFont boldSystemFontOfSize:20.0];
 
 NSDictionary *attr = @{(NSString*)kCTSuperscriptAttributeName : @(-1),
      (NSString*)kCTFontAttributeName : smallFont,
      NSForegroundColorAttributeName : [UIColor redColor]};
 
 [string addAttributes:attr range:range];
 lbl.attributedText = string;
 
 [self.view addSubview:lbl];

沒有留言:

張貼留言