如何在 UILabel 中嵌入小图标

我需要在 iOS7中嵌入小图标(类似自定义项目符号)到我的 UILabel。 如何在界面设计器中做到这一点? 或者至少在代码中?

在 Android 系统中,标签有 leftDrawablerightDrawable,但是在 iOS 系统中是如何实现的呢? 机器人中的例子:

android sample

138190 次浏览

你必须创建一个自定义对象,其中使用了 UIView,并在其中放入了 UIImageViewUILabel

试试这样..。

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

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

enter image description here

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

您可以使用具有 Left 视图属性的 UITextField,然后将 enabled属性设置为 NO

或者使用 UIButtonsetImage:forControlState

你可以用 iOS7的 文本附件来实现这一点,它是 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;

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

迅捷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

我已经在 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)

看起来是这样的:

SMIconLabel image

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

在 Swift 2.0中,

我对这个问题的解决办法是把这个问题的几个答案结合起来。我在@Phil 的回答中遇到的问题是,我无法改变图标的位置,它总是出现在右边的角落。从@anatoliy _ v 得到的一个答案是,我无法调整想要附加到字符串的图标大小。

为了让它为我工作,我首先做了一个 pod 'SMIconLabel',然后创建了这个函数:

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
}

这个解决方案不仅可以帮助您放置图像,而且还允许您对图标大小和其他属性进行必要的更改。

谢谢你。

下面是在 UILabel 中嵌入图标的方法。

也要对 对齐图标使用 依恋,界限


Swift 5.1

// 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 = NSAttributedString(string: "Using attachment.bounds!")
completeText.append(textAfterIcon)
self.mobileLabel.textAlignment = .center
self.mobileLabel.attributedText = completeText

目标 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];
NSAttributedString *textAfterIcon = [[NSAttributedString alloc] initWithString:@"Using attachment.bounds!"];
[completeText appendAttributedString:textAfterIcon];
self.mobileLabel.textAlignment = NSTextAlignmentRight;
self.mobileLabel.attributedText = completeText;

enter image description here

enter image description here

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

UILabel 扩展部

extension UILabel {


func set(text:String, leftIcon: UIImage? = nil, rightIcon: UIImage? = nil) {


let leftAttachment = NSTextAttachment()
leftAttachment.image = leftIcon
leftAttachment.bounds = CGRect(x: 0, y: -2.5, width: 20, height: 20)
if let leftIcon = leftIcon {
leftAttachment.bounds = CGRect(x: 0, y: -2.5, width: leftIcon.size.width, height: leftIcon.size.height)
}
let leftAttachmentStr = NSAttributedString(attachment: leftAttachment)


let myString = NSMutableAttributedString(string: "")


let rightAttachment = NSTextAttachment()
rightAttachment.image = rightIcon
rightAttachment.bounds = CGRect(x: 0, y: -5, width: 20, height: 20)
let rightAttachmentStr = NSAttributedString(attachment: rightAttachment)




if semanticContentAttribute == .forceRightToLeft {
if rightIcon != nil {
myString.append(rightAttachmentStr)
myString.append(NSAttributedString(string: " "))
}
myString.append(NSAttributedString(string: text))
if leftIcon != nil {
myString.append(NSAttributedString(string: " "))
myString.append(leftAttachmentStr)
}
} else {
if leftIcon != nil {
myString.append(leftAttachmentStr)
myString.append(NSAttributedString(string: " "))
}
myString.append(NSAttributedString(string: text))
if rightIcon != nil {
myString.append(NSAttributedString(string: " "))
myString.append(rightAttachmentStr)
}
}
attributedText = myString
}
}

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
}
}
 func atributedLabel(str: String, img: UIImage)->NSMutableAttributedString
{   let iconsSize = CGRect(x: 0, y: -2, width: 16, height: 16)
let attributedString = NSMutableAttributedString()
let attachment = NSTextAttachment()
attachment.image = img
attachment.bounds = iconsSize
attributedString.append(NSAttributedString(attachment: attachment))
attributedString.append(NSAttributedString(string: str))


return attributedString
}

可以使用此函数向标签添加图像或小图标

SWIFT 4 UIlabel扩展,将图像添加到标签中,并引用上述答案

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
}
}

快速5简单的方法只要复制粘贴和改变你想要的

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

enter image description here

在 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
}
}

在您所需的标签中使用上述代码:-

然后文本右边的图像:-

statusLabel.addTrailing(image: UIImage(named: "rightTick") ?? UIImage(), text: " Verified ")

然后文本左边的图像:-

statusLabel.addLeading(image: UIImage(named: "rightTick") ?? UIImage(), text: " Verified ")

产出:-

enter image description here

enter image description here

您可以扩展 UILabe 传递图像外接程序的标志,如果需要,也可以设置 imageBounds。

Swift 5 +

extension UILabel {
func add(image: UIImage, text: String, isLeading: Bool = true, imageBounds: CGRect = CGRect(x: 0, y: 0, width: 16, height: 12)) {
let imageAttachment = NSTextAttachment()
imageAttachment.bounds = imageBounds
        

imageAttachment.image = image
        

let attachmentString = NSAttributedString(attachment: imageAttachment)
let string = NSMutableAttributedString(string: text)
        

let mutableAttributedString = NSMutableAttributedString()
        

if isLeading {
mutableAttributedString.append(attachmentString)
mutableAttributedString.append(string)
attributedText = mutableAttributedString
} else {
string.append(attachmentString)
attributedText = string
}
}
}

对于那些希望在标签右端有一个图标的人,不一定要紧跟在文字后面,你可以根据这个答案使用这个技巧: < a href = “ https://stackoverflow./a/19318843/826946”> https://stackoverflow.com/a/19318843/826946 (请注意,这里有一些常量,您可能想要调整,但总体思路应该是清楚的)。如果标签使用隐式大小调整大小,那么这种方法将不起作用,只有在对宽度有其他限制的情况下,您才有信心为图标留出空间。

    let imgView = UIImageView(frame: CGRect(x: 0, y: 0, width: 20, height: 20))
imgView.image = UIImage(named: "arrow")
myLabel.addSubview(imgView)
imgView.translatesAutoresizingMaskIntoConstraints = false
imgView.centerYAnchor.constraint(equalTo: myLabel.centerYAnchor, constant: 0).isActive = true
imgView.rightAnchor.constraint(equalTo: myLabel.rightAnchor, constant: -20).isActive = true

如果您不是绝对需要一个标签(可能在某些情况下需要) ,您可以使用带约束的视图层次结构来创建一个 UIView,它可以放置在标签所在的位置。当然,组合视图的图像和标签文本可以动态更新。

func configureTitleLabelWithIcon(text: String, parent: UIView) {


let iconAndLabel = UIView()
let label = UILabel()
let icon  = UIImageView(image:UIImage(systemName: "globe"))


label.text = text
label.textColor = UIColor.secondaryLabel
    

label.translatesAutoresizingMaskIntoConstraints = false
ico .translatesAutoresizingMaskIntoConstraints = false
iconAndLabel.translatesAutoresizingMaskIntoConstraints = false


iconAndLabel.addSubview(icon)
iconAndLabel.addSubview(label)


icon.widthAnchor.constraint(  equalToConstant: 14).isActive = true
icon.heightAnchor.constraint( equalToConstant: 14).isActive = true


iconAndLabel.widthAnchor.constraint(  equalToConstant: 200).isActive = true
iconAndLabel.heightAnchor.constraint( equalToConstant: 22).isActive = true


icon.leadingAnchor.constraint(equalTo:   iconAndLabel.leadingAnchor).isActive = true
icon.trailingAnchor.constraint(equalTo:  label.leadingAnchor, constant: -7).isActive = true
label.trailingAnchor.constraint(equalTo: iconAndLabel.trailingAnchor).isActive = true
icon.centerYAnchor.constraint(equalTo:   iconAndLabel.centerYAnchor).isActive = true
view.addSubview(iconAndLabel)


label.centerXAnchor.constraint(equalTo: parent, constant: 10).isActive = true
label.bottomAnchor.constraint(equalTo:  parent, constant: -75).isActive = true
}

Swift 5 +

如果你想保持图像和图像的比例始终以文本为中心,那么,这就是我的解决方案:

extension UILabel {
var mutableAttributedString: NSMutableAttributedString? {
let attributedString: NSMutableAttributedString
if let labelattributedText = self.attributedText {
attributedString = NSMutableAttributedString(attributedString: labelattributedText)
} else {
guard let labelText = self.text else { return nil }
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = self.textAlignment
attributedString = NSMutableAttributedString(string: labelText)
attributedString.addAttribute(NSAttributedString.Key.paragraphStyle,
value: paragraphStyle,
range: NSRange(location: 0, length: attributedString.length))
}
return attributedString
}


func addImage(_ image: UIImage, toEndWith height: CGFloat) {
let fullAttributedString = mutableAttributedString
let imageAttachment = NSTextAttachment()
imageAttachment.image = image


let yImage = (font.capHeight - height).rounded() / 2
let ratio = image.size.width / image.size.height
imageAttachment.bounds = CGRect(x: 0, y: yImage, width: ratio * height, height: height)
        

let imageString = NSAttributedString(attachment: imageAttachment)
fullAttributedString?.append(imageString)
attributedText = fullAttributedString
}
    

func addImage(_ image: UIImage, toStartWith height: CGFloat) {
let imageAttachment = NSTextAttachment()
imageAttachment.image = image


let yImage = (font.capHeight - height).rounded() / 2
let ratio = image.size.width / image.size.height
imageAttachment.bounds = CGRect(x: 0, y: yImage, width: ratio * height, height: height)
        

let fullAttributed = NSMutableAttributedString(attachment: imageAttachment)
if let rawAttributed = mutableAttributedString {
fullAttributed.append(rawAttributed)
}
attributedText = fullAttributed
}
}

下面是如何使用上面的扩展:

let label = UILabel(frame: CGRect(x: 0, y: 0, width: 100, height: 20))
label.font = .systemFont(ofSize: 20)
let image = UIImage(systemName: "square.and.pencil")!
label.text = "Hi, "
label.addImage(image, toEndWith: 10)

以下是一些例子:

enter image description here

enter image description here

enter image description here

与属性化字符串一起使用:

let myString = "Hi, "
let myAttribute: [NSAttributedString.Key: UIColor] = [.foregroundColor: .blue]
let myAttrString = NSAttributedString(string: myString, attributes: myAttribute)
label.attributedText = myAttrString
label.addImage(image, toEndWith: 15)

enter image description here