使用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. eclipse中explorer显示方式

    不知道是不是上面的描述.做个记录 project explorer 项目资源管理器 这个要打开代码目录需要再点开java resources 还会出现deployment Descriptor项目工程 ...

  2. 数据库--oracle安装配置(本地安装的步骤及各种问题解决方案)

    oracle版本:Oracle 11g 本地电脑配置:安装内存8G 64为操作系统win8.1 下载Oracle 11g压缩包: 1 网址http://www.oracle.com/technetwo ...

  3. vue 分享知识点

    vue 分享模块清单 1.Vue 2.0之Vue实例和生命周期 2.vue 2.0之自定义指令 3.vue 2.0之观察者模式实现简单异步无限滚动 4.从JavaScript属性描述器剖析Vue.js ...

  4. 虚拟化明星——深挖轻量级容器docker

    docker是一个轻量级容器,属于操作系统层面的虚拟化技术,封装了文件系统(AUFS)以及网络互联,进程隔离等特性. 传统虚拟化架构: docker虚拟化架构: 可以看出,docker是没有Guest ...

  5. [C++][转]CPU字节序 网络序 主机序 大端小端

    原帖:http://www.cnblogs.com/darktime/p/3298075.html 不同的CPU有不同的字节序类型 这些字节序是指整数在内存中保存的顺序 这个叫做主机序最常见的有两种1 ...

  6. Spring的@Scheduled任务调度

    一. 定时任务实现方式 定时任务实现方式: Java自带的java.util.Timer类,这个类允许你调度一个java.util.TimerTask任务.使用这种方式可以让你的程序按照某一个频度执行 ...

  7. iOS字体打印

    //打印所以字体    NSArray *familyNames = [UIFont familyNames];    for(NSString *familyName in familyNames) ...

  8. ef和mysql使用(二)--让mysql支持EntityFramework.Extended实现批量更新和删除

    我们都知道Entity Framework 中不能同时更新多条记录,但是一个老外写的扩展库可以实现此功能EntityFramework.Extended,但是如何是mysql数据库要怎么实现呢 首先实 ...

  9. HwUI下载地址

    下载地址:HwUI.0.0.1.zip

  10. mootools vs jquery

    大部分最近才剛接觸JavaScript的人會面臨到的困難是該選擇哪個套件(library)或是該先學哪個套件.如果你在一間公司裡上班,那麼可能公司已經有一套固定使用的套件,若是在這種情況下,問題就沒那 ...