UILabel

2018-05-22  本文已影响0人  夜雨聲煩_

label 居上和居下

思路

使用 label 分类增加居上和局下方法。根据 (控件总高 - 内容占高)/ 字体高度,得出剩余高度行数,由空行代替。

代码
//
//  UILabel+VerticalAlign.m
//  UILabel
//
//  Created by chip on 2018/5/22.
//  Copyright © 2018年 chip. All rights reserved.
//

/*
   需指定:label.numberOfLines = 0;
 */

#import <UIKit/UIKit.h>

@interface UILabel (VerticalAlign)

- (void)alignTop;
- (void)alignBottom;

@end

#import "UILabel+VerticalAlign.h"

@implementation UILabel (VerticalAlign)

- (void)alignTop
{
    //计算内容字符串大小
    CGSize stringSize = [self.text sizeWithAttributes:@{NSFontAttributeName: self.font}];
    //label高度
    double labelHeight = self.frame.size.height;
    //label宽度
    double labelWidth = self.frame.size.width;
    //计算文字内容对应label尺寸(多行)
    CGRect rect = [self.text boundingRectWithSize:CGSizeMake(labelWidth, labelHeight) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: self.font} context:nil];
    //需加入多少空行
    int newLinesToPad = (labelHeight  - ceil(rect.size.height)) / stringSize.height;
    for(int i=0; i<newLinesToPad; i++){
       //加入空行
       self.text = [self.text stringByAppendingString:@"\n "];
    }
}

- (void)alignBottom
{
    //计算内容字符串大小
    CGSize stringSize = [self.text sizeWithAttributes:@{NSFontAttributeName: self.font}];
    //label高度
    double labelHeight = self.frame.size.height;
    //label宽度
    double labelWidth = self.frame.size.width;
    //计算文字内容对应label尺寸(多行)
    CGRect rect = [self.text boundingRectWithSize:CGSizeMake(labelWidth, labelHeight) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: self.font} context:nil];
    //需加入多少空行
    int newLinesToPad = (labelHeight  - ceil(rect.size.height)) / stringSize.height;
    for(int i=0; i<newLinesToPad; i++){
        //加入空行
        self.text = [NSString stringWithFormat:@" \n%@",self.text];
    }
}

@end
补充

其他实现方式:

label的图文混排

UILabel分类

-(void)setText:(NSString *)text frontImages:(NSArray<UIImage *> *)images imageSpan:(CGFloat)span imageSizeEqualToFont:(BOOL)equal
{
    NSMutableAttributedString *textAttrStr = [[NSMutableAttributedString alloc] init];
    
    for (UIImage *img in images) {//遍历添加标签
        
        NSTextAttachment *attach = [[NSTextAttachment alloc] init];
        attach.image = img;
        //计算图片大小,与文字同高,按比例设置宽度
        CGFloat imgH;
        CGFloat imgW;
        if (equal) {
            imgH = self.font.pointSize;
            imgW = (img.size.width / img.size.height) * imgH;
        }else{
            imgH = img.size.height;
            imgW = img.size.width;
        }
        /**
         计算文字originY ,使图片垂直居中
         font.ascender为基准线以上高度,即基准线Y值
         font.lineHeight为整个行高,除以二为中线Y值
         以上两者做差减中线下一半图片高度即为图片底居基准线距离
         小于0为基准线一下,大于0为基准线以上。
         **/
        CGFloat originY = self.font.ascender - self.font.lineHeight/2 - imgH/2;
        //bounds为基于基线的为原点坐标位置
        attach.bounds = CGRectMake(0, originY, imgW, imgH);
        
        NSAttributedString *imgStr = [NSAttributedString attributedStringWithAttachment:attach];
        [textAttrStr appendAttributedString:imgStr];
        //标签后添加空格
        [textAttrStr appendAttributedString:[[NSAttributedString alloc] initWithString:@" "]];
    }
    
    //设置显示文本
    [textAttrStr appendAttributedString:[[NSAttributedString alloc]initWithString:text]];
    //设置间距
    if (span != 0) {
        [textAttrStr addAttribute:NSKernAttributeName value:@(span)
                            range:NSMakeRange(0, images.count * 2/*由于图片也会占用一个单位长度,所以带上空格数量,需要 *2 */)];
    }
    
    self.attributedText = textAttrStr;
}

Label行间距设置

// 设置属性
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
// 设置行间距
paragraphStyle.lineSpacing = 5;
paragraphStyle.lineBreakMode = NSLineBreakByClipping;
NSDictionary *attributes = @{
    NSFontAttributeName:[UIFont systemFontOfSize:14],
    NSParagraphStyleAttributeName:paragraphStyle
};
self.textview.attributedText = [[NSAttributedString alloc] initWithString:self.str_msg attributes:attributes];

Label的size自适应方法

sizeToFit
sizeThatFits:CGSizeMake(self.contentview.width, MAXFLOAT)]
sizeWithAttributes
[self.text boundingRectWithSize:CGSizeMake(labelWidth, labelHeight)
上一篇下一篇

猜你喜欢

热点阅读