简体   繁体   中英

How to create dropdown with icon and custom text colors in swift?

I want to use dropdown within tableview cell and I was trying to use AssistoLab / ( https://github.com/AssistoLab/DropDown )DropDown library but the problem is I have to give different colors for the dropdown menu option text. As of my R & D it's not possible with AssistoLab/DropDown. If anybody has some idea plz help me out.

import UIKit
import DropDown



class AddWorkoutCategoryTableViewCell: UITableViewCell {

    
    @IBOutlet weak var dropDownView: UIView?
    
    
    let dropDown = DropDown()
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
    
    func configureGenderDropDown() {
        let blackColor = UIColor.black // 1.0 alpha
        let defaultDropDownBackgroundColor = blackColor.withAlphaComponent(0.7) // 0.8 alpha
        DropDown.appearance().textColor = UIColor.white
        dropDown.anchorView = dropDownView
        DropDown.appearance().selectedTextColor = UIColor.red
        DropDown.appearance().textFont = UIFont.systemFont(ofSize: 15)
        DropDown.appearance().backgroundColor = defaultDropDownBackgroundColor
        DropDown.appearance().selectionBackgroundColor = UIColor.black
        DropDown.appearance().cellHeight = 40
        dropDown.selectRow(at: 3)
        
        dropDown.dataSource = ["Sendworkout", "Edit Workout", "Duplicate workout", "Delete workout"]
    }

}

在此处输入图像描述

//ViewController.swift


import UIKit
import DropDown

class ViewController: UIViewController {
    @IBOutlet weak var btn: UIButton!
    let dropDown = DropDown()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        let a = UILabel()
        dropDown.dataSource = ["pencil.circle", "scribble", "trash.fill"]
        let icons = ["pencil.circle", "scribble", "trash.fill" ]
        dropDown.anchorView = btn
        
        
        dropDown.cellNib = UINib(nibName: "MyDropDownCell", bundle: nil)
        dropDown.customCellConfiguration = { (index: Index, item: String, cell: DropDownCell) -> Void in
           guard let cell = cell as? MyDropDownCell else { return }

           // can use attrubted string for colored text
           cell.myText.text = "Hello \(index)"
           cell.logoImageView.image = UIImage(systemName: icons[index])
        }
        
    }

    @IBAction func go(_ sender: Any) {
        dropDown.show()
    }
    
}

    //
    //  MyDropDownCell.swift
    
    //
    //  Created by developer on 6/3/21.
    //
    
        import UIKit
        import DropDown
        class MyDropDownCell: DropDownCell {
            @IBOutlet weak var logoImageView: UIImageView!
            
            @IBOutlet weak var myText: UILabel!
            
            
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM