使用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(, , , )];
_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的更多相关文章
随机推荐
- java学习-sha1散列算法
直接调用HashKit.sha1(String str)方法就可以了,,返回的是16进制的字符串长度是40, 也就是用md.digest()方法解析出来的字节数是160字节长度. 而MD5散列算法生成 ...
- 数据分析--降维--LDA和PCA
一.因子分析 因子分析是将具有错综复杂关系的变量(或样本)综合为少数几个因子,以再现原始变量和因子之间的相互关系,探讨多个能够直接测量,并且具有一定相关性的实测指标是如何受少数几个内在的独立因子所支配 ...
- Boosting和Bagging的异同
二者都是集成学习算法,都是将多个弱学习器组合成强学习器的方法. 1.Bagging (主要关注降低方差) Bagging即套袋法,其算法过程如下: A)从原始样本集中抽取训练集.每轮从原始样本集中使用 ...
- 笔记三:python乱码深度剖析一
一:学习内容 python编码转换 python乱码原因深入解析 二:python编码转换 1. Python内部字符串一般都是Unicode编码,代码中字符串的默认编码与代码文件本身的编码是一致的. ...
- Linux下C语言的调试--转
调试是每个程序员都会面临的问题. 如何提高程序员的调试效率, 更好更快地定位程序中的问题从而加快程序开发的进度, 是大家共同面对的问题. 可能Windows用户顺口就会说出:用VC呗 :-) , 它提 ...
- asdfasdfasdfasdf
- VS2013 项目项目安装和部署
版权声明:本文为博主原创文章,未经博主允许不得转载. 1.release 模式下生成项目 2.解决方案 右键 添加 新建项目 其他项目类型 安装和部署 3.操作前将待打包项目发布路径指向上述 ...
- mongodb int型id 自增
mongo的c#客户端提供了接口IIdGenerator,有guid和objectid等几种实现,但没有int型id的实现 接口主要2个方法,一个IsEmpty返回bool,判断当前id值是否是空(估 ...
- 解决部分小程序无法获取UnionId的问题
问题背景 通过观察数据,发现有一部分用户是无法获取到UnionId的 也就是接口返回的参数中不包含UnionId参数 看了微信文档的解释,只要小程序在开放平台绑定,就一定会分配UnionId 网上也有 ...
- hdu Rescue 1242
Rescue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...