UILabel 行間距 label自適應和字體行間距字間距的設置

label自適應和字體行間距字間距的設置

 

#import <Foundation/Foundation.h>html

 

@interface LabelLayout : NSObjectapp

/// label設置行間距 參數1:內容 參數2:label 參數3:行間距 參數4:字間距 參數5:字大小 參數6:label的寬度post

+(CGSize)AdaptiveLabelText:(NSString *)str andLabel:(UILabel *)label andLineSpac:(CGFloat )lineSoac andFontSpac:(NSNumber *)fontSpac andFontSize:(CGFloat )fontSize andWidth:(CGFloat )labelWidth;字體

/// 設置不一樣的字體大小 參數1:開始位置 參數2:結束位置 參數3:設置的內容url

+(NSAttributedString *)setDifferFontSzie2:(NSInteger )len1 andEndIndex:(NSInteger )len2 andString:(NSString *)str andFontSize:(float )size;spa

///設置button寬度自適應 參數1:字體大小 參數2:要顯示的內容htm

+(CGFloat)buttonWidth:(CGFloat )fontSize andStr:(NSString *)str;blog

 

///設置label寬度自適應 參數1:要顯示的內容 參數2:字體大小ci

 

+(CGFloat)LabelWithStr:(NSString *)str andFont:(CGFloat )foutSize;get

/// 設置不一樣的字體顏色 參數1:開始位置 參數2:結束位置 參數3:設置的內容 參數4:設置的顏色

+(NSAttributedString *)setDifferFontColor:(NSInteger )len1 andEndIndex:(NSInteger )len2 andString:(NSString *)str andColor:(UIColor *)color;

@end

 

 

 

 

//

//  LabelLayout.m

//  VaolonUser

//

//  Created by sky on 16/4/9.

//  Copyright © 2016年 FanLang. All rights reserved.

//      關於label一些屬性的公共方法

 

#import "LabelLayout.h"

#import "WidthHeight.pch"

@implementation LabelLayout

 

+(CGSize)AdaptiveLabelText:(NSString *)str andLabel:(UILabel *)label andLineSpac:(CGFloat )lineSoac andFontSpac:(NSNumber *)fontSpac andFontSize:(CGFloat )fontSize andWidth:(CGFloat )labelWidth{

        label.numberOfLines=0;

        NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];

        paraStyle.lineBreakMode = NSLineBreakByWordWrapping;

        paraStyle.alignment = NSTextAlignmentNatural;

        paraStyle.lineSpacing = lineSoac;//設置行間距

        paraStyle.hyphenationFactor = 1.0;

        paraStyle.firstLineHeadIndent = 0.0;

        paraStyle.paragraphSpacingBefore = 0.0;

        paraStyle.headIndent = 0;

        paraStyle.tailIndent = 0;

        NSDictionary *dic = @{NSFontAttributeName:[UIFont fontWithName:@"Arial" size:fontSize*mu], NSParagraphStyleAttributeName:paraStyle, NSKernAttributeName:fontSpac};

    

    NSAttributedString *attributeStr;

    if (str) {

        attributeStr = [[NSAttributedString alloc] initWithString:str attributes:dic];

    }else{

        attributeStr = [[NSAttributedString alloc] initWithString:@"" attributes:dic];

    }

    

        label.attributedText = attributeStr;

        

        return [str boundingRectWithSize:CGSizeMake(labelWidth, 9999999) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil].size;

 

}

 

+(void)setLabel:(UILabel *)label andFout:(CGFloat )foutSize andFoutColor:(UIColor *)color{

    label.font=[UIFont fontWithName:@"Arial" size:foutSize*mu];

    label.textColor=color;

    

}

#pragma mark - 設置不一樣的字體大小

+(NSAttributedString *)setDifferFontSzie:(NSInteger )len1 andEndIndex:(NSInteger )len2 andString:(NSString *)str{

    

    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:str];

    [text addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Arial" size:14*mu] range:NSMakeRange(len1, len2)];

    return text;

}

+(NSAttributedString *)setDifferFontSzie2:(NSInteger )len1 andEndIndex:(NSInteger )len2 andString:(NSString *)str andFontSize:(float )size{

    

    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:str];

    [text addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Arial" size:size*mu] range:NSMakeRange(len1, len2)];

    return text;

}

#pragma mark - 設置不一樣的字體大小

+(NSAttributedString *)setDifferFontColor:(NSInteger )len1 andEndIndex:(NSInteger )len2 andString:(NSString *)str andColor:(UIColor *)color{

    

    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:str];

//    [text addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Arial" size:14*mu] range:NSMakeRange(len1, len2)];

    [text addAttribute:NSForegroundColorAttributeName value:color range:NSMakeRange(len1, len2)];

    return text;

}

 

#pragma mark - button自適應寬度

+(CGFloat)buttonWidth:(CGFloat )fontSize andStr:(NSString *)str{

    NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:fontSize]};

    CGFloat length = [str boundingRectWithSize:CGSizeMake(320, 2000) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size.width+5;

    return length;

}

#pragma mark - label自適應寬度

+(CGFloat)LabelWithStr:(NSString *)str andFont:(CGFloat )foutSize{

 

    CGRect rect = [str boundingRectWithSize:CGSizeMake(0, 10000.0) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:foutSize*mu]} context:nil];

    

    return rect.size.width;

}

@end

相關文章
相關標籤/搜索