Change selected row label color in picker view swift 1.2
you could do something like the following:
class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
var colors = ["red","green","blue"]
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return colors.count
}
func pickerView(pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
let color = (row == pickerView.selectedRowInComponent(component)) ? UIColor.orangeColor() : UIColor.blackColor()
return NSAttributedString(string: colors[row], attributes: [NSForegroundColorAttributeName: color])
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
pickerView.reloadAllComponents()
}
}
Animate background color of selected UIPickerView Row in Swift
A slightly different approach since I couldn't find a smooth way to animate background on selected row. It can be done but not much of an improvement, so give this a try:
override func viewDidLoad() {
//...
/*
Create a colored `view` that stays bang in the center on top
of the `pickerView`.
The `pickerView` will scroll behind it normally and no need
for animating background color or even reloading
*/
createHighlightView()
}
func createHighlightView() {
let highlightView = UIView(frame: CGRect.zero)
highlightView.backgroundColor = UIColor.red.withAlphaComponent(0.2)
/*
Now lets programmatically add constraints
*/
highlightView.translatesAutoresizingMaskIntoConstraints = false
pickerView.addSubview(highlightView)
//HightLight View's width
highlightView.addConstraint(NSLayoutConstraint(item: highlightView,
attribute: .width,
relatedBy: .equal,
toItem: nil,
attribute: .notAnAttribute,
multiplier: 1,
constant: width))
//HightLight View's height
highlightView.addConstraint(NSLayoutConstraint(item: highlightView,
attribute: .height,
relatedBy: .equal,
toItem: nil,
attribute: .notAnAttribute,
multiplier: 1,
constant: height))
//HightLight View should be bang center-aligned with pickerView
pickerView.addConstraint(NSLayoutConstraint(item: highlightView,
attribute: .centerX,
relatedBy: .equal,
toItem: pickerView,
attribute: .centerX,
multiplier: 1,
constant: 0))
pickerView.addConstraint(NSLayoutConstraint(item: highlightView,
attribute: .centerY,
relatedBy: .equal,
toItem: pickerView,
attribute: .centerY,
multiplier: 1,
constant: 0))
}
And now your delegates can be simply:
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
//no need to reload
//do whatever else you want
}
func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
/*
Just return a `UILabel`. No need to put it in a `UIView`.
Nothing special either, just slap text into it
*/
var label = view as? UILabel
if label == nil {
label = UILabel()
//All the rest are safe force unwraps so chill tf out
label!.frame = CGRect(x: 0, y: 0, width: height, height: height)
label!.textAlignment = .center
label!.font = UIFont.systemFont(ofSize: 30)
label!.transform = CGAffineTransform(rotationAngle: 90 * (.pi/180))
}
label!.text = arrDatasource[row]
return label!
}
Changing the color of a few selected table view cells text in UITableView
First, your code in its current state sets the color of all the cells whose indexes are greater than 4 to blue
, not only the last 4 cells. You need to change your condition.
And after that, you need to add an else statement to handle other cells color, the cells who do not fulfill the condition. This is because the cells are reused, so they always need to have their color set.
So your code should look something like this:
if indexPath.row > numberOfRows - 4 {
cell.label.textColor = .blue
} else {
cell.label.textColor = .black
}
Or as mentioned by @jarvis, you can use ternary operator to make your code more compact, like this:
cell.label.textColor = indexPath.row > numberOfRows - 4 ? .blue : .black
numberOfRows
is the number of rows the table view has.
Formatting alignment of uipickerview
This should get you started:
func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView!) -> UIView {
let pickerLabel = UILabel()
let titleData = pickerData[row]
let myTitle = NSAttributedString(string: titleData, attributes: [NSFontAttributeName:UIFont(name: "Georgia", size: 26.0)!,NSForegroundColorAttributeName:UIColor.blackColor()])
pickerLabel.attributedText = myTitle
return pickerLabel
}
and this:
func pickerView(pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
let titleData = pickerData[row]
var myTitle = NSAttributedString(string: titleData, attributes: [NSFontAttributeName:UIFont(name: "Georgia", size: 15.0)!,NSForegroundColorAttributeName:UIColor.blueColor()])
return myTitle
}
For the Attributed string, this is where you can modified the text attributes to fit what you want, you can set up something like this, you'll have to figure out how to place the Attributed string, but it's very simple using the second method above:
var myString1 = NSMutableAttributedString(string:"$45.45")
let myString1Font1 = UIFont(name:"AvenirNext-Regular", size:24.0)
let myString1Color1 = UIColor(red: 0.292745, green: 0.461693, blue: 0.998524, alpha: 1.000000)
let originalNSString = myString1.string as NSString
let myString1Range1 = originalNSString.rangeOfString("$45.45")
var myString1ParaStyle1 = NSMutableParagraphStyle()
myString1ParaStyle1.alignment = NSTextAlignment.Right
myString1ParaStyle1.baseWritingDirection = NSWritingDirection.Natural
myString1ParaStyle1.defaultTabInterval = 0
myString1ParaStyle1.firstLineHeadIndent = 0
myString1ParaStyle1.headIndent = 0
myString1ParaStyle1.hyphenationFactor = 0
myString1ParaStyle1.lineBreakMode = NSLineBreakMode.ByWordWrapping
myString1ParaStyle1.lineHeightMultiple = 0
myString1ParaStyle1.lineSpacing = 0
myString1ParaStyle1.maximumLineHeight = 0
myString1ParaStyle1.minimumLineHeight = 0
myString1ParaStyle1.paragraphSpacing = 0
myString1ParaStyle1.paragraphSpacingBefore = 0
myString1ParaStyle1.tailIndent = 0
myString1.addAttribute(NSUnderlineColorAttributeName, value:myString1Color1, range:myString1Range1)
myString1.addAttribute(NSParagraphStyleAttributeName, value:myString1ParaStyle1, range:myString1Range1)
myString1.addAttribute(NSFontAttributeName, value:myString1Font1!, range:myString1Range1)
This seems to work:
func pickerView(pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
let titleData = pickerData[row]
var myString1 = NSMutableAttributedString(string:titleData)
let myString1Font1 = UIFont(name:"AvenirNext-Regular", size:24.0)
let myString1Color1 = UIColor(red: 0.292745, green: 0.461693, blue: 0.998524, alpha: 1.000000)
let originalNSString = myString1.string as NSString
let myString1Range1 = originalNSString.rangeOfString(titleData)
var myString1ParaStyle1 = NSMutableParagraphStyle()
myString1ParaStyle1.baseWritingDirection = NSWritingDirection.Natural
myString1ParaStyle1.lineBreakMode = NSLineBreakMode.ByWordWrapping
myString1.addAttribute(NSUnderlineColorAttributeName, value:myString1Color1, range:myString1Range1)
myString1.addAttribute(NSParagraphStyleAttributeName, value:myString1ParaStyle1, range:myString1Range1)
myString1.addAttribute(NSFontAttributeName, value:myString1Font1!, range:myString1Range1)
if pickerView == myPickerf {
myString1ParaStyle1.alignment = NSTextAlignment.Left
} else if pickerView == myPicker {
myString1ParaStyle1.alignment = NSTextAlignment.Right
}
return myString1
}
func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView!) -> UIView {
var pickerLabel = view as! UILabel!
if view == nil {
pickerLabel = UILabel()
let hue = CGFloat(row)/CGFloat(pickerData.count)
pickerLabel.backgroundColor = UIColor(hue: hue, saturation: 1.0, brightness:1.0, alpha: 1.0)
}
let titleData = pickerData[row]
var myString1 = NSMutableAttributedString(string:titleData)
let myString1Font1 = UIFont(name:"AvenirNext-Regular", size:24.0)
let myString1Color1 = UIColor(red: 0.292745, green: 0.461693, blue: 0.998524, alpha: 1.000000)
let originalNSString = myString1.string as NSString
let myString1Range1 = originalNSString.rangeOfString(titleData)
var myString1ParaStyle1 = NSMutableParagraphStyle()
myString1ParaStyle1.baseWritingDirection = NSWritingDirection.Natural
myString1ParaStyle1.lineBreakMode = NSLineBreakMode.ByWordWrapping
myString1.addAttribute(NSUnderlineColorAttributeName, value:myString1Color1, range:myString1Range1)
myString1.addAttribute(NSParagraphStyleAttributeName, value:myString1ParaStyle1, range:myString1Range1)
myString1.addAttribute(NSFontAttributeName, value:myString1Font1!, range:myString1Range1)
pickerLabel!.attributedText = myString1
if pickerView == myPickerf {
myString1ParaStyle1.alignment = NSTextAlignment.Left
} else if pickerView == myPicker {
myString1ParaStyle1.alignment = NSTextAlignment.Right
}
return pickerLabel
}
Here's your code reformatted, this should work:
import Foundation
import UIKit
class LocationViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
@IBOutlet weak var DisplayStartMileage: UITextField!
@IBOutlet weak var labelFuelAmount: UILabel!
@IBOutlet weak var spinFuelAmount: UIPickerView!
var mileageToPass: String!
var fuelAmount: Double = 0.00
var pickerData = [[String]]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
var poundValues = [String]()
var penceValues = [String]()
for var indexP:Int = 0; indexP < 100; indexP += 1 {
poundValues.append("£ \(indexP)")
penceValues.append(NSString(format: ".%02d", indexP) as String)
}
self.pickerData = [poundValues, penceValues]
spinFuelAmount.delegate = self
spinFuelAmount.dataSource = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func FuelSegmentChanged(sender: UISegmentedControl) {
switch sender.selectedSegmentIndex
{
case 0:
labelFuelAmount.hidden = false
spinFuelAmount.hidden = false
case 1:
labelFuelAmount.hidden = true
spinFuelAmount.hidden = true
default:
break;
}
}
func numberOfComponentsInPickerView(spinFuelAmount: UIPickerView) -> Int {
return pickerData.count
}
func pickerView(spinFuelAmount: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return pickerData[component].count
}
func pickerView(spinFuelAmount: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
return pickerData[component][row]
}
func pickerView(spinFuelAmount: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
var firstComponent = pickerData[0][spinFuelAmount.selectedRowInComponent(0)]
firstComponent = firstComponent.stringByReplacingOccurrencesOfString( "£ ", withString: "" )
var secondComponent = pickerData[1][spinFuelAmount.selectedRowInComponent(1)]
secondComponent = secondComponent.stringByReplacingOccurrencesOfString( ".", withString: "" )
if let firstValue = firstComponent.toInt(), let secondValue = secondComponent.toInt() {
let fuelAmount = firstValue + secondValue / 100
}
}
func pickerView(spinFuelAmount: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
let titleData = pickerData[component][row] //this is bad, you'll have to compute what to do about this, but you get the point
var myString1 = NSMutableAttributedString(string:titleData)
let myString1Font1 = UIFont(name:"AvenirNext-Regular", size:24.0)
let myString1Color1 = UIColor(red: 0.292745, green: 0.461693, blue: 0.998524, alpha: 1.000000)
let originalNSString = myString1.string as NSString
let myString1Range1 = originalNSString.rangeOfString(titleData)
var myString1ParaStyle1 = NSMutableParagraphStyle()
myString1ParaStyle1.baseWritingDirection = NSWritingDirection.Natural
myString1ParaStyle1.lineBreakMode = NSLineBreakMode.ByWordWrapping
myString1.addAttribute(NSUnderlineColorAttributeName, value:myString1Color1, range:myString1Range1)
myString1.addAttribute(NSParagraphStyleAttributeName, value:myString1ParaStyle1, range:myString1Range1)
myString1.addAttribute(NSFontAttributeName, value:myString1Font1!, range:myString1Range1)
if component == 0 {
myString1ParaStyle1.alignment = NSTextAlignment.Left
} else if component == 1 {
myString1ParaStyle1.alignment = NSTextAlignment.Right
}
return myString1
}
func pickerView(spinFuelAmount: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView!) -> UIView {
var pickerLabel = view as! UILabel!
if view == nil {
pickerLabel = UILabel()
let hue = CGFloat(row)/CGFloat(pickerData.count)
pickerLabel.backgroundColor = UIColor(hue: hue, saturation: 1.0, brightness:1.0, alpha: 1.0)
}
let titleData = pickerData[component][row]
var myString1 = NSMutableAttributedString(string:titleData)
let myString1Font1 = UIFont(name:"AvenirNext-Regular", size:24.0)
let myString1Color1 = UIColor(red: 0.292745, green: 0.461693, blue: 0.998524, alpha: 1.000000)
let originalNSString = myString1.string as NSString
let myString1Range1 = originalNSString.rangeOfString(titleData)
var myString1ParaStyle1 = NSMutableParagraphStyle()
myString1ParaStyle1.baseWritingDirection = NSWritingDirection.Natural
myString1ParaStyle1.lineBreakMode = NSLineBreakMode.ByWordWrapping
myString1.addAttribute(NSUnderlineColorAttributeName, value:myString1Color1, range:myString1Range1)
myString1.addAttribute(NSParagraphStyleAttributeName, value:myString1ParaStyle1, range:myString1Range1)
myString1.addAttribute(NSFontAttributeName, value:myString1Font1!, range:myString1Range1)
pickerLabel!.attributedText = myString1
if component == 0 {
myString1ParaStyle1.alignment = NSTextAlignment.Left
} else if component == 1 {
myString1ParaStyle1.alignment = NSTextAlignment.Right
}
return pickerLabel
}
}
You will need to check my work and make sure that when THIS is called:
let titleData = pickerData[component][row]
that it's actually pointing to the correct dictionary item which is either Pounds or Pense. I actually looked at your code, more and I see that you had pickerData[component][row] already, so I reformated and then used these for the mutableatributed string callers. see the revised code which is really your code but with the correction
Is there a way to get/set standard UITableViewCell textLabel from a custom method?
You should keep (or pass) a reference to the "currently selected cell". Then you can easily access its text label by simply writing:
// assuming the selected cell is kept in a property by name selectedTableViewCell
UILabel * tempLabel = self.selectedTableViewCell.textLabel;
In case you're wondering how to keep such a reference, you should implement the
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
method, and in it, simply state:
self.selectedTableViewCell = [tableView cellForRowAtIndexPath:indexPath];
This way, when the user taps the cell, it will be set as the "selected cell".
Another way to fly would be by adding the reference to the cell you want to edit the text from in the IBAction
as a parameter, like this:
-(IBAction)getTextLabelOfUITableViewCell:(UITableViewCell *)editMe;
And then you could write something like:
UILabel * tempLabel = editMe.textLabel;
You pretty much can do whatever you want with the label from there on.
Hope this helps!
Related Topics
Opening Viewcontroller in Appdelegate While Keeping Tabbar
How to Change How a Remote Notification Is Presented Before Presentation
iOS 10.3 - How to Change App Icon Programmatically
Get Light or Dark Variant of a Color Declared in Assets
Swift: Obtain and Save the Updated Scnnode Over Time Using Projectpoint in Scenekit
How to Update Particular Value of Child in Firebase Db
Scaling Physics Bodies in Xcode Spritekit
Longer Subtitles in Mapview Annotations (Swift)
How to Make App Wait Until Firebase Request Is Finished
Uitextfield Starting Cursor Position Is Wrong
Programmatically Auto-Connect to Wifi iOS
Wkwebview Decidepolicyfornavigationaction Being Call After the Long Press Recogniser Ended
How to Work with Udp Sockets in iOS, Swift
Swift - How to Open Another Viewcontroller with Collectionviewcell Inside Uitableviewcell