zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    自动化流程完成打包 IPA 到 上传 AppStore(部分)
    dex2jar jd_jui 反编译apk
    mac 系统常用小工具
    从 Jira page 上获取信息和下载附件
    python zip文件处理 之 zipfile模块
    showDoc 自动创建文档分析
    Unable to install ‘*****’
    自动化流程完成 打包 IPA 到 上传 AppStore 之 iOS IPA签名
    灰度图片和灰度颜色(代码里面只是一些相关的方法替换按需选取几个就好)
    Mac开发一些好的软件
  • 原文地址:https://www.cnblogs.com/yuwei0911/p/6037790.html
Copyright © 2011-2022 走看看