美文网首页
文字和图片合并显示

文字和图片合并显示

作者: 海牛骑士 | 来源:发表于2019-05-27 20:16 被阅读0次

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

@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

相关文章

  • 文字和图片合并显示

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

  • 小程序入坑指南

    使用小程序时,请求数据后,数据中有文字和图片,文字能够正常显示,但是图片显示不出来,并报出以下错误: 原因: 图片...

  • Qt:QLabe显示文字和图片

    QLabe Class参考文档 文字与图片的显示格式实际上是用Qt StyleSheet(样式表)设置的与css样...

  • python中pygame基础

    pygame 游戏的最小系统 在窗口显示图片 显示文字 显示图形 事件和动画

  • css sprite

    定义:是一种CSS图像合并技术,该方法是将小图标和背景图像合并到一张图片上,然后利用css的背景定位来显示需要显示...

  • 雪碧图的简单制作

    雪碧图是一种CSS图像合并技术,该方法是将小图标和背景图像合并到一张图片上,然后利用css的背景定位来显示需要显示...

  • 2018-07-27 day10 Pygame模块

    Pygame模块 显示文字 显示图片 显示图形 动画

  • UIButton 上图片下文字显示

    要实现的效果: 图片在文字之上显示,图片和文字都居中显示,可控制图片和文字之间的间距。 计算: 正常图片不设置按钮...

  • Day_10 异常与pygame

    异常捕获 pygame操作流程 pygame显示文字 pygame显示图片与图片操作 pygame基本显示

  • day17-pygame

    一、图片显示 二、文字显示 三、图形显示 四、事件

网友评论

      本文标题:文字和图片合并显示

      本文链接:https://www.haomeiwen.com/subject/iiufcqtx.html