使用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. freeSWITCH之安装

    freeSWITCH 安装 官网教程 https://freeswitch.org/confluence/display/FREESWITCH/FreeSWITCH+First+Steps Windo ...

  2. redis 安装 与错误解决办法

    redis 安装与安装中遇到的错误 redis 安装 wget http://download.redis.io/releases/redis-4.0.11.tar.gz .tar.gz cd red ...

  3. C#的TextBox获取行高

    当TextBox使用多行之后,如果想获取每行的高度,似乎有点问题, TextBox.Height获取的是控件的高度, 而我们常做的是根据行的数量来决定是否要显示滚动条 如下: //不能直接获取每行的高 ...

  4. mongodb自学

    http://www.runoob.com/mongodb/mongodb-databases-documents-collections.html

  5. sql中替换换行符和空格的示例

    select DiscussID,L.Name as LocationName , C.Name as ClientName, REPLACE(BrandName,' ','') BrandName ...

  6. iOS字体打印

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

  7. 【转】JS前台加密,java后台解密实现

    因项目需求,需要一些敏感信息进行加密,不能以明文暴露到浏览器. 然后后台进行解密操作 先看一下效果图 未对其加密传输 1.前台JS <script type="text/javascr ...

  8. python 软件管理规范

    一.背景 软件开发是一个系统工程,当然编码实现是其中尤其重要的一个环节,关乎到功能需求的实现好坏.这个环节中除了编码这一硬功之外,与之相关的编码风格这一柔道,虽然没有直接决定功能的实现与否,但却在很大 ...

  9. 一文告诉你git如何使用

    提供简易教程学习网址 http://www.bootcss.com/p/git-guide/ git add . //提交至缓存 git commit -m '注释' //提交至本地 (git com ...

  10. Laravel 多域名共享session

    在网站开发中会涉及登陆的问题,在登陆的过程中为了方便用户体验,我们需要用户在主域名登陆,在其他域名下也要保持登陆状态: 在config/session.php中: 更新网站配置缓存即可