使用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(, , , )];
_textLabel.textColor = [UIColor whiteColor];
_textLabel.numberOfLines = ; // 设置自动换行
[_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(, , fixWidth, );
_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 = ; // 固定Label宽度为100
hud.gapSize = CGSizeMake(, ); // 间隙宽度为10
hud.cornerRadius = .f; // 圆角值为5 NSString *str = @"Warnning:\n\nYouXianMing NoZuoNoDie";
NSArray *sets = @[
// 全局设置
[ConfigAttributedString font:[UIFont systemFontOfSize:.f]
range:[str range]], // 局部设置
[ConfigAttributedString font:[UIFont fontWithName:@"HelveticaNeue-Thin" size:.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 = ;
rect.origin.y = ;
hud.frame = rect; [self.view addSubview:hud];
} @end

可以固定位置:

可以进行复杂的设置:

可以设置富文本:

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

使用YXHUD的更多相关文章

随机推荐

  1. Visual Studio使用阿里云Code Git服务器的常见问题

    使用Github的服务器太慢,阿里的https://code.aliyun.com的国内服务器还是很快的.但是使用阿里的Git服务器总是有些地方出问题,现记录下常见的问题: 1.如提示源码已在TFS管 ...

  2. TCP连接、Http连接与Socket连接

    1.TCP连接 手机能够使用联网功能是因为手机底层实现了TCP/IP协议,可以使手机终端通过无线网络建立TCP连接.TCP协议可以对上层网络提供接口,使上层网络数据的传输建立在“无差别”的网络之上. ...

  3. HTTP协议以及HTTP请求中8种请求方法

    HTTP协议以及HTTP请求中8种请求方法 什么是协议? 协议,是指通信的双方,在通信流程或内容格式上,共同遵守的标准. 什么是http协议? http协议,是互联网中最常见的网络通信标准. http ...

  4. Node.js中setTimeout和setInterval的使用

    Node.js和js一样也有计时器,超时计时器.间隔计时器.及时计时器,它们以及process.nextTick(callback)函数来实现事件调度.今天先学下setTimeout和setInter ...

  5. Template parse errors: The pipe 'translate' could not be found

    问题描述: 基于Ionic最新的super模板,创建的项目,在自己改造成懒加载机制后,原本正常的项目出现问题了,提示模板内部使用的翻译管道找不到,如图: 模板内部使用的翻译管道代码,我确定没有问题, ...

  6. sql中全文检索CHARINDEX 和PATINDEX 区别

    最近经常使用字符串查找功能. 包括 1.全匹配查找字符串 2.模糊查找字符串 CHARINDEX 和 PATINDEX 函数都返回指定模式的开始位置.PATINDEX 可使用通配符,而 CHARIND ...

  7. PHP数组基本的操作方法

    1.数组操作的基本函数 数组的键和值: array_values($arr);获得数组的值 array_keys($arr);获得数组的键名 array_flip($arr);数组中的值与键名互换(如 ...

  8. Jquery Easy UI初步学习(二)datagrid的使用

    第一篇学的是做一个管理的外框,接着就是数据datagrid绑定了,这里我用asp.net mvc3来做的,主要就是熟悉属性.方法. 打开easyui的demo 就可以看到如下一段代码: 和上篇一样cl ...

  9. [日常] 搭建golang开发环境

    下载目录:https://studygolang.com/dl32位选 go1.10.linux-386.tar.gz64位选 go1.10.linux-amd64.tar.gz uname -a查看 ...

  10. Win10+Ubuntu双系统安装

    笔者使用的是华硕FX50J装的双系统,之前使用过Dell的游闸安装过,但是没有碰到那么多问题.所以觉得有必要记录下.安装双系统之前,电脑先安装了win10(win10也崩过几次).下面开始安装双系统步 ...