I would like all UIPickerViews in an app to have white text. I can make it work fine for an individual pickerview, but I would like to have it apply to all occurrences.
I got this far...
extension UIPickerView {
func pickerView(pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
let titleData = ?????
let colorTitle = NSAttributedString(string: titleData, attributes: [NSForegroundColorAttributeName:UIColor.whiteColor()])
}
However, I don't want to actually change the text... just the color. How can I grab the title (text) for the specified row so that I can returned it as an NSAttributedString?
Thanks.
I ended up creating a subclass of UIDatePicker and overriding the addSubView method. The code below is working well for me.
class ColoredDatePicker: UIDatePicker {
var changed = false
override func addSubview(view: UIView) {
if !changed {
changed = true
self.setValue(UIColor.whiteColor(), forKey: "textColor")
}
super.addSubview(view)
}
}
Related
Is there a possibility to get UITextField from UIPickerView? I set my UIPickerView as inputView for text filed. I have few text fields on the screen and I want to simple get current text field that shown picker view.
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
{
// something like let textField = pickerView.requestedTextField
}
Is there any way to do it?
If you have multiple textField that show pickerView then create one more instance of textField in your viewController like this and use this object inside textFieldDidBeginEditing and assign the reference of that textField to that tempTextField.
var selectedTextField: UITextField = UITextField()
func textFieldDidBeginEditing(textField: UITextField!) {
self.selectedTextField = textField
}
Now in didSelectRow method of PickerView use this textField o know the current editing field.
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if self.selectedTextField == countryField {
//Set text for countryField
}
else self.selectedTextField == stateField {
//Set text for stateField
}
}
Note: For example I have used countryField and stateField you can use textField for that you want to set text.
Set same tag to your pickerview and textfield both.
now you can get your tag in didSelectRow like,
let myTag = pickerView.tag
and then from myTag you can get your textfield something like,
let myTextField = self.view.viewWithTag(myTag) as! UITextField
As user Lion rightly pointed out in his answer, you can get the corresponding UITextField by giving both the pickerView and textField a tag. However, the issue with giving them the same tag is that viewWithTag may return the UIPickerView itself, depending on the order of the subviews.
As this is unreliable, a better solution would be to give the UITextField the negative of the tag given to the UIPickerView (eg. 1 and -1). To get the UITextView, you would do something like this in your method, once you have assigned your picker views and text views opposite tags:
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
let textField = view.viewWithTag(-pickerView.tag) as! UITextField
//Use textField
}
I'm trying to get the first row data for pickerView.
I'm getting first data when I'm back the pickerView page, but I'm getting the wrong row on the screen. This is how I'm getting the first row :
pickerView(myPickerView!, didSelectRow: 0, inComponent: 0)
I'm using this code in viewDidLoad. How can I show the first row on the screen?
u may use the func pickerView(pickerView:
UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView?) -> UIView {
have a look at this article
please use this code to select a particular row,
let picker = UIPickerView()
picker.selectRow(0, inComponent: 0, animated: true)
I give up to get first picker row data when I come back the page. I think it should be the selected row data I should get. I combined your solutions and find the solve.
Working code :
func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView?) -> UIView {
let pickerLabel = UILabel()
let titleData = newuser!.valueForKey("name").objectAtIndex(row) as! String
let myTitle = NSAttributedString(string: titleData, attributes: [NSFontAttributeName:UIFont(name: "Times New Roman", size: 25.0)!,NSForegroundColorAttributeName:UIColor.whiteColor()])
pickerLabel.attributedText = myTitle
pickerLabel.textAlignment = .Center
selectedLog = (newuser?.valueForKey("name").objectAtIndex(row))! as! String
return pickerLabel
}
This viewForRow function provides me to get selected row data when I come back the pickerView page. selectedLog is my string variable. I show and save the selected row data from here. titleData variable provides me to get selected data. That's it! Thanks all of you.
Attempting to right/left align the text in two components in a UIPickerView. It's being displayed correctly inside of the selected zone, but the text is overlapping elsewhere.
Any idea what I'm doing wrong here?
func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView?) -> UIView {
let pickerLabel = UILabel()
var titleData: String!
if component == 0 {
if pickerView.tag == 0 {
titleData = weightData[row].description
} else {
titleData = bodyFatData[row].description
}
pickerLabel.textAlignment = .Right
} else {
titleData = decimalData[row].description
pickerLabel.textAlignment = .Left
}
let myTitle = NSAttributedString(string: titleData, attributes: [NSFontAttributeName:UIFont.systemFontOfSize(22.0),NSForegroundColorAttributeName:
UIColor.blackColor()])
pickerLabel.attributedText = myTitle
return pickerLabel
}
EDIT:
When I add this line
pickerLabel.frame.size = CGSizeMake(165, 26)
It does have an effect, but the selected/unselected rows are still misaligned.
I think you are simply overlooking the possibilities of this function:
// Swift
func pickerView(pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat {
return CGFloat(50.0)
}
By default the components will center align, and it appears the whole picker will also center align. If you omit the textAlignment, all the values in a component (column) will line up.
I've a many textfield and a pickerView with toolBar and it has a done button. My issue is I can't select the first row from the picker. While I have debugged in the didSelectRow but it won't run inside it. So please where would be my issue?
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
var value = currentPickerArray[row]
textFieldOutletArray[currentTag].text = value
}
Just replace didSelect method in your sample code like as bellowed. it will working fine.
#IBAction func doneBtn(sender: AnyObject) {
var row = pickerView.selectedRowInComponent(0);
NSLog("value L %d", row)
pickerView(pickerView, didSelectRow: row, inComponent:0)
}
Hope this help you.
In the titleForRow function add this line of code. It shows first row selected:
textField.text = pickerArray.objectAtIndex(row).objectForKey("Name") as? String
I am currently working on a small project and i have a viewController that has 4 textFields which 3 work ok. They take String objects. However, the 4th textField is supposed to bring up a UIPickerView with 4 selectable items.
So far this is what i have in my controller that implements this:
#IBOutlet var pickerTextfield: UITextField!
#IBOutlet var itemPicker: UIPickerView! = UIPickerView()
The pickerTextfield is the UITextField object that is the 4th field.
The itemPicker is an unlinked UIPickerView that i want to create programatically.
Right below these properties, i have an array of items for the UIPickerView object:
var seasonalItems = ["Spring", "Summer", "Fall", "Winter"]
In my viewDidLoad method i have this as follow:
itemPicker.hidden = true;
pickerTextfield.text = seasonalItems[0]
pickerTextfield.delegate = self
And the rest of the implementation:
// Below these lines is the implementation of the Picker
func numberOfComponentsInPickerView(pickerView: UIPickerView!) -> Int{
return 1
}
// returns the # of rows in each component..
func pickerView(pickerView: UIPickerView!, numberOfRowsInComponent component: Int) -> Int{
return seasonalItems.count
}
func pickerView(pickerView: UIPickerView!, titleForRow row: Int, forComponent component: Int) -> String! {
return seasonalItems[row]
}
func pickerView(pickerView: UIPickerView!, didSelectRow row: Int, inComponent component: Int)
{
pickerTextfield.text = seasonalItems[row]
itemPicker.hidden = true;
}
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
itemPicker.hidden = false
return false
}
So the end result from this is when i tap the pickerTextfield object in the app, it shows the first item of the array (Spring) but in text within the UITextField object but it does not show the UIPickerView object with the other selectable items where i could select one and then hide it when selected.
My question is, where or what am i doing wrong here? i been trying to figure this out on my own but i do not seem to get good clear examples with Swift and storyboards. I much rather not drag a UIPickerView in the storyboard but rather the way i attempted to implement. Thanks
You can give UIPickerView as inputView for your TextField in which you want to show picker view.
You also do not need to initially hide picker view in this case.
pickerTextfield.inputView = itemPicker
When you use UIPickerView as inputView of any UITextField then when you tap on the TextField instead of default keypad PickerView will show.