Programim dhe zhvillim, javascript, python, php, html

Krijo në mënyrë programore UIView me madhësi bazuar në nënshikimet (etiketat)

Unë kam një skenar ku më nevojitet për të krijuar një UIView plotësisht programatikisht. Kjo pamje shfaqet si një mbivendosje mbi një furnizim video dhe përmban 2 etiketa UIL si nënshikime.

Këto etiketa mund të kenë gjerësi dhe lartësi të ndryshme, kështu që unë nuk dua të krijoj një kornizë fikse për UIView ose etiketat.

Unë e kam bërë këtë me kufizime programatike pa problem, por problemi është se pamjet janë të gabuara kur lëviz telefonin dhe korniza që po përdor, e cila rekomandon që furnizimi i videos të mos përdoret automatikisht për shkak të këtij problemi.

Kodi im aktual:

  • Unë krijoj pamjen

        func ar(_ arViewController: ARViewController, viewForAnnotation: ARAnnotation) -> ARAnnotationView {
                let annotationView = AnnotationView() //*this is the view, inherits from UIView
                annotationView.annotation = viewForAnnotation
                annotationView.delegate = self
    
                return annotationView
            }
    
  • Dhe kodi aktual i pamjes

    class AnnotationView: ARAnnotationView {
    
    
            var titleLabel: UILabel?
            var distanceLabel: UILabel!
            var delegate: AnnotationViewDelegate?
            override var annotation: ARAnnotation? {
                didSet {
                    loadUI()
                }
            }
    
            override func didMoveToSuperview() {
                super.didMoveToSuperview()
    
                setupUI()
            }
    
            private func setupUI() {
                layer.cornerRadius = 5
                layer.masksToBounds = true
                backgroundColor = .transparentWhite
            }
    
            private func loadUI() {
                titleLabel?.removeFromSuperview()
                distanceLabel?.removeFromSuperview()
    
                let label = UILabel()
                label.font = UIFont(name: "AvenirNext-Medium", size: 16.0) ?? UIFont.systemFont(ofSize: 14)
                label.numberOfLines = 2
                label.textColor = UIColor.white
                self.titleLabel = label
    
                distanceLabel = UILabel()
                distanceLabel.textColor = .cbPPGreen
                distanceLabel.font = UIFont(name: "AvenirNext-Medium", size: 14.0) ?? UIFont.systemFont(ofSize: 12)
                distanceLabel.textAlignment = .center
    
                self.translatesAutoresizingMaskIntoConstraints = false
                label.translatesAutoresizingMaskIntoConstraints = false
                distanceLabel.translatesAutoresizingMaskIntoConstraints = false
    
                self.addSubview(label)
                self.addSubview(distanceLabel)
    
                if self.constraints.isEmpty {
                    NSLayoutConstraint.activate([
                        NSLayoutConstraint(item: label, attribute: .left, relatedBy: .equal, toItem: self, attribute: .left, multiplier: 1, constant: 5),
                        NSLayoutConstraint(item: label, attribute: .right, relatedBy: .equal, toItem: self, attribute: .right, multiplier: 1, constant: -5),
                        NSLayoutConstraint(item: label, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1, constant: 0),
                        NSLayoutConstraint(item: label, attribute: .bottom, relatedBy: .equal, toItem: distanceLabel, attribute: .top, multiplier: 1, constant: 0),
    
                        NSLayoutConstraint(item: distanceLabel, attribute: .left, relatedBy: .equal, toItem: self, attribute: .left, multiplier: 1, constant: 0),
                        NSLayoutConstraint(item: distanceLabel, attribute: .right, relatedBy: .equal, toItem: self, attribute: .right, multiplier: 1, constant: 0),
                        NSLayoutConstraint(item: distanceLabel, attribute: .bottom, relatedBy: .equal, toItem: self, attribute: .bottom, multiplier: 1, constant: 0),
                        NSLayoutConstraint(item: distanceLabel, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 15),
                        ])
        }
    
                if let annotation = annotation as? BikeStationAR {
                    titleLabel?.text = annotation.address
                    distanceLabel?.text = String(format: "%.2f km", annotation.distanceFromUser / 1000)
                }
            }
    
            override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
                delegate?.didTouch(annotationView: self)
            }
        }        
    

Pra, problemi im është tani: si mund të arrij të njëjtën sjellje, pa përdorur autolayout?

Edit: këtu është një video e sjelljes https://streamable.com/s/4zusq/dvbgnb

Redaktimi 2: Zgjidhja bazohet në përgjigjen e Mahgol Fa dhe duke përdorur një pamje rafte ose pozicionim vertikal në mënyrë që të mos vendoset korniza për etiketat individuale. Pra, zbatimi përfundimtar është

private func loadUI() {
        titleLabel?.removeFromSuperview()
        distanceLabel?.removeFromSuperview()

        let label = UILabel()
        label.font = titleViewFont
        label.numberOfLines = 0
        label.textColor = UIColor.white
        label.textAlignment = .center
        self.titleLabel = label

        distanceLabel = UILabel()
        distanceLabel.textColor = .cbPPGreen
        distanceLabel.font = subtitleViewFont
        distanceLabel.textAlignment = .center

        label.translatesAutoresizingMaskIntoConstraints = false
        distanceLabel.translatesAutoresizingMaskIntoConstraints = false

        if let annotation = annotation as? BikeStationAR {
            let title = annotation.address
            let subtitle = String(format: "%.2f km", annotation.distanceFromUser / 1000)

            let fontAttributeTitle = [NSAttributedString.Key.font: titleViewFont]
            let titleSize = title.size(withAttributes: fontAttributeTitle)

            let fontAttributeSubtitle = [NSAttributedString.Key.font: subtitleViewFont]
            let subtitleSize = annotation.address.size(withAttributes: fontAttributeSubtitle)

            titleLabel?.text = title
            distanceLabel?.text = subtitle

            let stackView = UIStackView(frame: CGRect(x: 0, y: 0, width: titleSize.width + 5, height: titleSize.height + subtitleSize.height + 5))
            stackView.axis = .vertical
            stackView.distribution = .fillProportionally
            stackView.alignment = .fill
            stackView.addArrangedSubview(titleLabel ?? UIView())
            stackView.addArrangedSubview(distanceLabel)

            frame = stackView.bounds
            addSubview(stackView)
        }
    }
27.10.2018

  • Mund të shfaqësh një pamje të ekranit se si duket kur ka defekt? 27.10.2018
  • Çfarë kuptoni me lëvizjen e telefonit? A pëlqen rrotullimi? 28.10.2018
  • Unë kam shtuar një video. 28.10.2018

Përgjigjet:


1

Në këtë mënyrë mund të merrni gjerësinë e teksteve të etiketës suaj:

let fontAttributes1= [NSAttributedStringKey.font: your declared font in your code]
   let size1 = (“your string” as String).size(withAttributes: fontAttributes1)
let fontAttributes2= [NSAttributedStringKey.font: your declared font in your code]
   let size2 = (“your string” as String).size(withAttributes: fontAttributes2)

Pastaj:

YourView.frame = CGRect( width: size1.width + size2.width + some spacing , height : ....)
28.10.2018
  • Ju jeni MVP i vërtetë! 28.10.2018
  • Nuk ka problem ???????? 29.10.2018
  • Materiale të reja

    Masterclass Coroutines: Kapitulli-3: Anulimi i korutinave dhe trajtimi i përjashtimeve.
    Mirë se vini në udhëzuesin gjithëpërfshirës mbi Kotlin Coroutines! Në këtë seri artikujsh, unë do t'ju çoj në një udhëtim magjepsës, duke filluar nga bazat dhe gradualisht duke u thelluar në..

    Faketojeni derisa ta arrini me të dhënat false
    A e gjeni ndonjëherë veten duke ndërtuar një aplikacion të ri dhe keni nevojë për të dhëna testimi që duken dhe duken më realiste ose një grup i madh të dhënash për performancën e ngarkesës...

    Si të përdorni kërkesën API në Python
    Kërkesë API në GitHub për të marrë depot e përdoruesve duke përdorur Python. Në këtë artikull, unë shpjegoj procesin hap pas hapi për të trajtuar një kërkesë API për të marrë të dhëna nga..

    Një udhëzues hap pas hapi për të zotëruar React
    Në këtë artikull, do të mësoni se si të krijoni aplikacionin React, do të mësoni se si funksionon React dhe konceptet thelbësore që duhet të dini për të ndërtuar aplikacione React. Learning..

    AI dhe Psikologjia — Pjesa 2
    Në pjesën 2 të serisë sonë të AI dhe Psikologji ne diskutojmë se si makineritë mbledhin dhe përpunojnë të dhëna për të mësuar emocione dhe ndjenja të ndryshme në mendjen e njeriut, duke ndihmuar..

    Esencialet e punës ditore të kodit tim VS
    Shtesat e mia të preferuara - Git Graph 💹 Kjo shtesë është vërtet e mahnitshme, e përdor përpara se të filloj të punoj për të kontrolluar dy herë ndryshimet dhe degët më të fundit, mund të..

    Pse Python? Zbulimi i fuqisë së gjithanshme të një gjiganti programues
    Në peizazhin gjithnjë në zhvillim të gjuhëve të programimit, Python është shfaqur si një forcë dominuese. Rritja e tij meteorike nuk është rastësi. Joshja e Python qëndron në thjeshtësinë,..