给label添加不同颜色不同字体的内容


label添加不同颜色不同字体的内容


NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"ociOS编程的语言"];

    [str addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0,2)];

    [str addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(2,3)];

    [str addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(3,8)];

    [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Arial Hebrew" size:30.0] range:NSMakeRange(0, 2)];

    [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Helvetica Neue" size:30.0] range:NSMakeRange(2, 3)];

    [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Kohinoor Devanagari" size:30.0] range:NSMakeRange(3, 8)];

   

    NSArray *names = [UIFont familyNames];

    

    NSLog(@"names :%@",names);

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 300, 300, 150)];

    label.backgroundColor = [UIColor whiteColor];

    label.attributedText = str;

    label.numberOfLines = 0;

    [self.view addSubview:label];

给label添加不同颜色不同字体的内容_第1张图片


你可能感兴趣的:(iOS,布局)