2019独角兽企业重金招聘Python工程师标准>>>
4.5 BUG 修复 if(_dcAttributedString.length+1 == ms.length){
} //增加这句判断 如果需要请联系我
4.6日 进行重构 代码更少 需要可以QQ联系 完整稳定版1.0完成
使用代码
DCLabel *testLabel = [[DCLabel alloc]initWithFrame:CGRectMake(0, 100, 320, 60)];
testLabel.backgroundColor = [UIColor lightGrayColor];
testLabel.textAlignment = NSTextAlignmentCenter;
testLabel.dcAttributedString = @"123456";
//请务必按顺序使用 // 1:imgSize 2:networkImg||localImg 3:position 4:imgMargin
testLabel.imgSize = CGRectMake(0,0,40,40);
testLabel.networkImg = @"http://desk.fd.zol-img.com.cn/t_s960x600c5/g5/M00/05/02/ChMkJ1bxATiIVlzmAAlJIHaXmLkAAOTUgN32YEACUk4716.jpg"; //支持网络
//testLabel.localImg = //支持本地
testLabel.position = 0;
testLabel.imgMargin = 10;
h文件
//
// DCLabel.h
// DCLabel
//
// Created by point on 16/4/2.
// Copyright © 2016年 赵大财. All rights reserved.
//
#import
@interface DCLabel : UILabel
@property(copy,nonatomic) NSString *networkImg; //需要插入的网络图片
@property(strong,nonatomic) UIImage *localImg; //需要插入的本地图片
@property(assign,nonatomic) NSInteger position; //需要插入的图片位置
@property(assign,nonatomic) CGRect imgSize; //图片的大小
@property(assign,nonatomic) CGFloat imgMargin; //图片的边距
@property(copy,nonatomic) NSString *dcAttributedString; //文字
@end
m文件
//
// DCLabel.m
// DCLabel
//
// Created by point on 16/4/2.
// Copyright © 2016年 赵大财. All rights reserved.
//
#import "DCLabel.h"
@interface DCLabel()
@property(strong,nonatomic) NSTextAttachment *attch;
@property(strong,nonatomic) UIImage *networkImage;
/** 内存缓存的图片 */
@property (nonatomic, strong) NSMutableDictionary *images;
/** 队列对象 */
@property (nonatomic, strong) NSOperationQueue *queue;
@end
@implementation DCLabel
- (void)setDcAttributedString:(NSString *)string {
_dcAttributedString = string;
}
- (void)setLocalImg:(UIImage *)localImg {
[self insertMs:localImg];
}
/**
* 插入图片
*/
- (void)insertMs:(UIImage *)img {
_attch = [[NSTextAttachment alloc] init];
_attch.image = img;
_attch.bounds = _imgSize;
NSMutableAttributedString *ms = [[NSMutableAttributedString alloc]initWithString:_dcAttributedString];
NSAttributedString *string = [NSAttributedString attributedStringWithAttachment:_attch];
[ms insertAttributedString:string atIndex:0];
self.attributedText = ms;
[self setPosition:_position];
[self setImgMargin:_imgMargin]; //重新处理图片的间距
}
- (void)setNetworkImg:(NSString *)networkImg {
UIImage *image = self.images[networkImg];
if (image) {
_networkImage = image;
[self insertMs:image];
} else {
NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
NSString *filename = [networkImg lastPathComponent];
NSString *file = [cachesPath stringByAppendingPathComponent:filename];
NSData *data = [NSData dataWithContentsOfFile:file];
if (data) {
UIImage *image = [UIImage imageWithData:data];
[self insertMs:image];
_networkImage = image;
self.images[networkImg] = image;
} else {
[self.queue addOperationWithBlock:^{
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:networkImg]];
UIImage *image = [UIImage imageWithData:data];
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
_networkImage = image;
[self insertMs:image];
}];
self.images[networkImg] = image;
[data writeToFile:file atomically:YES];
}];
}
}
}
- (void)setImgSize:(CGRect)imgSize {
_imgSize = imgSize;
}
- (void)setPosition:(NSInteger)position {
_position = position;
if(_networkImage || _localImg) {
NSAttributedString *string = [NSAttributedString attributedStringWithAttachment:_attch];
NSMutableAttributedString *ms =[[NSMutableAttributedString alloc]init];
ms = (NSMutableAttributedString *)self.attributedText;
[ms replaceCharactersInRange:NSMakeRange(0, 1) withString:@""];
[ms insertAttributedString:string atIndex:position];
}
}
- (void)setImgMargin:(CGFloat)imgMargin {
_imgMargin = imgMargin;
if(_networkImage || _localImg) {
NSMutableAttributedString *ms =[[NSMutableAttributedString alloc]init];
ms = (NSMutableAttributedString *)self.attributedText;
self.attributedText = ms;
//创建透明图片
UIView * imageV = [[UIView alloc]init];
imageV.backgroundColor = [UIColor clearColor];
imageV.bounds = CGRectMake(0, 0, imgMargin, imgMargin);
//此为透明图片的边距
UIImage * image = [self imageWithUIView:imageV];
NSTextAttachment *attch = [[NSTextAttachment alloc] init];
attch.image = image;
attch.bounds = CGRectMake(0, 0, imgMargin, imgMargin);
NSAttributedString *string = [NSAttributedString attributedStringWithAttachment:attch];
if(_position == 0) {
[ms insertAttributedString:string atIndex:_position+1];
}else if(_position == ms.length-1){
[ms insertAttributedString:string atIndex:_position];
}else{
[ms insertAttributedString:string atIndex:_position];
[ms insertAttributedString:string atIndex:_position+2];
}
}
}
- (UIImage*) imageWithUIView:(UIView*) view{
UIGraphicsBeginImageContext(view.bounds.size);
CGContextRef currnetContext = UIGraphicsGetCurrentContext();
[view.layer renderInContext:currnetContext];
UIImage* image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
- (NSOperationQueue *)queue
{
if (!_queue) {
_queue = [[NSOperationQueue alloc] init];
_queue.maxConcurrentOperationCount = 3;
}
return _queue;
}
- (NSMutableDictionary *)images
{
if (!_images) {
_images = [NSMutableDictionary dictionary];
}
return _images;
}
@end