【问题标题】:attach image to UILabel not working将图像附加到 UILabel 不起作用
【发布时间】:2016-03-25 15:36:17
【问题描述】:

我想在iOS中给UILabel添加右小图标

这是我正在使用的代码

NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
        attachment.image = [UIImage imageNamed:@"icon_list"];

        NSAttributedString *attachmentString = [NSAttributedString attributedStringWithAttachment:attachment];

        NSMutableAttributedString *myString= [[NSMutableAttributedString alloc] initWithString:@"Hi"];
        [myString appendAttributedString:attachmentString];

        cell.categoryTitle.attributedText = myString;

我得到的结果如下,字符串和三个点,谁能告诉我是什么问题?

【问题讨论】:

    标签: ios iphone uiimage uilabel


    【解决方案1】:

    问题是图标大小非常大,我通过在添加到我的标签之前调整图标大小来解决它

    所以工作代码如下:

    UIImage *image1 = [UIImage imageNamed:@"icon_ads.png”];
    
    UIImage *myIcon = [self imageWithImage:image1 scaledToSize:CGSizeMake(20, 20)];
    
    NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
    
    attachment.image = myIcon;
    
    NSAttributedString *attachmentString = [NSAttributedString attributedStringWithAttachment:attachment];
    
    NSAttributedString *titleString = [[NSAttributedString alloc] initWithString:test.catName];
    
    NSMutableAttributedString *myString = [[NSMutableAttributedString alloc] initWithString:@""];
    
    [myString appendAttributedString:attachmentString];
    
    [myString appendAttributedString:titleString];
    
    cell.categoryTitle.attributedText = myString; 
    

    【讨论】:

      猜你喜欢
      • 2017-11-12
      • 1970-01-01
      • 1970-01-01
      • 2019-11-14
      • 2022-06-29
      • 1970-01-01
      • 2014-03-18
      • 2011-06-01
      • 2018-10-22
      相关资源
      最近更新 更多