I just want an idea to make something like this.Suppose there is an UILabel string #"don't late." but when it will display it will show like this #"don' tlate.", Now user will rearrange those characters by dragging and dropping and make it correct word.
Now anyone can give me any idea or sample of example that how can i do that in iPhone application development?.
Thanks in Advance
I've created a sample project on my github account.
I think raywenderlich's gesture tutorial may help you.
Below is the most relevant code
// MARK: Actions
extension ViewController {
#IBAction func moveLabel(gesture:UIPanGestureRecognizer) {
if let labelBeingMoved = labelBeingMoved {
labelBeingMoved.center = gesture.locationInView(labelContainer)
}
}
}
// MARK: UIGestureRecognizerDelegate
extension ViewController : UIGestureRecognizerDelegate {
func gestureRecognizerShouldBegin(gesture: UIGestureRecognizer) -> Bool {
let location = gesture.locationInView(labelContainer)
//You'll have to change this to account for labels on top of one another
for label in labels {
if label.frame.contains(location) {
labelBeingMoved = label
labelContainer.bringSubviewToFront(labelBeingMoved!)
return true
}
}
return false
}
}
Related
I am new in Ios and i am having hard time wrapping my head around protocols and delegates concept. I am implementing a library called BEMCheckBox https://github.com/Boris-Em/BEMCheckBox or https://cocoapods.org/pods/BEMCheckBox for implementing radio buttons. Its documentation is pretty descriptive using which i have successfully added checkboxes, grouped them together to work as radio boxes.
#IBOutlet var inarelashipcb: BEMCheckBox!
#IBOutlet var complicatedcb: BEMCheckBox!
#IBOutlet var singlecb: BEMCheckBox!
var groupbx:BEMCheckBoxGroup!
func initialize(){
groupbx = BEMCheckBoxGroup(checkBoxes: [inarelashipcb,
complicatedcb, singlecb])
groupbx.selectedCheckBox = singlecb
groupbx.mustHaveSelection = true
}
Now i want to use didTapCheckBox method but i donot understand how. The documentation is blurry there no snippets for that. For the record this is what the documentation states
"BEMCheckBox uses a delegate to receive check box events. The delegate object must conform to the BEMCheckBoxDelegate protocol, which is composed of two optional methods:
didTapCheckBox:
Sent to the delegate every time the check box gets tapped, after its properties are updated (on), but before the animations are completed."
Any snippets to help me use delgate so i can implement didTapCheckBox method?
func initialize(){
groupbx = BEMCheckBoxGroup(checkBoxes: [inarelashipcb,
complicatedcb, singlecb])
groupbx.selectedCheckBox = singlecb
groupbx.mustHaveSelection = true
for checkbox in groupbx {
checkbox.delegate = self
}
}
must call initialize in viewDidLoad of the viewController
override func viewDidLoad() {
// Do your work
initialize()
}
compiler will show you an error, error will be gone if you add this codes
extension ViewController : BEMCheckBoxDelegate {
func didTap(_ checkBox: BEMCheckBox) {
//do your work
// if you have multiple checkboxes, then do like that
//if checkBox == checkBox1 {
//do work for checkbox1
//} else if {
// ..
//}
}
}
Don't forget to add the following line
import BEMCheckBox
The outlet checkbox you have declared, set delegate to self.
For eg, if your checkbox outlet is checkbox1 set its delegate as
checkbox1.delegate = self
I solve it.
using this code
func initialize(){
groupbx = BEMCheckBoxGroup(checkBoxes: [inarelashipcb,
complicatedcb, singlecb])
groupbx.selectedCheckBox = singlecb
groupbx.mustHaveSelection = true
inarelashipcb.delegate = self
complicatedcb.delegate = self
singlecb.delegate = self
}
func didTap(_ checkBox: BEMCheckBox) {
print("here hello")
}
Also my uiviewcontroller inherited from BEMCheckBoxDelegate
thanks for all help:)! fixed it using iboutlet collection and add properies on viewDidLoad
I'm trying to add properties to keyboard keys like layer.shadowColor or layer.shadowRadius.
I got an error
'Value of type '(UIButton)' -> () has no member 'layer'
how to fix this ?
this is my code keyboardViewController.swift
import UIKit
class KeyboardViewController: UIInputViewController {
var newKeyboardView: UIView!
#IBAction func keyPressed(sender: UIButton) {
}
#IBOutlet var nextKeyboardButton: UIButton!
override func updateViewConstraints() {
super.updateViewConstraints()
// Add custom view sizing constraints here
}
override func viewDidLoad() {
super.viewDidLoad()
loadInterface()
}
func loadInterface() {
// load the nib file
let keyboardNib = UINib(nibName: "newKeyboard", bundle: nil)
// instantiate the view
newKeyboardView = keyboardNib.instantiateWithOwner(self, options: nil)[0] as! UIView
// add the interface to the main view
view.addSubview(newKeyboardView)
// copy the background color
view.backgroundColor = newKeyboardView.backgroundColor
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated
}
override func textWillChange(textInput: UITextInput?) {
// The app is about to change the document's contents. Perform any preparation here.
}
override func textDidChange(textInput: UITextInput?) {
// The app has just changed the document's contents, the document context has been updated.
var textColor: UIColor
let proxy = self.textDocumentProxy
if proxy.keyboardAppearance == UIKeyboardAppearance.Dark {
textColor = UIColor.whiteColor()
} else {
textColor = UIColor.blackColor()
}
self.nextKeyboardButton.setTitleColor(textColor, forState: .Normal)
}
}
I think that in order to apply some style to the button, you need an outlet to this button.
Right now, from what I can understand, you are trying to apply styles to the button from the #IBAction to the sender, which is not the proper way to do it.
Try to make an outlet to the button in the view controller and then to apply the styles from within the viewDidLoad method.
I hope this is clear, but if you want a more specific answer you need to show us what you tried, for example pasting the code you have in the view controller
EDIT:
Based on the code you post, the keyboard is a Nib you instantiate from loadInterface(). I don't have a clear vision of the whole thing with only this piece of code, but it seems to me that you are trying to apply some styles to every key button of a keyboard view. Unfortunately this really depends on how the keyboard is implemented, can you provide some more details?
Anyway, from what I see I think you didn't write this code: probably you are following a tutorial or maintaining someone else's code. That's ok, but I suggest you to follow a an introduction course to iOS development with Swift, like the Udacity's one, which is fantastic IMHO (https://www.udacity.com/course/intro-to-ios-app-development-with-swift--ud585)
If you try to format your UIButton with QuartzCore framework, you'll need to import it first:
import QuartzCore
Then you will be able to access those members.
For example (latest swift3 code):
#IBAction func keyPressed(sender: UIButton) {
let button = sender as UIButton!
button?.backgroundColor = UIColor.red
button?.layer.shadowColor = UIColor.black.cgColor
button?.layer.shadowRadius = 1.0
button?.layer.cornerRadius = 4.0
}
In case you need to apply your styles sooner, try to consider to put this code into viewDidLoad or viewDidAppear methods:
self.nextKeyboardButton.backgroundColor = UIColor.red
self.nextKeyboardButton.layer.shadowColor = UIColor.black.cgColor
self.nextKeyboardButton.layer.shadowRadius = 1.0
self.nextKeyboardButton.layer.cornerRadius = 4.0
Seems like you're trying to "add property" not to a button, but rather to a closure which accepts a button as an argument.
Make it like this:
nextKeyboardButton.layer.shadowColor = UIColor.redColor.cgColor
nextKeyboardButton.layer.shadowRadius = 5.0
Ok so I have a function that allows my user to use the keyboard to go to the next field (Which I got the code from SO) It works perfect. My problem is, once my user gets to the final text field, in which, I've selected "GO" as the return button, and I want to use the go as the ideal submit button. As the flow through the form goes, its presentably right, but the functionality at the end isn't there. I found an answer here, but it looks like its calling the same functions as the "next field" code. So they don't work well together. So here's what the Next Field code looks like:
override func viewDidLoad() {
super.viewDidLoad()
// Keyboard Next Field & Delegate
enterEmailTextField.delegate = self
enterPasswordTextField.delegate = self
self.enterEmailTextField.nextField = self.enterPasswordTextField
self.enterPasswordTextField.nextField = self.enterNameTextField
// ...
}
And the next block is displayed below override (which i'm sure you know) func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() }
// Next Field
func textFieldShouldReturn(textField: UITextField) -> Bool {
if let nextField = textField.nextField {
nextField.becomeFirstResponder()
}
return true
}
Ok that works just fine down to the last text field, But since the last field is "Go" I'm trying to mimic what I would do with say an #IBAction for a button, but instead the go button. Here's where I got the idea/code for the Go to work:
Action of the "Go" button of the ios keyboard
Any Ideas on how to implement these? Or maybe just to work with the next function, and implement a "keyboard go" action similar to an #IBaction? Maybe just an all around better way to implement both of these so they coincide with each other? Any help is much appreciated!
EDIT!
I forgot the actual NextField.swift File which I'm sure is important (sorry)
import Foundation
import UIKit
private var kAssociationKeyNextField: UInt8 = 0
extension UITextField {
#IBOutlet var nextField: UITextField? {
get {
return objc_getAssociatedObject(self, &kAssociationKeyNextField) as? UITextField
}
set(newField) {
objc_setAssociatedObject(self, &kAssociationKeyNextField, newField, UInt(OBJC_ASSOCIATION_RETAIN))
}
}
}
this would help a lot I'm assuming
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
if (textField.returnKeyType == UIReturnKeyNext) {
// tab forward logic here
return YES;
}
else if (textField.returnKeyType == UIReturnKeyGo) {
// submit action here
return YES;
}
return NO;
}
On a cleaner way to handle tab order and form submitting, read my answer here.
First set return key to Go of your TextField, then implement the following method:
func textFieldShouldReturn(textField: UITextField) -> Bool {
if (textField.returnKeyType == UIReturnKeyType.Go)
{
// Implement your IBAction method here
}
return true
}
then see in below image:
Available for iOS 15 in SwiftUI. You can use .submitLabel(.go) to do it.
#available(iOS 15.0, *)
struct TextFieldSubmitView: View {
#Binding var name: String
var body: some View {
VStack(alignment: .leading) {
TextField("Type text", text: $name)
.textFieldStyle(.roundedBorder)
.padding()
#if os(iOS)
.submitLabel(.go)
#endif
}
}
}
#available(iOS 15.0, *)
struct TextFieldSubmitView_Previews: PreviewProvider {
static var previews: some View {
TextFieldSubmitView(name: .constant("Name"))
.previewLayout(.sizeThatFits)
}
}
Is it possible to set a design-time text for UILabel (or image if using UIImageView) on iOS 8? if so, how to?
Basically what I need is not to empty all of my labels before compiling so that it doesn't show dummy data before loading from the network the actual data. An algorithm to programatically clear all outlets isn't really a good solution as it is unnecessary code.
You could try subclassing the classes you want to have design-time attributes. Here is an example of that for UILabel:
import UIKit
class UIPrototypeLabel: UILabel {
#IBInspectable var isPrototype: Bool = false
override func awakeFromNib() {
if (isPrototype) {
self.text = "test"
}
}
Then, in IB, you will see isPrototype, and you can set it to true or false.
You can also change the default from false to true in the isPrototype:Bool = false line if you want. You can also change what happens if isPrototype is true. I had it make the text "test" so I could see feedback when testing this out, so you could change it to nil or "" or whatever.
You can also just eschew the isPrototype bool and have this class always reset the text. I just thought the IBInspectable attribute was cool, but if you just want this class to always clear the label text then you would just delete the bool and the check and just self.text=nil every time.
The con to this approach is you need to make all of your labels UIPrototypeLabel to get this functionality.
There is a second, scarier approach, that will add this functionality to all of your UILabels, and that is extending UILabel.
import ObjectiveC
import UIKit
// Declare a global var to produce a unique address as the assoc object handle
var AssociatedObjectHandle: UInt8 = 0
extension UILabel {
#IBInspectable var isPrototype:Bool {
get {
var optionalObject:AnyObject? = objc_getAssociatedObject(self, &AssociatedObjectHandle)
if let object:AnyObject = optionalObject {
return object as! Bool
} else {
return false // default value when uninitialized
}
}
set {
objc_setAssociatedObject(self, &AssociatedObjectHandle, newValue, objc_AssociationPolicy(OBJC_ASSOCIATION_RETAIN_NONATOMIC))
}
}
public override func awakeFromNib() {
if (isPrototype) {
self.text = "test"
}
}
}
credit to Is there a way to set associated objects in Swift? for some of that code
I'm new to swift. My app uses photos people upload to the web and showing the photos in a table view.
It is reloading whenever some user uploads a new photo.
I have a UITextField that when you press it the keyboard goes up. My problem is that it goes down whenever reloadView is happening (when a new photo arrives)
What i'm trying to do is to check if the UITextField is first recogniser and if so I want to wait with the reload until it not first recogniser.
func refreshView()
{
dispatch_async(dispatch_get_main_queue()) { () -> Void in
while (self.dataSource.writeSomethingTextLabel.isFirstResponder()) {
//need to wait somehow for notification that it is not first responder anymore
}
self.tableView.reloadData()
}
}
So with this code the keyboard is not going down of course but the wile loop runs and everything is stuck. my question is how can I wait until the user finishes using the text label (first responder is false).
thanks
You can conditionally reload tableView on two places: when new data arrives and when textField ends editing:
#class YourViewController: UIViewController, UITextFieldDelegate {
var needsToReloadTableView = false;
func reloadTableViewIfNeeded() {
if needsToReloadTableView {
self.tableView.reloadData()
needsToReloadTableView = false
}
}
func gotNewData() {
needsToReloadTableView = true
if !self.textField.isFirstResponder {
reloadTableViewIfNeeded()
}
}
func textFieldDidEndEditing(textField:UITextField) {
reloadTableViewIfNeeded()
}
}
#IBAction func viewTapped(sender : AnyObject) {
totalTextField.resignFirstResponder()
}