label添加字符间距

label添加字符间距_第1张图片/**
*  添加字符间距
*
*  @param maxInteger    最大字符长度
*  @param currentString 当前显示字符
*  @param label         显示label
*/
- (void)conversionCharacterInterval:(NSInteger)maxInteger current:(NSString *)currentString withLabel:(UILabel *)label
{
    CGRect rect = [@"联" boundingRectWithSize:CGSizeMake(label.frame.size.width,label.frame.size.height)
                                     options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading
                                  attributes:@{NSFontAttributeName: label.font}
                                     context:nil];
    
    
    NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:currentString];
    [attrString addAttribute:NSKernAttributeName value:@(((maxInteger - currentString.length) * rect.size.width)/(currentString.length - 1)) range:NSMakeRange(0, currentString.length)];
    label.attributedText = attrString;

你可能感兴趣的:(label添加字符间距)