如何在UILabel中embedded小图标

我需要在iOS7的UILabel中embedded小图标(自定义项目符号)。 我怎么能在界面devise器中做到这一点? 或者至less在代码?

在Android中有leftDrawable和rightDrawable标签,但它是如何在iOS中完成的? 在android中的示例: android示例

您可以使用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; 

这里是在UILabel中embedded图标的方法。

还要alignment图标使用attachment.bounds


Swift版本

 //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; 

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; 

在这里输入图像描述

在这里输入图像描述

迅速:

 let attachment = NSTextAttachment() attachment.image = UIImage(named: "yourIcon.png") let attachmentString = NSAttributedString(attachment: attachment) let myString = NSMutableAttributedString(string: price) myString.appendAttributedString(attachmentString) label.attributedText = myString 

您的参考图像看起来像一个button。 尝试(也可以在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]; 

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 

我已经在这里迅速实现了这个function: 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) 

以下是它的外观:

SMIconLabel图像

Swift 2.0版本:

 //Get image and set it's size let image = UIImage(named: "imageNameWithHeart") let newSize = CGSize(width: 10, height: 10) //Resize image UIGraphicsBeginImageContextWithOptions(newSize, false, 0.0) image?.drawInRect(CGRectMake(0, 0, newSize.width, newSize.height)) let imageResized = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() //Create attachment text with image var attachment = NSTextAttachment() attachment.image = imageResized var attachmentString = NSAttributedString(attachment: attachment) var myString = NSMutableAttributedString(string: "I love swift ") myString.appendAttributedString(attachmentString) myLabel.attributedText = myString 

尝试拖动一个UIView到IB的屏幕上。 从那里你可以拖动一个UIImageView和UILabel到你刚刚创build的视图中。 在属性检查器中将UIImageView的图像设置为自定义项目符号图像(您必须将其添加到项目中,方法是将其拖动到导航窗格中),然后可以在标签中写入一些文本。

试试这个方法…

  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]; 

您可以使用带有leftView属性的UITextField ,然后将enabled属性设置为NO

或者使用UIButton和setImage:forControlState

在Swift 2.0中,

我对这个问题的解决scheme是对这个问题的几个答案的组合。 @菲尔答案中我所面临的问题是,我无法改变图标的​​位置,它总是出现在angular落。 和@anatoliy_v的一个答案,我无法调整我想追加到string的图标大小。

为了使它为我工作,我首先做了一个pod 'SMIconLabel' ,然后创build了这个function:

 func drawTextWithIcon(labelName: SMIconLabel, imageName: String, labelText: String!, width: Int, height: Int) { let newSize = CGSize(width: width, height: height) let image = UIImage(named: imageName) UIGraphicsBeginImageContextWithOptions(newSize, false, 0.0) image?.drawInRect(CGRectMake(0, 0, newSize.width, newSize.height)) let imageResized = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() labelName.text = " \(labelText)" labelName.icon = imageResized labelName.iconPosition = .Left } 

此解决scheme不仅可以帮助您放置图像,还可以让您对图标大小和其他属性进行必要的更改。

谢谢。

你必须在你使用UIView地方制作一个自定义的对象,并在里面放置一个UIImageView和一个UILabel

Swift 3 UILabel扩展

提示:如果在图像和文本之间需要一些空格,请在labelText之前使用一个或两个空格。

 extension UILabel { func addIconToLabel(imageName: String, labelText: String, bounds_x: Double, bounds_y: Double, boundsWidth: Double, boundsHeight: Double) { let attachment = NSTextAttachment() attachment.image = UIImage(named: imageName) attachment.bounds = CGRect(x: bounds_x, y: bounds_y, width: boundsWidth, height: boundsHeight) let attachmentStr = NSAttributedString(attachment: attachment) let string = NSMutableAttributedString(string: "") string.append(attachmentStr) let string2 = NSMutableAttributedString(string: labelText) string.append(string2) self.attributedText = string } }