使用YXHUD

简介:

使用YXHUD

这是本人自己设计的一个类,但功能很不完善,先看看效果:

源码:

YXHUD.h 与 YXHUD.m

//
//  YXHUD.h
//  UILabel
//
//  Created by YouXianMing on 14-9-16.
//  Copyright (c) 2014年 YouXianMing. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface YXHUD : UIView

@property (nonatomic, strong) NSString            *showString;           // 普通文本
@property (nonatomic, strong) NSAttributedString  *showAttributedString; // 富文本
@property (nonatomic, assign) CGFloat              fixWidth;             // 固定的宽度
@property (nonatomic, assign) CGSize               gapSize;              // 间隙宽度
@property (nonatomic, assign) CGFloat              cornerRadius;         // 圆角

- (void)fix;

@end


//
//  YXHUD.m
//  UILabel
//
//  Created by YouXianMing on 14-9-16.
//  Copyright (c) 2014年 YouXianMing. All rights reserved.
//

#import "YXHUD.h"

@interface YXHUD ()

@property (nonatomic, strong) UILabel  *textLabel;
@property (nonatomic, strong) UIView   *backedView;

@end

@implementation YXHUD

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        // backedView(背景view)
        _backedView = [UIView new];
        _backedView.backgroundColor = [UIColor blackColor];
        [self addSubview:_backedView];
     
        // textLabel(文字label)
        _textLabel               = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 0)];
        _textLabel.textColor     = [UIColor whiteColor];
        _textLabel.numberOfLines = 0; // 设置自动换行
        [_backedView addSubview:_textLabel];
    }
    return self;
}

#pragma mark - getter.setter方法

// 设置普通文本
@synthesize showString = _showString;
- (void)setShowString:(NSString *)showString
{
    _showString     = showString;
    _textLabel.text = showString;
}
- (NSString *)showString
{
    return _showString;
}

// 设置富文本
@synthesize showAttributedString = _showAttributedString;
- (void)setShowAttributedString:(NSAttributedString *)showAttributedString
{
    _showAttributedString     = showAttributedString;
    _textLabel.attributedText = showAttributedString;
}
- (NSAttributedString *)showAttributedString
{
    return _showAttributedString;
}

// 固定宽度
@synthesize fixWidth = _fixWidth;
- (void)setFixWidth:(CGFloat)fixWidth
{
    _textLabel.frame = CGRectMake(0, 0, fixWidth, 0);
    _fixWidth        = fixWidth;
}
- (CGFloat)fixWidth
{
    return _fixWidth;
}

// backedView的圆角
@synthesize cornerRadius = _cornerRadius;
- (void)setCornerRadius:(CGFloat)cornerRadius
{
    _cornerRadius = cornerRadius;
    _backedView.layer.cornerRadius = cornerRadius;
}
- (CGFloat)cornerRadius
{
    return _cornerRadius;
}

#pragma mark - 其他方法

// 计算出文本尺寸
- (CGRect)calculate
{
    [_textLabel sizeToFit];
    return _textLabel.bounds;
}

// 最终计算出尺寸
- (void)fix
{
    // 计算出文本的rect值
    CGRect rect       = [self calculate];
    rect.size.height += _gapSize.height;
    rect.size.width  += _gapSize.width;
    _backedView.frame = rect;
    _textLabel.center = _backedView.center;
    
    self.frame        = rect;
}

@end

使用时候的代码:(注: 里面用到的富文本为本人自己所写的类
//
//  RootViewController.m
//  UILabel
//
//  Created by YouXianMing on 14-9-16.
//  Copyright (c) 2014年 YouXianMing. All rights reserved.
//

#import "RootViewController.h"
#import "YXHUD.h"
#import "ConfigAttributedString.h"
#import "NSString+YX.h"

@interface RootViewController ()

@end

@implementation RootViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    

    YXHUD *hud = [YXHUD new];
    hud.fixWidth     = 200;                // 固定Label宽度为100
    hud.gapSize      = CGSizeMake(20, 20); // 间隙宽度为10
    hud.cornerRadius = 5.f;                // 圆角值为5
    
    
    NSString *str = @"Warnning:\n\nYouXianMing NoZuoNoDie";
    NSArray *sets = @[
                      // 全局设置
                      [ConfigAttributedString font:[UIFont systemFontOfSize:10.f]
                                             range:[str range]],
                      
                      // 局部设置
                      [ConfigAttributedString font:[UIFont fontWithName:@"HelveticaNeue-Thin" size:14.f]
                                             range:[@"Warnning:" rangeFrom:str]],
                      [ConfigAttributedString foregroundColor:[UIColor yellowColor]
                                                        range:[@"Warnning:" rangeFrom:str]],
                      [ConfigAttributedString foregroundColor:[UIColor redColor]
                                                        range:[@"YouXianMing" rangeFrom:str]]];
    
    
    NSLog(@"%@", [str createAttributedStringAndConfig:sets]);
    hud.showAttributedString = [str createAttributedStringAndConfig:sets];
    [hud fix];
    
    CGRect rect = hud.frame;
    rect.origin.x = 100;
    rect.origin.y = 100;
    hud.frame = rect;
    
    
    [self.view addSubview:hud];
}

@end

可以固定位置:

可以进行复杂的设置:

可以设置富文本:

不过还不是完全品......

目录
相关文章
|
8天前
|
人工智能 自然语言处理 API
深入浅出LangChain与智能Agent:构建下一代AI助手
LangChain为大型语言模型提供了一种全新的搭建和集成方式,通过这个强大的框架,我们可以将复杂的技术任务简化,让创意和创新更加易于实现。本文从LangChain是什么到LangChain的实际案例到智能体的快速发展做了全面的讲解。
279539 52
深入浅出LangChain与智能Agent:构建下一代AI助手
|
9天前
|
设计模式 人工智能 JSON
一文掌握大模型提示词技巧:从战略到战术
本文将用通俗易懂的语言,带你从战略(宏观)和战术(微观)两个层次掌握大模型提示词的常见技巧,真正做到理论和实践相结合,占领 AI 运用的先机。
237783 4
|
9天前
|
NoSQL Cloud Native Redis
Redis核心开发者的新征程:阿里云与Valkey社区的技术融合与创新
阿里云瑶池数据库团队后续将持续参与Valkey社区,如过往在Redis社区一样耕耘,为开源社区作出持续贡献。
Redis核心开发者的新征程:阿里云与Valkey社区的技术融合与创新
|
9天前
|
关系型数据库 分布式数据库 数据库
PolarDB闪电助攻,《香肠派对》百亿好友关系实现毫秒级查询
PolarDB分布式版助力《香肠派对》实现百亿好友关系20万QPS的毫秒级查询。
PolarDB闪电助攻,《香肠派对》百亿好友关系实现毫秒级查询
|
2天前
|
机器人 Linux API
基于Ollama+AnythingLLM轻松打造本地大模型知识库
Ollama是开源工具,简化了在本地运行大型语言模型(ile优化模型运行,支持GPU使用和热加载。它轻量、易用,可在Mac和Linux上通过Docker快速部署。AnythingLLM是Mintplex Labs的文档聊天机器人,支持多用户、多种文档格式,提供对话和查询模式,内置向量数据库,可高效管理大模型和文档。它也是开源的,能与Ollama结合使用,提供安全、低成本的LLM体验。这两款工具旨在促进本地高效利用和管理LLMs。
70248 19
|
10天前
|
消息中间件 Cloud Native Serverless
RocketMQ 事件驱动:云时代的事件驱动有啥不同?
本文深入探讨了云时代 EDA 的新内涵及它在云时代再次流行的主要驱动力,包括技术驱动力和商业驱动力,随后重点介绍了 RocketMQ 5.0 推出的子产品 EventBridge,并通过几个云时代事件驱动的典型案例,进一步叙述了云时代事件驱动的常见场景和最佳实践。
246778 2
|
7天前
|
物联网 PyTorch 测试技术
手把手教你捏一个自己的Agent
Modelscope AgentFabric是一个基于ModelScope-Agent的交互式智能体应用,用于方便地创建针对各种现实应用量身定制智能体,目前已经在生产级别落地。
|
11天前
|
弹性计算 安全 API
访问控制(RAM)|云上安全使用AccessKey的最佳实践
集中管控AK/SK的生命周期,可以极大降低AK/SK管理和使用成本,同时通过加密和轮转的方式,保证AK/SK的安全使用,本次分享为您介绍产品原理,以及具体的使用步骤。
101885 3
|
10天前
|
自然语言处理 Cloud Native Serverless
通义灵码牵手阿里云函数计算 FC ,打造智能编码新体验
近日,通义灵码正式进驻函数计算 FC WebIDE,让使用函数计算产品的开发者在其熟悉的云端集成开发环境中,无需再次登录即可使用通义灵码的智能编程能力,实现开发效率与代码质量的双重提升。
95464 4
|
2天前
|
人工智能 自然语言处理 API
Claude3是什么?
Claude 3最近备受各大媒体瞩目,成为了AI领域备受关注的新宠。在ChatGPT推出更高版本之前,Claude 3已经被公认为是语言类AI工具中的佼佼者,特别在处理逻辑性和长篇上下文方面表现突出。然而,与此同时,Claude 3的注册流程也备受诟病,被认为是所有AI工具中最为复杂的之一。 这篇内容教大家 注册Claude 3 以及升级 教程。
13682 1
Claude3是什么?