iOS中富文本的实现

label中存在attributedText属性

  1. 设置整体的富文本
    UILabel *label = [[UILabel alloc]init];
    NSString *string = @"这是一段话,用于实现label的富文本";
    NSMutableAttributedString *attribute = [[NSMutableAttributedString alloc]initWithString:string];
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[NSFontAttributeName] = [UIFont fontWithName:@"Zapfino" size:10];
    dict[NSForegroundColorAttributeName] = [UIColor redColor];
    NSRange range = NSMakeRange(2, 2);
    [attribute setAttributes:dict range:range];
    label.attributedText = attribute;
    label.numberOfLines = 0;
    label.frame = CGRectMake(100, 100, 200, 100);
    [self.view addSubview:label];

2.分开设置

    UILabel *label = [[UILabel alloc]init];
    NSString *string = @"这是一段话,用于实现label的富文本";
    NSMutableAttributedString *attribute = [[NSMutableAttributedString alloc]initWithString:string];
    NSRange range = NSMakeRange(2, 2);
    [attribute addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Zapfino" size:10] range:range];
    [attribute addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:range];
    label.attributedText = attribute;
    label.numberOfLines = 0;
    label.frame = CGRectMake(100, 100, 200, 100);
    [self.view addSubview:label];

你可能感兴趣的:(iOS中富文本的实现)