ios - Swift selectable label - TagMerge
2Swift selectable labelSwift selectable label

Swift selectable label

Asked 8 months ago
1
2 answers

I was thinking to something like :

public class LayerViewController: UIViewController {
    // Type of style to apply
    enum StyleType: Int, CaseIterable {
        case normal = 1
        case satellite = 2
        case terrain = 3
        case explore = 4
        // Add more styles here and increment value
    }
    // Button images
    static let buttonImages: [StyleType:String] =
    [.normal: "normal",
     .satellite: "satellite",
     .terrain: "terrain",
     .explore: "explore"]
        // Add more button images for new styles
    
    // The selected style
    var selectedSyle = StyleType.normal
    
    var buttons: [StyleType:UIButton] = [:]

    override public func viewDidLoad() {
        super.viewDidLoad()
        
        addButton(.normal)
        addButton(.satellite)
        addButton(.terrain)
        addButton(.explore)
        setStyle(selectedSyle)
    }
    
    // Button creation and adding to button dictionary
    func addButton(_ style: StyleType) {
        let x: CGFloat = 100 + 200 * CGFloat(buttons.count)
        
        let frame = CGRect(
            x: x,
            y: self.view.frame.height * 0.2,
            width: 100,
            height: 100
        )
        if let imageName = Self.buttonImages[style] {
            let button = UIButton(frame: frame)
            
            // Use tag to know which button is touched
            button.tag = style.rawValue
            button.setBackgroundImage(UIImage(named: imageName), for: .normal)
            button.layer.borderColor = .init(red: 0, green: 0, blue: 255, alpha: 1)
            button.layer.borderWidth = 5
            button.isSelected = true
            button.addTarget(self, action: #selector(selectStyle(_:)), for: .touchUpInside)
            buttons[style] = button
            self.view.addSubview(button)
        }
    }

    // Update the style of button when selecting one style
    func setStyle(_ selectedStyle: StyleType) {
        for (style, button) in buttons {
            if style == selectedStyle {
                button.layer.borderColor = .init(red: 0, green: 0, blue: 255, alpha: 1)
                button.layer.borderWidth = 5
                button.isSelected = true
                selectedSyle = style
            } else {
                button.layer.borderColor = .init(red: 0, green: 0, blue: 0, alpha: 0)
                button.layer.borderWidth = 0
                button.isSelected = false
            }
            
        }
    }

    // Called when button touched
    @objc func selectStyle(_ sender: UIButton) {
        // Get style via button tag
        if let selectedStyle = StyleType(rawValue: sender.tag) {
            self.setStyle(selectedStyle)
        }
    }
}

Source: link

0

You can also do this in code with:
label.numberOfLines = 0
Lets look at some other ways we can fit text on labels. Connect up our labels on the storyboard all as outlets in our code names as follows:
@IBOutlet var greet1: UILabel!
    @IBOutlet var greet2: UILabel!
    @IBOutlet var greet3: UILabel!
    @IBOutlet var greet4: UILabel!
The first way is by truncating text, this means that some text is cut off. We can adjust the cut off point to be at the start of the label, middle or end. Add this in viewDidLoad().
greet1.lineBreakMode = .byTruncatingMiddle
Next up we can adjust the text size to fit the label the best it can, to do this on greet3, simply add the following line of code also in viewDidLoad()
greet3.adjustsFontSizeToFitWidth = true
To do this simply create a new empty Swift file named UILabel.swift and code it as follows:
import UIKit

extension UILabel {
    var optimalHeight : CGFloat {
        get
        {
            let label = UILabel(frame: CGRect(x: 0, y: 0, width: self.bounds.width, height: CGFloat.greatestFiniteMagnitude))
            label.numberOfLines = 0
            label.lineBreakMode = NSLineBreakMode.byWordWrapping
            label.font = self.font
            label.text = self.text
            label.sizeToFit()
            return label.frame.height
        }
        
    }
}

Source: link

Recent Questions on ios

    Programming Languages