Display text along with border in swift iOS - ios

I will get array of strings those strings need to display with border in swift iOS.
Example:
let tags = ["Draft", "Sent", "Inbox"]
The above list need to convert as a flat string and display in single line as shown in below
Expected output with border draft image:
Draft Sent Inbox // All tags should have border like below also text colour should be different
I tried with below it works upto making single line string how to create border for each along with different colour.
func mergeTags(tags: [String]) -> String {
let tagsList : String = tags.reduce("", { $0 == "" ? $1 : $0 + " " + $1 })
return tagsList
}

Related

How can I replace specific characters in UITextView

Hi in the log window I have two array.First one is my colored one as you can see the simulator blue ones.I want to change/replace blue ones with the second array but they are my chords .
Actually I tried like in following codes but all blue text changed with "Dbm" that was the second array's first string.
for txt in coloredTexts {
for transposed in transposedAkorArray{
self.akor_goster.text = self.akor_goster.text.replacingOccurrences(of: "\(txt)", with: "\(transposed)")
}
}
Try,
for (index,txt) in coloredTexts.enumerated() {
let range = akor_goster.text.range(of: txt)
akor_goster.text.replacingOccurrences(of: txt, with: transposedAkorArray[index], options: [], range: range)
}
I think you are trying to do something like this:
for i in 0..<coloredTexts.count {
self.akor_goster.text = self.akor_goster.text.replacingOccurrences(of: "\(coloredTexts[i])", with: "\(transposedAkorArray[i])")
}

creating spaces in placeholder fields

my problem is I want to put 2 placeholders for one textField. one of the place holders should be on the left and the other should be on the right.
my code:
func returnPlaceHolder(first: String, second: String, textField: UITextField){
let width: Int = Int(textField.bounds.width * 0.2)
let spaceValue = width - (first.count + second.count)
var temp = "\(first) "
let tempCount = spaceValue - (first.count + second.count)
var value = String()
for _ in 0..<tempCount {
temp.append(" ")
}
value = "\(temp) \(second)"
textField.placeholder = value
textField.setLeftPaddingPoints(10)
textField.setRightPaddingPoints(10)
}
I'm currently using this function to create spaces.. but my problem is the spaces won't be the same for more than one textField, and I want them to be aligned..
just like this picture: https://imgur.com/pZZMoNv
and this is the result I'm getting for my current code: https://imgur.com/a/5AN8EXl
don't mind the textFields format & text I can fix them later.. I just want to be able to align the textFields placeholders.
It would be really hard (if possible) to achieve what you are trying to do by injecting spaces into the text because each character in the font has a different width unless you use a monospaced font.
https://en.wikipedia.org/wiki/Monospaced_font
Instead, I would recommend a different approach. Override the text field, provide two UILabels and adjust their position using Autolayout.

Set Image When Word Appears In Label From Array

I am trying to make an image view set to a certain image when a label displays a certain word from an array which is being randomised.
The picture is supposed to set to the image view when specific words are the text of the label from the array.
Here is the array:
let freeMoodArray = ["Happy", "Sad", "Angry", "Annoyed", "Curious", "Bored", "Chilled", "Furious", "Excited", "Scared", "Emotionless", "Shocked", "Tired", "Sick", "Amused"]
Here is the random label text:"
self.moodAnswer.text = "\(self.freeMoodArray.randomElement()!)"
Now when I load the view, the image chooses a random word from the array which is working. Now, lets say it says Happyas the text of the label.
I want to set a certain image only when it says happy.
Here is my code, which doesn't work: (this func is called in the viewDidLoad())
func emojiMood() {
if moodAnswer.text == "Happy" {
emojiImg.image = UIImage(named: "happy.png")
}
}
how a but a variable and DidSet ?
// create a variable
var moodAnswer : string = "" {
didSet {
// if or switchCase, set image only if the text is equal to Happy
if moodAnswer == "Happy" {
emojiImg.image = UIImage(named: "happy.png")
}
// always set text field with new updated text
self.moodAnswerLabel.text = moodAnswer
}
}
// in your code always set moodAnswer variable, this is more clean and also you set label and image only in a single place
self.moodAnswer = "\(self.freeMoodArray.randomElement()!)"

Adding UITextField’s input into an array

I have a simple app that has 3 text fields, a label and a button. User would enter text into the 3 text fields and then press the button. The text from one of the text fields would randomly be selected and applied as the text to a label.
I’ve tried using something like
let textArray : String [text1, text2, text3]
For the array but this doesn’t seem to be working. Can someone nudge me in the right direction?
You can take an array and add textfield text into that array.
var textFieldArray: [String] {
return [textfield1.text!, textfield2.text!, textfield3.text!]
}
Then generate a random number using the code below:
func RandomInt(min: Int, max: Int) -> Int {
if max < min { return min }
return Int(arc4random_uniform(UInt32((max - min) + 1))) + min
}
Now get the random text using the index path of the array.
let randomNumber = RandomInt(min: 1, max: 3)
let randomText = textFieldArray[randomNumber]
labelName.text = randomText
Not sure if I understand. You probably want this:
var textArray: [String] {
return [textfield1.text, textfield2.text, textfield3.text]
}

Swift - Placeholder long text - cut the middle

I have UITextField with long placeholder like this:
"QWERTYUIOPASDFGHJKLZXCVBNM", but my textfield is small and when I use function textField.adjustsFontSizeToFitWidth = true the minimumFontSize that I can set is 9 but still I can't fit the text into the textField. I want to cut the middle of the text and I expect the text to be "QWERTY...CVBNM" how to do that?
func cutTheMiddleOfLongString(var string:String) -> String {
if(countElements(string)>20){
let begining = string[advance(string.startIndex, 0)..<advance(advance(string.startIndex, 0), 12)]
let ending = string[advance(string.startIndex, countElements(string)-8)..<advance(advance(string.startIndex, countElements(string)-8), 8)]
string = begining + "..." + ending
}
return string
}
This will take first 12 letters and last 8 letters of the string and will put ... between them(begining ending).

Resources