update/replace text in textview - swift

I have a TextField in the first view controller where the user enter his name. In the second view controller i Have a form in textview where the user entered details to be replaced the specific word in the specific place.
I cannot able to find the right guidance from the various searches, where I can only find to format the text/color, etc. My object is to replace the existing text with user filled data.
import UIKit
class SandyaViewController: UIViewController, UITextFieldDelegate {
func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if segue.identifier == "SViewController" {
if let nextVC = segue.destination as? SPViewController {
nextVC.yourName = self.nameTextField.text!
nextVC.address = self.addressTextField.text!
}
}
}
}
class SPViewController: UIViewController {
var yourName: String!
var address: String!
#IBOutlet weak var formTextView: UITextView!
myTextView.text = "This is to certify , that I yourname residing at home address....."
let attributedString = NSMutableAttributedString(string: "This is to certify , that I your name residing at home address .........\n")
let attributes1: [NSAttributedStringKey : Any] = [
NSAttributedStringKey.foregroundColor: UIColor(red: 153/255, green: 51/255, blue: 255/255, alpha: 1.0),
NSAttributedStringKey.font: UIFont(name: "HelveticaNeue-Bold", size: 15)!,
NSAttributedStringKey.backgroundColor: UIColor(red: 255/255, green: 255/255, blue: 0/255, alpha: 1.0)
]
attributedString.addAttributes(attributes1, range: NSMakeRange(28, 9))
let attributes3: [NSAttributedStringKey : Any] = [
NSAttributedStringKey.foregroundColor: UIColor(red: 230/255, green: 0/255, blue: 0/255, alpha: 1.0),
NSAttributedStringKey.backgroundColor: UIColor(red: 255/255, green: 255/255, blue: 0/255, alpha: 1.0)
]
attributedString.addAttributes(attributes3, range: NSMakeRange(51, 12))
Here I want the replace the attributes1 value with yourName and attributes3 value with address
myTextView.text = "This is to certify , that I attributes1 residing at home attributes3....."
I assume the I have detailed properly
Thanks

Related

Trouble Referencing UIButton using NSMutableAttributedString in Instantiated View Controller

As soon as my view controller loads, I am presented with a button (gray background with white font) that displays the text “Sto 1”. This is called in viewWillLayoutSubviews and the title is set using a NSMutableAttributedString. “Sto” is short for store.
For my application, I would like the user to be able to select the Sto 1 button and be able to store a number that is presented on a UILabel. I am able to grab the current number being displayed but I’m unable to update the text inside my Sto 1 button using NSMutableAttributedString. In other words I want to go from the button showing “Sto 1” to displaying some number (e.g., 12).
Thank you all for any help you may be able to provide me. I am still relatively new to Swift and I have been trying to resolve this issue over the past week.
import UIKit
class ViewController: UIViewController {
var fontConstant = CGFloat()
var someRandomNumberDisplayedOnAUILabel = String(12)
#IBOutlet weak var myButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillLayoutSubviews() {
fontConstant = 1
let myString = "Sto 1"
let myAttributes = [NSAttributedString.Key.foregroundColor : UIColor(red: 251/255.0, green: 251/255.0, blue: 251/255.0, alpha: 1.0), NSAttributedString.Key.font : UIFont.systemFont(ofSize: 10 * fontConstant)]
let mutableAttributedString = NSMutableAttributedString(string: myString, attributes: myAttributes)
myButton.setAttributedTitle(mutableAttributedString, for: .normal)
myButton.backgroundColor = UIColor(red: 94/255.0, green: 94/255.0, blue: 94/255.0, alpha: 1.0)
}
#IBAction func myButtonAction(_ sender: Any) {
let myAttributes = [NSAttributedString.Key.foregroundColor : UIColor(red: 0/255.0, green: 0/255.0, blue: 0/255.0, alpha: 1.0), NSAttributedString.Key.font : UIFont.systemFont(ofSize: 10 * fontConstant)]
let mutableAttributedString = NSMutableAttributedString(string: someRandomNumberDisplayedOnAUILabel, attributes: myAttributes)
myButton.setAttributedTitle(mutableAttributedString, for: .normal)
myButton.backgroundColor = UIColor(red: 251/255.0, green: 251/255.0, blue: 251/255.0, alpha: 1.0)
}}
Normally, you would use setTitle(:for:) to change the text on a UIButton. But since you're working with an NSMutableAttributedString you will need the setAttributedTitle(:for:) function. I think this might be what you're looking for:
myButton.setAttributedTitle(myNSMutableAttributedString, for: .normal)
Heads up, though. You might need to call this function for the different control states and not just .normal otherwise you might see different text for an instant as the button is highlighted. Here is a list of the control states.
EDIT:I would try referencing the sender in the IBAction instead of myButton. This might be a quick fix:
#IBAction func myButtonAction(_ sender: Any) {
let myAttributes = [NSAttributedString.Key.foregroundColor : UIColor(red: 0/255.0, green: 0/255.0, blue: 0/255.0, alpha: 1.0), NSAttributedString.Key.font : UIFont.systemFont(ofSize: 10 * fontConstant)]
let mutableAttributedString = NSMutableAttributedString(string: someRandomNumberDisplayedOnAUILabel, attributes: myAttributes)
guard let button = sender as? UIButton else {
print("Error: sender was not a button")
return
}
button.setAttributedTitle(mutableAttributedString, for: .normal)
button.backgroundColor = UIColor(red: 251/255.0, green: 251/255.0, blue: 251/255.0, alpha: 1.0)
}
EDIT #2:If you're losing the reference to your IBOutlet you might be able to work around that by assigning a selector to the button before you lose it. Try this:
override func viewDidLoad() {
super.viewDidLoad()
// Add the action to the button rather than holding on to the IBOutlet
myButton.addTarget(self, action: #selector(RideInProgressViewController.myAction(sender:)), for: .touchUpInside)
}
#objc private func myAction(sender: Any) {
guard let button = sender as? UIButton else {
print("Error: sender was not a button")
return
}
let myAttributes = [NSAttributedString.Key.foregroundColor : UIColor(red: 0/255.0, green: 0/255.0, blue: 0/255.0, alpha: 1.0), NSAttributedString.Key.font : UIFont.systemFont(ofSize: 10 * fontConstant)]
let mutableAttributedString = NSMutableAttributedString(string: someRandomNumberDisplayedOnAUILabel, attributes: myAttributes)
button.setAttributedTitle(mutableAttributedString, for: .normal)
button.backgroundColor = UIColor(red: 251/255.0, green: 251/255.0, blue: 251/255.0, alpha: 1.0)
}

Navigation Controller. Background color

For one controller I have settings:
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationController?.navigationBar.shadowImage = UIImage()
and
override func scrollViewDidScroll(_ scrollView: UIScrollView) {
var offset = scrollView.contentOffset.y / 150
if offset > 1 {
offset = 1
self.navigationController?.navigationBar.backgroundColor = UIColor(red: 82/255, green: 76/255, blue: 70/255, alpha: offset)
UIApplication.shared.statusBarView?.backgroundColor = UIColor(red: 82/255, green: 76/255, blue: 70/255, alpha: offset)
self.navigationItem.title = name
} else {
self.navigationController?.navigationBar.backgroundColor = UIColor(red: 82/255, green: 76/255, blue: 70/255, alpha: offset)
UIApplication.shared.statusBarView?.backgroundColor = UIColor(red: 82/255, green: 76/255, blue: 70/255, alpha: offset)
self.navigationItem.title = ""
}
}
The main problem is that when i press back button, settings save. In the end i have white NavigationController. How can I make the settings not taken from the last controller?
func makeSearchController() {
searchController = UISearchController(searchResultsController: nil)
navigationItem.searchController = searchController
navigationItem.hidesSearchBarWhenScrolling = false
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
searchController.searchBar.tintColor = .white
searchController.searchBar.placeholder = "Блюдо или продукт ..."
}
You can reset the navigation controller's color in viewWillDisappear, like this:
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
self.navigationController?.navigationBar.backgroundColor = UIColor(red: 221/255, green: 221/255, blue: 225/255, alpha: offset) //gray color
UIApplication.shared.statusBarView?.backgroundColor = UIColor(red: 221/255, green: 221/255, blue: 225/255, alpha: offset) //gray color
}

Issues creating toggle button

I am trying to create a button that reads "Special 1" then when it is clicked, the button reads "USED" and it stays as "USED" and doesn't allow the reader to toggle it back.
For example, the Twitter "follow" button can be pressed and then the button is changed to "following". However on Twitter, the user can click the "following" button and it will change back to "follow".
For my app, I want the user to be able to click "Special 1" and then the button is changed to "USED" and it remains that way forever.
Below is the attached code that I have created so far. The problem with my code so far is that the button doesn't read "Special 1" or "USED" when it is selected, the only thing it contains is the colors. The colors change how I want them to, but I can't get the words to appear and change.
import UIKit
class SpecialOneBTN: UIButton {
var isOn = false
override init(frame: CGRect) {
super.init(frame: frame)
initButton()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
initButton()
}
func initButton() {
layer.borderWidth = 2.0
layer.cornerRadius = frame.size.height/2
setTitleColor(UIColor(red: 255.0/255.0, green: 193.0/255.0, blue: 95.0/255.0, alpha: 1.0), for: .normal)
addTarget(self, action: #selector(SpecialOneBTN.buttonPressed), for: .touchUpInside )
}
#objc func buttonPressed() {
activateButton(bool: !isOn)
}
func activateButton(bool: Bool) {
isOn = bool
let color = bool ? UIColor(red: 102.0/255.0, green: 102.0/255.0, blue: 100.0/255.0, alpha: 1.0) : UIColor(red: 255.0/255.0, green: 193.0/255.0, blue: 95.0/255.0, alpha: 1.0)
let titleColor = bool ? UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 0.0) : UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 0.0)
let title = bool ? "USED" : "Special 1"
setTitle(title, for: .normal)
setTitleColor(titleColor, for: .normal)
backgroundColor = color
}
}

Can't trigger action of a UIButton after modifying its UI

I create a UIButton which is displayed inside a XIB. But when I add a modification for the iPhone X display, I can't trigger the method inside its action. Here is the code below:
#IBOutlet weak var tryAgainButton: UIButton!
#IBAction func tryAgainButtonPressed(_ sender: UIButton) {
myFunction()
}
if UIDevice().userInterfaceIdiom == .phone, // Iphone X
UIScreen.main.nativeBounds.height == 2436 {
label.font = label.font.withSize(12)
contentView.snp.makeConstraints { make in
make.centerY.equalToSuperview()
make.top.equalToSuperview().offset(-20)
containerView.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
contentView.backgroundColor? = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
containerView.backgroundColor? = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
label.textColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 1)
helpButton.setTitleColor(#colorLiteral(red: 0, green: 0, blue: 0, alpha: 1), for: .normal)
tryAgainButton.setTitleColor(#colorLiteral(red: 0, green: 0, blue: 0, alpha: 1), for: .normal)
let underlineAttributes : [NSAttributedStringKey: Any] = [
NSAttributedStringKey.underlineStyle : NSUnderlineStyle.styleSingle.rawValue,
NSAttributedStringKey.foregroundColor : #colorLiteral(red: 0, green: 0, blue: 0, alpha: 1)
]
let helpButtonAttributeString = NSMutableAttributedString(string: "Help",
attributes: underlineAttributes)
let tryAgainButtonAttributeString = NSMutableAttributedString(string: "Try Again",
attributes: underlineAttributes)
helpButton.setAttributedTitle(helpButtonAttributeString, for: .normal)
tryAgainButton.setAttributedTitle(tryAgainButtonAttributeString, for: .normal)
}
}
myFunction() is not reachable when I run with an Iphone X.

Set UIColor in storyboard from variable/constant [duplicate]

This question already has answers here:
How to change dynamically color used in UIStoryboard
(2 answers)
Closed 4 years ago.
How do I set, let's say a text color on a UILabel, to a color defined in one of my classes like:
func mainAppColor() -> UIColor {
return UIColor(red: 0.2, green: 0.3, blue: 0.4, alpha: 1)
}
or
let mainAppColor = UIColor(red: 0.2, green: 0.3, blue: 0.4, alpha: 1)
in storyboard.
If your method is declared in some other class like:
class TextColorClass {
func mainAppColor() -> UIColor {
return UIColor(red: 0.2, green: 0.3, blue: 0.4, alpha: 1)
}
}
Then you can use it by make instance of that class this way:
let classInstance = TextColorClass()
yourLBL.textColor = classInstance.mainAppColor()
If it is declared like:
class TextColorClass {
let mainAppColor = UIColor(red: 0.2, green: 0.3, blue: 0.4, alpha: 1)
}
Then you can use it like:
let classInstance = TextColorClass()
yourLBL.textColor = classInstance.mainAppColor
If that method is declare in same class suppose in ViewController class then you can use it this way:
override func viewDidLoad() {
super.viewDidLoad()
yourLBL.textColor = mainAppColor()
}
func mainAppColor() -> UIColor {
return UIColor(red: 0.2, green: 0.3, blue: 0.4, alpha: 1)
}
UPDATE:
If you want to set any property from storyboard you can use #IBInspectable as shown in below example code:
import UIKit
#IBDesignable
class PushButtonView: UIButton {
#IBInspectable var fillColor: UIColor = UIColor.greenColor()
#IBInspectable var isAddButton: Bool = true
override func drawRect(rect: CGRect) {
}
}
And assign this class to your button.like shown into below Image:
And if you go to the Attribute Inspector you can see custom property for that button.
Now you can set it as per your need.
For more Info refer this tutorial:
http://www.raywenderlich.com/90690/modern-core-graphics-with-swift-part-1