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.
Related
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
I'm trying to accomplish an autocomplete of my own for matching array items while typing. So, I already have the logic done for matching what's being typed with what's in the array, but my question is related to displaying the proposed correction in the UITextView like this screenshot
I was trying by splitting the text in the textview and replacing the last word into attributed strings, but that makes the proposed correction part of the actual contents. Any ideas of how this is accomplished in this app?
What you currently do is good , when you set the text of the textView to attributed string make bool flag say it's name is textEdited = true with a string part that the user types say it's name userStr , when textView change method is triggered check that bool and according to it make the search if it's true proceed search with userStr if it's not proceed search with whole textView text , don't forget to make textEdited= false after every zero suggested result
Edit: regarding the cursor put a label under the textfield with the same font as the textView and make it's background darkGray , also make background of the textview transparent and every attributed string assign it to the label so plus part of the label will be shown and cursor will be as it is in the textView
I'm new to Xcode and trying to do something easy.
But unfortunately, I couldn't find an easy way to do so.
My question might be (laughably) easy but I'm seriously stuck right now.
In Xcode, there is an object in the object library called "Text Field".
In this text field, words can be entered. I want to push a button and save the written words not a string, which I can then display as a label.
Help would be really appreciated.
Thanks in advance!
The "Text Field" UI element in the object library represents an instance of a UITextField. If you look at the documentation, a text field has a text property - a String that contains the current textual content of the text field. Similarly, UILabel has a text property. So you want to take the text field's text, and assign it to the label's text.
Assuming you have an outlet to your text field in the storyboard and an outlet/variable referencing the label you want to set the text of, in your button's action method, you might do something like this:
#IBAction func buttonPressed(button: UIButton) {
myLabel.text = myTextField.text
}
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
}
I am wondering if there is a way to use a UITextField programatically (i.e. use buttons as inputs) so that you can select a UITextField but not show the UIKeyboard, then when you select a UIButton it would assign a string value to the currently selected UITextField.
I don't really know where to start.
I think you can visually change the appearance of the text field (for example add a blue border), let the user feel it’s “selected”. Then you just modify textfield.text when user presses button.
Or alternately, you can create a customized keyboard. There are many similar questions.
It seems that you don't really need a text field (e.g., edit/select text, etc.), but a "button that stays highlighted" instead. Then, you can programmatically change the button's title label to the specified string when the user taps the 'proper' buttons.