文字和图片合并显示

将图标和文字合并一起进行显示

@implementation TwiterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor= [UIColor whiteColor];
    
    YYLabel  * label = [YYLabel new];
    label.size = CGSizeMake(250, 300);
    label.left = 20;
    label.top = 100;
    label.textLayout = [self lebLayout];
    [self.view addSubview:label];
    NSMutableArray  * array = [NSMutableArray arrayWithObjects:@7,@9,@6,@12,@15, nil];
    [self BinarySearch:array target:@12];
    
}
-(NSInteger )BinarySearch:(NSArray *)array  target:(id)key
{
    NSInteger  left = 0;
    
    NSInteger right =  [array count] - 1;
    
    NSInteger middle = [array count] / 2;
    
    while (right >= left) {
        middle = (right + left)/2;
        
        if (array[middle] == key)
        {
            return  middle;
            
        }
        if (array[middle] > key) {
            right = middle -1;
        }
        else if (array[middle] < key)
        {
            left = middle +1;
        }
    }
    return -1;
}

-(YYTextLayout *)lebLayout
{
    
    NSMutableAttributedString  * str = [[NSMutableAttributedString alloc]initWithString:@"测试测试测试 "];
    UIImage  * image = [UIImage imageNamed:@"avatar_vgirl"];
    NSAttributedString *blueVText = [self _attachmentWithFontSize:20 image:image shrink:NO];
    [str appendAttributedString:blueVText];
    UIImage  * image2 = [UIImage imageNamed:@"avatar_vgirl"];
    NSAttributedString *blueVText2 = [self _attachmentWithFontSize:20 image:image2 shrink:NO];
    [str appendAttributedString:blueVText2];
    NSAttributedString  * sta1 = [[NSAttributedString alloc]initWithString:@"测试测试"];
    [str appendAttributedString:sta1];
    
    str.font = [UIFont systemFontOfSize:16];
    str.lineBreakMode = NSLineBreakByCharWrapping;
    YYTextContainer *container = [YYTextContainer containerWithSize:CGSizeMake(350, 9999)];
    container.maximumNumberOfRows = 1;
    lebyLayout  = [YYTextLayout layoutWithContainer:container text:str];
    return  lebyLayout;
    
}
// 图片 转文字占位
- (NSAttributedString *)_attachmentWithFontSize:(CGFloat)fontSize image:(UIImage *)image shrink:(BOOL)shrink
{
     CGFloat ascent =   YYEmojiGetAscentWithFontSize(fontSize);
     CGFloat descent =  YYEmojiGetDescentWithFontSize(fontSize);
     CGRect bounding =  YYEmojiGetGlyphBoundingRectWithFontSize(fontSize);

    // Heiti SC 字体。。
//    CGFloat ascent = fontSize * 0.86;
//    CGFloat descent = fontSize * 0.14;
//    CGRect bounding = CGRectMake(0, -0.14 * fontSize, fontSize, fontSize);
    UIEdgeInsets contentInsets = UIEdgeInsetsMake(ascent - (bounding.size.height + bounding.origin.y), 0, descent + bounding.origin.y, 0);
    
    YYTextRunDelegate *delegate = [YYTextRunDelegate new];
    delegate.ascent = ascent;
    delegate.descent = descent;
    delegate.width = bounding.size.width;
    
    YYTextAttachment *attachment = [YYTextAttachment new];
    attachment.contentMode = UIViewContentModeScaleAspectFit;
    attachment.contentInsets = contentInsets;
    attachment.content = image;
    
    if (shrink) {
        // 缩小~
        CGFloat scale = 1 / 10.0;
        contentInsets.top += fontSize * scale;
        contentInsets.bottom += fontSize * scale;
        contentInsets.left += fontSize * scale;
        contentInsets.right += fontSize * scale;
        contentInsets = UIEdgeInsetPixelFloor(contentInsets);
        attachment.contentInsets = contentInsets;
    }
    
    NSMutableAttributedString *atr = [[NSMutableAttributedString alloc] initWithString:YYTextAttachmentToken];
    [atr setTextAttachment:attachment range:NSMakeRange(0, atr.length)];
    CTRunDelegateRef ctDelegate = delegate.CTRunDelegate;
    [atr setRunDelegate:ctDelegate range:NSMakeRange(0, atr.length)];
    if (ctDelegate) CFRelease(ctDelegate);
    
    return atr;
}

效果图

效果图.png

你可能感兴趣的:(文字和图片合并显示)