---
title: Day 56
author: Daniel Devesa Derksen-Staats
date: 2022-07-13 13:49
tags: VoiceOver, accessibilityLabel, iOS
categories: ["Accessibility"]
series: ["365 Days iOS Accessibility"]
image: /Images/365DaysIOSAccessibility/image151.jpg
---

It is possible to embed icons within text using NSTextAttachment and NSAttributedString. If you do, please remember to override the accessibility label, otherwise VoiceOver will announce it as "Attachment.png File".  

![If you assign an attributed string to the attributed text property in a label, and the attributed string contains an nstextattachment that is an image, you need to override the accessibility label of the label. In the example, if you don't do it, VoiceOver would say: "Select the Attachment.png, File button to find elements in the list." If you do, VoiceOver would say: "Select the Search button to find elements in the list".](/Images/365DaysIOSAccessibility/image151.jpg)

[Example code in the image:](https://gist.github.com/dadederk/04c1f0ee17c619083ee5d0cca6dbfdd4)

```swift
let magnifyingGlassIcon = UIImage(systemName: "magnifyingglass")!
let searchButton = UIButton()
let searchTutorialLabel = UILabel()
searchButton.accessibilityLabel = "search"

let textAttachment = NSTextAttachment(image: magnifyingGlassIcon)
let string = "Select the <icon> button to find elements in the list"
let attributedString = NSMutableAttributedString(string: string)
let attributedStringIcon = NSAttributedString(attachment: textAttachment)
let iconPlaceholderRange = attributedString.string.range(of: "<icon>")!
let iconRange = NSRange(iconPlaceholderRange, in: attributedString.string)

attributedString.replaceCharacters(in: iconRange, with: attributedStringIcon)
searchTutorialLabel.attributedText = attributedString


searchTutorialLabel.accessibilityLabel = string.replacingCharacters(in: iconPlaceholderRange,
                                                                    with: searchButton.accessibilityLabel!)
```
