How can I set up a DialogBox in Swift? - ios

I would like to have a box appear in the screen (some kind of textfield) and want to make it look like the static character is talking to the user (like the link below). The text needs to be slowly shown to the user (not all at once). I'm not sure the best way to implement this. Also, I'm not sure how to wrap the text within the box.
http://lightsendgame.com/images/screenshots/CrystalsRoom.JPG

You can create a UIView with a UILabel inside and set the alpha to 0. Whenever you want the box to appear (on button press for example) you can execute a UIAnimation block to set the alpha to 1. This will give the fade in effect you want.

How you display the text is up to you, use the styling in xCode for this, most of us aren’t the best of designers ;)
About the text which slowly comes in to view, you can iterate through a string and keep on adding the next letting in the string to another string. After you’ve added one letter to the second string, output this second string to the user. This will go pretty quick though, if you want to slow this down you can make the thread sleep for an x amount between the iterations.
import UIKit
import XCPlayground
var text:String = “Slowly old-school displaying text is awesome!"
var scrollingText:String = “"
var label:UILabel = UILabel(frame: CGRectMake(0, 0, 400, 100))
for char in text{
scrollingText = "\(scrollingText)\(char)"
label.text = scrollingText
}

Related

iOS accessibility fields grouped by View

As I understand it the accessibility tool on iOS reads top down and left to right. Is there a way to group by view so that two views next to each other at equal height (one on the right and one on the left) will not have their respective elements read together (right left, right left) but rather all the right view's elements followed by the left view's elements?
Using the UIAccessibilityContainer protocol and especially the shouldGroupAccessibilityElement instance property may help you grouping all the accessible elements to be read out by VoiceOver in a specific order. 👍
I suggest to take a look at this good example including {code snippet (ObjC;Swift) + illustration} that highlights the way to spell out a numeric keypad following a predetermined sequence. 😉
The accepted answer is good, but to save the next person more reading here's what you can do:
func setAccessibility() {
/// groups all elements into a single accessibility element.
/// The order isn't important for screen reader.
accessibilityElements = [rightLabel, leftLabel]
/// required
isAccessibilityElement = true
/// grouping does not auto generate accessibilityLabel content,
/// so you can setup the text as you like.
let rightText = rightLabel.text ?? ""
let leftText = leftLabel.text ?? ""
accessibilityLabel = "\(rightText), \(leftText)"
}
The order of the array of elements isn't really important to a screen reader as the label still needs to be constructed separately. There may be other purposes for keeping the order though.

UIButton word wrap doesn't work when entering text?

I've got a UIButton, it's a simple segue to another page.
I've set Title to attributed and then selected word wrap. This works fine, the second word wraps down to the next line.
However, it is all left justified. When I select "Align Centre" (using the buttons just under the "Title", the word wrap no longer works and simply runs .... so you can't see it all. (e.g. "next pa" instead of "next page")
Am I missing something here? It seems like such a trivial thing to do! There's an old answer here can't get word wrap to work on UIButton but it's both old and uses code - surely you don't need code to centre the button text if you want to word wrap it to 2 lines!?
I've set Title to attributed and then selected word wrap. This works fine, the second word wraps down to the next line. However, it is all left justified.
Once you've decided to use an attributed string, you must do everything with the attributed string. So give your attributed string a paragraph style that centers the text.
let para = NSMutableParagraphStyle()
para.alignment = .center
para.lineBreakMode = .byWordWrapping
let s = NSAttributedString(
string: "Hello World", attributes: [.paragraphStyle : para])
self.button.setAttributedTitle(s, for: .normal)
You will also need to set the button's title label to allow multiple lines.
self.button.titleLabel?.numberOfLines = 0
Result:
surely you don't need code to centre the button text if you want to word wrap it to 2 lines!?
Not to center it, no; you can set the centering in the storyboard. So you could eliminate the first batch of code and configure it in the storyboard. But you must use code to turn the title label into a multiline label:
self.button.titleLabel?.numberOfLines = 0
There is no way to do that in the storyboard, because you have no access to the title label there.
I've just been playing round with this and I've found it works if you set it to 'character wrap' rather than 'word wrap' once you've selected centre alignment.
If anyone has a better solution please add it, as I guess this might have issues if you slightly change the width etc when using auto layout for different screen sizes etc if you want it to adapt its width so this might not be the best solution but it does work

Hyperlink + click like button on NSAttributedString

I have a username variable and a message string such as "liked your post" and in order to be able to cast it as one string, I combined them in an NSMutableAttributedString. As it'll be easy, I am
let username = notification["username"].string
let notificationBody = notification["body"].string
let notificationString = NSMutableAttributedString(string: "\(username!) \(notificationBody!)")
notificationString.addAttributes([NSForegroundColorAttributeName: UIColor.blackColor()], range: NSMakeRange(0, (username?.characters.count)!))
cell.notificationLabel.attributedText = notificationString
Colouring the text is working as expected, however, I want to be able to click on the username and at the same time achieve a colour-effect of click (like click on button). Here is what I mean:
Without click:
on click :
and segue to another VC.
I actually achieved what I want to achieve with a primitive hack that I absolutely do not like. What I did was, first I added an extra button over username and set background-color: clear color. And it visually seems nice if the width of the username coincidentally matches the width of the button in Storyboard - cell. Otherwise, it seems buggy.
Extra Button layer over 'username' text:
What is the practical way of achieving what I want to achieve? Should I just change the width of the transparent button for each cell, according to the username character count? So if for 4-6 words, x width; if 8-10 words, y width?
Are there any better ways than this?
I would change the layout to have a button that contains the user name and a label that contains the body instead of just one label. That way the button will always be the same size as the username.

Adding values from button to UILabel

I am learning Xcode(objective C). I want to make simple calculator.
I started by adding four simple buttons and one label. Two buttons are for numbers(1 and 2), and I added variables into .m file:
int *one = 1;
int *two = 2;
Next step what I've done is that I made action on button clicked and said that label take value from that button:
self.firstLabel.text=[NSString stringWithFormat:#"%d", one);
And I made same action for another button.
Everything is fine until this step, what I want to do next is when I click on number button to add value to that label, then when I click to divide(/) button to add / sign and when I click to another button to add another value.
I want to be something like this in label: 2 / 1, and when i click = button to divide them and show result. Thanks.
EDIT:
I also added label proper into .h, and when I click on button it shows me only one value in label.
Since you are just starting to learn obj C you may want to keep your calculator simple, building a fully functional calc. can get surprisingly complex. You should consider using an RPN approach, it will make things much easier, you can just push the numbers to a stack and perform operations one at a time.
Something like: 2, enter, 1, enter + enter.
You may also want to have a look at Stanford's iOS course on Apple University, the course is in Swift but the class project was a calculator so it should give you a good reference point. Hope that helps and good luck!
you should add a custom target for each button, i.e.,
- (IBAction)addDivide:(id)sender {
self.firstLabel.text = [[NSString alloc] initWithFormat:#"%# /", self.firstLabel.text];
}
Any action will update your label.
Just a further advice for you, don't name the label as "firstLabel", try to give it a name tied to its semantics.
You are only setting the int value to the label. You need to append the value maintaining the previous text so that it appears as "2 / 1".
I'll suggest using tags for the buttons and use a common method for number buttons in your calculator.
This is more sort of logical thing rather than specific to iOS and Xcode.
Similarly when "=" is pressed use a method to calculate the result.

In iOS, how do I programmatically display dynamically changing values in a text field?

I have Objective C code that continually updates a set of numerical values. I need to display these values on the screen. That's it! I can convert numerical values into a text string, no problem. But how do I display this string in a UI element? Do I use a text box or a text field or a text view, or a...? I cannot find examples to show how to pass a string from code into the UI. I assume I need to set up a text thingy, and then periodically refresh the contents of that text thingy when the values change?
I assume the answer is simple but is just obscured by a smokescreen of technical jargon.
Thanks!
From the UI perspective you might want something like a
UITextView - multi-line text input
UITextField - single-line text input
UILabel - just text
For your purpose of just printing text, you should use UILabel, since you dont want / need any kind of input. You can access its text using:
// yourLabel is your current UILabel* you want to output yourValue to
yourLabel.text = yourValue;
Of course that yourValue needs to be converted to NSString before.
To actually get hold of the UILabel, you need to connect it from the Interface Builder as an IBOutlet. For tutorials on that topic, take a look at tutorials like Interface Tutorials by Ray Wenderlich or youtube or just google Interface Builder tutorial.

Resources