iOS 在UILabel显示不同的字体和颜色&&## 截取字符串

iOS 在UILabel显示不同的字体和颜色

  • self.title = @"For iOS 6 & later";
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"Using NSAttributed String"];
    [str addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0,5)];
    [str addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(6,12)];
    [str addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(19,6)];
    [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Arial-BoldItalicMT" size:30.0] range:NSMakeRange(0, 5)];
    [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue-Bold" size:30.0] range:NSMakeRange(6, 12)];
    [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Courier-BoldOblique" size:30.0] range:NSMakeRange(19, 6)];
    attrLabel.attributedText = str;

截取字符串

  • 定义一个字符串a, 截取a 的某一个部分,复制给b, b必须是int型
    NSString *a = @"1.2.30";
    int b= [[a substringWithRange:NSMakeRange(4,2)] intValue];
    NSLog(@"a:%@ \n",a );
    NSLog(@"b:%d",b );

Output : 2011-07-05 11:49:08.170 Q[4005:207] a:1.2.30
2011-07-05 11:49:08.172 Q[4005:207] b:30

  • 字符串截取到第n位 (substringToIndex: n)(第n 位不算再内)

-(void)viewDidLoad
{
NSString *a = @"i like long dress";
NSString *b = [a substringToIndex:4];
NSLog(@"\n b: %@",b);
}

2011-07-11 18:12:40.119 Q[6321:207]
b: i li

  • 字符串从第n 位开始截取,直到最后 (substringFromIndex:n)(包括第 n 位)

-(void)viewDidLoad
{
NSString *a = @"i like long dress";
NSString *b = [a substringFromIndex:4];
NSLog(@"\n b: %@",b);
}

2011-07-11 18:15:08.125 Q[6366:207]
b: ke long dress

你可能感兴趣的:(iOS 在UILabel显示不同的字体和颜色&&## 截取字符串)