1

I made some custom fonts in Illustrator with the Fontself extension. I exported the fonts as an otf-file and mapped the glyphs on alphabetic keys (a,b,c,d,..). I wanted to use them to display an icon for my editActionsForRowAt method instead of a string.

I followed the steps provided by apple (and other guides) to include the fonts in my project, so no problem there.

 override func tableView(_ tableView: UITableView, editActionsForRowAt: IndexPath) -> [UITableViewRowAction]? {
        let doc = documentations.value[editActionsForRowAt.row]

        let deleteBtn = UITableViewRowAction(style: .normal, title: "Delete") { action, index in
            try? Documentation.db.cascadedDelete(documentationId: doc.id)
            var ary = self.documentations.value
            ary.remove(at: editActionsForRowAt.row)
            self.documentations.accept(ary)
        }
        deleteBtn.backgroundColor = UIColor.init(hexString: "#e53935")

        return [deleteBtn]
    }

I mapped a Trash-icon on a and want to display it instead of "Delete".

How can I include my Ios-fonts.otf file to display an icon instead of the title "Delete" ?

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
  • Great question. I don't believe this is possible directly (note that UITableViewRowAction is UIContextualAction since iOS 11, but that doesn't change anything about the question). I believe the best approach is to render this to an image and use that rather than text (`UIContextualAction.image`). There are a lot of hacky solutions here, but I recommend just rendering to an image with transparency: https://stackoverflow.com/questions/26887563/how-to-change-uitableviewrowaction-title-color/36145706#36145706 – Rob Napier Apr 21 '20 at 13:22
  • 1
    Thanks for your input. I already checked out a lot of solutions.. all hacky tho. I thought there must be an easier way. Since I m using iOS 8 this might be the only way. Thankls! –  Apr 21 '20 at 14:10
  • if you use UIContextualAction instead of UITableViewRowAction, [this](https://stackoverflow.com/a/60969075/8956604) helps for you – Kasım Özdemir Apr 21 '20 at 14:55

1 Answers1

0

first, import the font file into your project. second, add font file name into your info.plist file (Fonts provided by application) add string value into it. third, use them into your code like:

use this static function

enum FontStyle: String {
    case regular = "Regular"
    case semiBold = "SemiBold"
    case medium = "Medium"
}

static func applyFonts_SFProText(style: FontStyle, size: CGFloat) -> UIFont {
    return UIFont(name: "SFProText-\(style.rawValue)", size: size)!
}

use them example:

textLabel.font = applyFonts_SFProText(style: .semiBold, size: 14)
Muhammad Ahmad
  • 388
  • 4
  • 9