美文网首页
Label字间距行间距

Label字间距行间距

作者: 温水煮青蛙a | 来源:发表于2018-01-19 17:42 被阅读0次
    UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(0, 100, 100, 500)];
        [self.view addSubview:label2];
        
        NSString *labelText = @"哈哈\nqwer\n!!!aaaaaaaaaaaaaaaaaaaaaa";
        label2.numberOfLines = 0; // 最关键的一句
        NSMutableDictionary *attDic = [NSMutableDictionary dictionary];
        // 字体大小
        [attDic setValue:[UIFont systemFontOfSize:15] forKey:NSFontAttributeName];
        // 字体颜色
        [attDic setValue:[UIColor redColor] forKey:NSForegroundColorAttributeName];
        // 字间距
        [attDic setValue:@5 forKey:NSKernAttributeName];
        // 设置字体背景色
        [attDic setValue:[UIColor cyanColor] forKey:NSBackgroundColorAttributeName];
        
        NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:labelText attributes:attDic];
        
        // 设置行间距
        NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
        style.lineSpacing = 10;
        [attStr addAttribute:NSParagraphStyleAttributeName value:style range: NSMakeRange(0, labelText.length)];
        
        label2.attributedText = attStr;
    

    相关文章

      网友评论

          本文标题:Label字间距行间距

          本文链接:https://www.haomeiwen.com/subject/rxkaoxtx.html