[控件] LabelView

简介:

LabelView

此LabelView是用来将Label显示在固定的View上的,需要计算Label的高度与宽度.

源码:

NSString+StringHeight.h 与 NSString+StringHeight.m

//
//  NSString+StringHeight.h
//  USA
//
//  Created by YouXianMing on 14/12/10.
//  Copyright (c) 2014年 fuhuaqi. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface NSString (StringHeight)

/**
 *  计算文本的高度
 *
 *  @param font  字体
 *  @param width 固定的宽度
 *
 *  @return 高度
 */
- (CGFloat)heightWithLabelFont:(UIFont *)font withLabelWidth:(CGFloat)width;

/**
 *  计算文本的宽度
 *
 *  @param font 字体
 *
 *  @return 宽度
 */
- (CGFloat)widthWithLabelFont:(UIFont *)font;

@end


//
//  NSString+StringHeight.m
//  USA
//
//  Created by YouXianMing on 14/12/10.
//  Copyright (c) 2014年 fuhuaqi. All rights reserved.
//

#import "NSString+StringHeight.h"

@implementation NSString (StringHeight)

- (CGFloat)heightWithLabelFont:(UIFont *)font withLabelWidth:(CGFloat)width {
    CGFloat height = 0;
    
    if (self.length == 0) {
        height = 0;
    } else {

        // 字体
        NSDictionary *attribute = @{NSFontAttributeName: [UIFont systemFontOfSize:18.f]};
        if (font) {
            attribute = @{NSFontAttributeName: font};
        }
        
        // 尺寸
        CGSize retSize = [self boundingRectWithSize:CGSizeMake(width, MAXFLOAT)
                                            options:
                          NSStringDrawingTruncatesLastVisibleLine |
                          NSStringDrawingUsesLineFragmentOrigin |
                          NSStringDrawingUsesFontLeading
                                         attributes:attribute
                                            context:nil].size;
        
        height = retSize.height;
    }
    
    return height;
}

- (CGFloat)widthWithLabelFont:(UIFont *)font {
    CGFloat retHeight = 0;
    
    if (self.length == 0) {
        retHeight = 0;
    } else {
        
        // 字体
        NSDictionary *attribute = @{NSFontAttributeName: [UIFont systemFontOfSize:18.f]};
        if (font) {
            attribute = @{NSFontAttributeName: font};
        }
        
        // 尺寸
        CGSize retSize = [self boundingRectWithSize:CGSizeMake(MAXFLOAT, 0)
                                            options:
                          NSStringDrawingTruncatesLastVisibleLine |
                          NSStringDrawingUsesLineFragmentOrigin |
                          NSStringDrawingUsesFontLeading
                                         attributes:attribute
                                            context:nil].size;
        
        retHeight = retSize.width;
    }
    
    return retHeight;
}

@end

LabelView.h 与  LabelView.m
//
//  LabelView.h
//  YXMWeather
//
//  Created by XianMingYou on 15/2/16.
//  Copyright (c) 2015年 XianMingYou. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "NSString+StringHeight.h"

@interface LabelView : UIView

/**
 *  文本
 */
@property (nonatomic, strong) NSString  *text;

/**
 *  文本颜色
 */
@property (nonatomic, strong) UIColor   *textColor;

/**
 *  文本字体
 */
@property (nonatomic, strong) UIFont    *font;

/**
 *  背景色
 */
@property (nonatomic, strong) UIColor   *color;

/**
 *  距离顶部的距离
 */
@property (nonatomic) CGFloat gapFromTop;

/**
 *  距离底部的距离
 */
@property (nonatomic) CGFloat gapFromBottom;

/**
 *  距离左侧的距离
 */
@property (nonatomic) CGFloat gapFromLeft;

/**
 *  距离右侧的距离
 */
@property (nonatomic) CGFloat gapFromRight;

/**
 *  创建出view
 */
- (void)buildView;

/**
 *  创建出默认配置的label
 *
 *  @param text   字符串
 *  @param origin 起始位置
 *
 *  @return 实例对象
 */
+ (instancetype)createWithText:(NSString *)text atOrigin:(CGPoint)origin;

@end


//
//  LabelView.m
//  YXMWeather
//
//  Created by XianMingYou on 15/2/16.
//  Copyright (c) 2015年 XianMingYou. All rights reserved.
//

#import "LabelView.h"

@interface LabelView ()

@property (nonatomic) CGFloat          labelWidth;
@property (nonatomic) CGFloat          labelHeight;

@property (nonatomic, strong) UILabel *label;

@end

@implementation LabelView

- (void)buildView {
    // 设置label
    self.label.text      = self.text;
    self.label.font      = self.font;
    self.label.textColor = self.textColor;
    
    // 获取宽度
    self.labelWidth   = [self.text widthWithLabelFont:self.font];
    self.labelHeight  = [self.text heightWithLabelFont:self.font withLabelWidth:MAXFLOAT];
    self.label.width  = self.labelWidth;
    self.label.height = self.labelHeight;

    // 计算间距
    self.label.x = self.gapFromLeft;
    self.label.y = self.gapFromTop;
    
    // 重新设置尺寸
    self.width  = self.labelWidth + self.gapFromLeft + self.gapFromRight;
    self.height = self.labelHeight + self.gapFromTop + self.gapFromBottom;
    
    // 设置背景色
    if (self.color) {
        self.backgroundColor = self.color;
    }
}

@synthesize label = _label;
- (UILabel *)label {
    if (_label == nil) {
        _label = [[UILabel alloc] initWithFrame:CGRectZero];
        _label.textAlignment = NSTextAlignmentCenter;
        [self addSubview:_label];
    }
    
    return _label;
}

+ (instancetype)createWithText:(NSString *)text atOrigin:(CGPoint)origin {
    LabelView *labelView    = [[LabelView alloc] initWithFrame:CGRectMake(origin.x, origin.y, 0, 0)];
    labelView.color         = [UIColor blackColor];
    labelView.text          = text;
    labelView.textColor     = [UIColor whiteColor];
    labelView.font          = [UIFont fontWithName:LATO_BOLD size:8];
    labelView.gapFromLeft   = 10.f;
    labelView.gapFromRight  = 10.f;
    labelView.gapFromTop    = 2.f;
    labelView.gapFromBottom = 2.f;
    
    [labelView buildView];

    return labelView;
}

@end

使用时候的源码:

    LabelView *labelView = [LabelView createWithText:@"YouXianMing" atOrigin:CGPointMake(10, 90)];

    [self.view addSubview:labelView];

 

目录
相关文章
|
9月前
|
机器学习/深度学习 人工智能 算法
【AI系统】模型压缩基本介绍
模型压缩旨在通过减少存储空间、降低计算量和提高计算效率,降低模型部署成本,同时保持模型性能。主要技术包括模型量化、参数剪枝、知识蒸馏和低秩分解,广泛应用于移动设备、物联网、在线服务系统、大模型及自动驾驶等领域。
522 4
【AI系统】模型压缩基本介绍
|
搜索推荐 数据管理
5分钟快速定制企业年度报告
钉钉数据资产平台致力于打造数据消费的新方式,助力企业在数据驱动的时代中脱颖而出。
|
容灾 安全 云计算
《云上容灾交付服务白皮书》——4.交付标准化评估要素
《云上容灾交付服务白皮书》——4.交付标准化评估要素
175 0
|
11天前
|
弹性计算 关系型数据库 微服务
基于 Docker 与 Kubernetes(K3s)的微服务:阿里云生产环境扩容实践
在微服务架构中,如何实现“稳定扩容”与“成本可控”是企业面临的核心挑战。本文结合 Python FastAPI 微服务实战,详解如何基于阿里云基础设施,利用 Docker 封装服务、K3s 实现容器编排,构建生产级微服务架构。内容涵盖容器构建、集群部署、自动扩缩容、可观测性等关键环节,适配阿里云资源特性与服务生态,助力企业打造低成本、高可靠、易扩展的微服务解决方案。
1230 5
|
10天前
|
机器学习/深度学习 人工智能 前端开发
通义DeepResearch全面开源!同步分享可落地的高阶Agent构建方法论
通义研究团队开源发布通义 DeepResearch —— 首个在性能上可与 OpenAI DeepResearch 相媲美、并在多项权威基准测试中取得领先表现的全开源 Web Agent。
1213 87
|
10天前
|
云栖大会
阿里云云栖大会2025年9月24日开启,免费申请大会门票,速度领取~
2025云栖大会将于9月24-26日举行,官网免费预约畅享票,审核后短信通知,持证件入场
1796 13
|
20天前
|
人工智能 运维 安全