UILabel

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 高度。缺点:修改了 label 高度。
  • 可以通过重写 drawRect 方法绘制。缺点:drawRect 方法效率低,尤其在 tableView 中可能会造成卡顿,慎用。

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)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • UILabel是一个常用的控件,它的属性设置的方法在纯代码中经常使用。在storyboard中,使用UILabel...
    坤哥lqk阅读 862评论 0 2
  • 对于UILabel其实我觉得并没有太多的要素需要仔细去弄明白的,因为至今为止,我所了解到的label属性不外乎就是...
    懒惰的习惯阅读 582评论 0 0
  • 常用属性和方法有: 1、创建 CGRect rect = CGRectMake(100,200,50,50);UI...
    西蜀阅读 5,095评论 0 3
  • UILabel垂直居上对齐[label sizeToFit]; //设置文字过长时的显示格式 label.line...
    冬梦日月明阅读 5,256评论 0 0
  • 写于小宝10个月19天 我不知道以什么样的文字作为开头,因为今天,想记录的是分离。 年前小宝夜奶频繁,全家整夜难以...
    陌上花开Air阅读 209评论 0 0