• UILabel 行间距


    label自适应和字体行间距字间距的设置

     

    #import <Foundation/Foundation.h>

    @interface LabelLayout : NSObject

    /// label设置行间距 参数1:内容 参数2:label 参数3:行间距 参数4:字间距 参数5:字大小 参数6:label的宽度

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

    /// 设置不同的字体大小 参数1:开始位置 参数2:结束位置 参数3:设置的内容

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

    ///设置button宽度自适应 参数1:字体大小 参数2:要显示的内容

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

    ///设置label宽度自适应 参数1:要显示的内容 参数2:字体大小

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

    /// 设置不同的字体颜色 参数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

  • 相关阅读:
    C#屏幕截图
    WPF转换器用法示例
    WPF自定义数字输入框控件
    LINQ 用法,返回结果不是在定义时取值,而是在调用时实时取值,有意思!
    类泛型--必须继承接口
    wpf在异步中给前台赋值
    VS Code中Matlab插件安装设置
    Python 自动给数字前面补0
    Tensorflow (1)
    解决Keras在IDE集成环境中找不到nvcc
  • 原文地址:https://www.cnblogs.com/yuwei0911/p/6037790.html
Copyright © 2020-2023  润新知