Swift: UIPickerView: Get the Columns Width plus Set Columns Label

The following swift code will give show you how to obtain the width from the first column plus how to set up all the columns’ label

func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView!) -> UIView {
       
 
        // Display the first column's width
        // The value 0 is to point to the first column regardless if you have one or multiple columns
        NSLog("Width: \(pickerView.rowSizeForComponent(0).width)")
        
        let pickerLabel : UILabel = UILabel()
       
       // pickerViewLabelTextForComponentRow is a custom made function to obtain what text will go in each row  
        pickerLabel.text = pickerViewLabelTextForComponentRow(component, row: row)
        pickerLabel.textAlignment = NSTextAlignment.Center
        pickerLabel.textColor = UIColor.blackColor()
        pickerLabel.font = UIFont.boldSystemFontOfSize(13.0)
        pickerLabel.backgroundColor = UIColor.lightGrayColor()

       // If you wish to set up the labels individually in each column, use the value of the parameter component with a control condition such as if or switch
        
        return pickerLabel
    }

© 2015, Alejandro G. Carlstein Ramos Mejia. All rights reserved.

Share

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.

*

Click to Insert Smiley

SmileBig SmileGrinLaughFrownBig FrownCryNeutralWinkKissRazzChicCoolAngryReally AngryConfusedQuestionThinkingPainShockYesNoLOLSillyBeautyLashesCuteShyBlushKissedIn LoveDroolGiggleSnickerHeh!SmirkWiltWeepIDKStruggleSide FrownDazedHypnotizedSweatEek!Roll EyesSarcasmDisdainSmugMoney MouthFoot in MouthShut MouthQuietShameBeat UpMeanEvil GrinGrit TeethShoutPissed OffReally PissedMad RazzDrunken RazzSickYawnSleepyDanceClapJumpHandshakeHigh FiveHug LeftHug RightKiss BlowKissingByeGo AwayCall MeOn the PhoneSecretMeetingWavingStopTime OutTalk to the HandLoserLyingDOH!Fingers CrossedWaitingSuspenseTremblePrayWorshipStarvingEatVictoryCurseAlienAngelClownCowboyCyclopsDevilDoctorFemale FighterMale FighterMohawkMusicNerdPartyPirateSkywalkerSnowmanSoldierVampireZombie KillerGhostSkeletonBunnyCatCat 2ChickChickenChicken 2CowCow 2DogDog 2DuckGoatHippoKoalaLionMonkeyMonkey 2MousePandaPigPig 2SheepSheep 2ReindeerSnailTigerTurtleBeerDrinkLiquorCoffeeCakePizzaWatermelonBowlPlateCanFemaleMaleHeartBroken HeartRoseDead RosePeaceYin YangUS FlagMoonStarSunCloudyRainThunderUmbrellaRainbowMusic NoteAirplaneCarIslandAnnouncebrbMailCellPhoneCameraFilmTVClockLampSearchCoinsComputerConsolePresentSoccerCloverPumpkinBombHammerKnifeHandcuffsPillPoopCigarette