如何在UILabel中嵌入小图标

AVE*_*imi 142 cocoa-touch objective-c ios ios7 textkit

我需要UILabel在iOS7中嵌入小图标(一些自定义项目符号).我如何在界面设计器中执行此操作?或者至少在代码中?

在Android中有leftDrawablerightDrawable贴标签,但它是如何在iOS中做了什么?android中的示例:

android示例

Sco*_*ets 284

您可以使用iOS 7的文本附件执行此操作,这些文本附件是TextKit的一部分.一些示例代码:

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

NSAttributedString *attachmentString = [NSAttributedString attributedStringWithAttachment:attachment];

NSMutableAttributedString *myString= [[NSMutableAttributedString alloc] initWithString:@"My label text"];
[myString appendAttributedString:attachmentString];

myLabel.attributedText = myString;
Run Code Online (Sandbox Code Playgroud)

  • 昨天已经试过了.似乎错过了一些东西,因为它现在有效.谢谢.以防每个人都试图完成同样的事情(因为它略有不同):`NSAttributedString*attachmentString = [NSAttributedString attributionStringWithAttachment:attachment]; NSMutableAttributedString*myString = [[NSMutableAttributedString alloc] initWithAttributedString:attachmentString]; NSAttributedString*myText = [[NSMutableAttributedString alloc] initWithString:text]; [myString appendAttributedString:myText];` (10认同)
  • @reVerse而不是将图像(附件字符串)附加到文本字符串,您可以尝试反过来,因此将文本字符串附加到附件字符串. (4认同)

Tar*_*era 128

这是在UILabel中嵌入图标的方法.

还要对齐图标使用attachment.bounds


Swift 4.2

//Create Attachment
let imageAttachment =  NSTextAttachment()
imageAttachment.image = UIImage(named:"iPhoneIcon")
//Set bound to reposition
let imageOffsetY:CGFloat = -5.0;
imageAttachment.bounds = CGRect(x: 0, y: imageOffsetY, width: imageAttachment.image!.size.width, height: imageAttachment.image!.size.height)
//Create string with attachment
let attachmentString = NSAttributedString(attachment: imageAttachment)
//Initialize mutable string
let completeText = NSMutableAttributedString(string: "")
//Add image to mutable string
completeText.append(attachmentString)
//Add your text to mutable string
let  textAfterIcon = NSMutableAttributedString(string: "Using attachment.bounds!")
completeText.append(textAfterIcon)
self.mobileLabel.textAlignment = .center;
self.mobileLabel.attributedText = completeText;
Run Code Online (Sandbox Code Playgroud)

Objective-C版本

 NSTextAttachment *imageAttachment = [[NSTextAttachment alloc] init];
imageAttachment.image = [UIImage imageNamed:@"iPhoneIcon"];
CGFloat imageOffsetY = -5.0;
imageAttachment.bounds = CGRectMake(0, imageOffsetY, imageAttachment.image.size.width, imageAttachment.image.size.height);
NSAttributedString *attachmentString = [NSAttributedString attributedStringWithAttachment:imageAttachment];
NSMutableAttributedString *completeText= [[NSMutableAttributedString alloc] initWithString:@""];
[completeText appendAttributedString:attachmentString];
NSMutableAttributedString *textAfterIcon= [[NSMutableAttributedString alloc] initWithString:@"Using attachment.bounds!"];
[completeText appendAttributedString:textAfterIcon];
self.mobileLabel.textAlignment=NSTextAlignmentRight;
self.mobileLabel.attributedText=completeText;
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

在此输入图像描述

  • 投票附件.bounds (21认同)
  • 实际上,可以计算 imageOffsetY 而不是使用固定值 -5.0。让 imageOffsetY:CGFloat = -(imageAttachment.image!.size.height - self.mobileLabel.font.pointSize) / 2.0; (4认同)
  • 很好的使用attachment.bounds.这正是我想要的. (3认同)

And*_*tos 50

Swift 4.2:

let attachment = NSTextAttachment()        
attachment.image = UIImage(named: "yourIcon.png")
let attachmentString = NSAttributedString(attachment: attachment)
let myString = NSMutableAttributedString(string: price)
myString.append(attachmentString)
label.attributedText = myString
Run Code Online (Sandbox Code Playgroud)

  • 不工作ios swift 3和xcode 8.2 (2认同)

Som*_*Guy 23

您的参考图像看起来像一个按钮.尝试(也可以在Interface Builder中完成):

在此输入图像描述

UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(50, 50, 100, 44)];
[button setImage:[UIImage imageNamed:@"img"] forState:UIControlStateNormal];
[button setImageEdgeInsets:UIEdgeInsetsMake(0, -30, 0, 0)];
[button setTitle:@"Abc" forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button setBackgroundColor:[UIColor yellowColor]];
[view addSubview:button];
Run Code Online (Sandbox Code Playgroud)


Raj*_*r R 20

Swift 3版

let attachment = NSTextAttachment()
attachment.image = UIImage(named: "plus")
attachment.bounds = CGRect(x: 0, y: 0, width: 10, height: 10)
let attachmentStr = NSAttributedString(attachment: attachment)
let myString = NSMutableAttributedString(string: "")
myString.append(attachmentStr)
let myString1 = NSMutableAttributedString(string: "My label text")
myString.append(myString1)
lbl.attributedText = myString
Run Code Online (Sandbox Code Playgroud)


ana*_*y_v 17

我已经在swift中实现了这个功能:https://github.com/anatoliyv/SMIconLabel

代码尽可能简单:

var labelLeft = SMIconLabel(frame: CGRectMake(10, 10, view.frame.size.width - 20, 20))
labelLeft.text = "Icon on the left, text on the left"

// Here is the magic
labelLeft.icon = UIImage(named: "Bell") // Set icon image
labelLeft.iconPadding = 5               // Set padding between icon and label
labelLeft.numberOfLines = 0             // Required
labelLeft.iconPosition = SMIconLabelPosition.Left // Icon position
view.addSubview(labelLeft)
Run Code Online (Sandbox Code Playgroud)

以下是它的外观:

SMIconLabel图像


Ash*_*hra 12

在 Swift 5 中,通过使用 UILabel 扩展在文本的前导和尾随中嵌入图标,如下所示:-

extension UILabel {
    
    func addTrailing(image: UIImage, text:String) {
        let attachment = NSTextAttachment()
        attachment.image = image

        let attachmentString = NSAttributedString(attachment: attachment)
        let string = NSMutableAttributedString(string: text, attributes: [:])

        string.append(attachmentString)
        self.attributedText = string
    }
    
    func addLeading(image: UIImage, text:String) {
        let attachment = NSTextAttachment()
        attachment.image = image

        let attachmentString = NSAttributedString(attachment: attachment)
        let mutableAttributedString = NSMutableAttributedString()
        mutableAttributedString.append(attachmentString)
        
        let string = NSMutableAttributedString(string: text, attributes: [:])
        mutableAttributedString.append(string)
        self.attributedText = mutableAttributedString
    }
}
Run Code Online (Sandbox Code Playgroud)

要在您想要的标签中使用上述代码,如下所示:-

文本右侧的图像然后:-

statusLabel.addTrailing(image: UIImage(named: "rightTick") ?? UIImage(), text: " Verified ")
Run Code Online (Sandbox Code Playgroud)

文本左侧的图像然后:-

statusLabel.addLeading(image: UIImage(named: "rightTick") ?? UIImage(), text: " Verified ")
Run Code Online (Sandbox Code Playgroud)

输出:-

在此输入图像描述

在此输入图像描述


Sha*_*med 9

Swift 5 Easy Way 只需复制粘贴并更改您想要的内容

let fullString = NSMutableAttributedString(string:"To start messaging contacts who have Talklo, tap ")

 // create our NSTextAttachment
let image1Attachment = NSTextAttachment() 
image1Attachment.image = UIImage(named: "chatEmoji")
image1Attachment.bounds = CGRect(x: 0, y: -8, width: 25, height: 25)

// wrap the attachment in its own attributed string so we can append it
let image1String = NSAttributedString(attachment: image1Attachment)

 // add the NSTextAttachment wrapper to our full string, then add some more text.

 fullString.append(image1String)
 fullString.append(NSAttributedString(string:" at the right bottom of your screen"))

 // draw the result in a label
 self.lblsearching.attributedText = fullString
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

  • 从 2021 年开始,您的屏幕发生了什么;) (2认同)
  • 这是我女朋友的礼物,她离开了我,但我怎么能扔掉这份礼物呢?:-( (2认同)
  • 悲剧。我认为你应该保留它。 (2认同)
  • 从来没有想过堆栈溢出会让我如此沮丧 (2认同)

Age*_*ith 8

Swift 4 UIlabel Extension将参考上述答案添加到Label的图像

extension UILabel {
  func set(image: UIImage, with text: String) {
    let attachment = NSTextAttachment()
    attachment.image = image
    attachment.bounds = CGRect(x: 0, y: 0, width: 10, height: 10)
    let attachmentStr = NSAttributedString(attachment: attachment)

    let mutableAttributedString = NSMutableAttributedString()
    mutableAttributedString.append(attachmentStr)

    let textString = NSAttributedString(string: text, attributes: [.font: self.font])
    mutableAttributedString.append(textString)

    self.attributedText = mutableAttributedString
  }
}
Run Code Online (Sandbox Code Playgroud)


use*_*099 5

试试这个方法...

  self.lbl.text=@"Drawble Left";
    UIImageView *img=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 20, 20)];
    img.image=[UIImage imageNamed:@"Star.png"];
    [self.lbl addSubview:img];
Run Code Online (Sandbox Code Playgroud)