使用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
可以固定位置:
可以进行复杂的设置:
可以设置富文本:
不过还不是完全品......