I have a string and I want to change colors of two words in that string. So, I created a function
func setup()
{
let main_string = "By continuing you agree to our Term of use and Privacy Policy "
var string_to_color = ["By continuing you agree to our","and"]
for i in 0..<2
{
let range = (main_string as NSString).range(of: string_to_color[i])
let attributedString = NSMutableAttributedString.init(string:main_string)
attributedString.addAttribute(NSAttributedStringKey.foregroundColor, value: UIColor.black , range: range)
privacyL.attributedText = attributedString
}
But, it only changes the colour for the second word and not for the first one.
Can anyone help?
let main_string = "By continuing you agree to our Term of use and Privacy Policy "
var string_to_color = ["By continuing you agree to our","and"]
for i in 0..<2
{
let range = (main_string as NSString).range(of: string_to_color[i])
let attributedString = NSMutableAttributedString.init(string:main_string)
attributedString.addAttribute(NSAttributedStringKey.foregroundColor, value: UIColor.black , range: range)
privacyL.attributedText = attributedString
}
You are overriding each time privacyL.attributedText, so you'll get only the "result" of last iteration.
Instead, do:
let attributedString = NSMutableAttributedString.init(string:main_string)
for i in 0..<2
{
let range = (main_string as NSString).range(of: string_to_color[i])
attributedString.addAttribute(NSAttributedStringKey.foregroundColor, value: UIColor.black , range: range)
}
privacyL.attributedText = attributedString
Now, next issues:
Don't do a loop for i in 0..<2, instead use at least the count of string_to_color (or a for each loop). If tomorrow you add a string in it or remove one, you'll encounter an issue.
Also range(of:) will return the first occurence found, so if you have:
let main_string = "and and"
var string_to_color = ["something", "and"]
only the first "and" will be colored.
You have then to iterate or use a NSRegularExpression.
Here is a related question: Color all occurrences of string in swift
Just want to add to Larme's answer. You can create extension of String, which will be responsible for string coloring
extension String {
func colored(_ strings: [String], with color: UIColor) -> NSAttributedString {
let attributedString: NSMutableAttributedString = NSMutableAttributedString(string: self)
for string in strings {
let range = (self as NSString).range(of: string)
attributedString.addAttribute(NSAttributedStringKey.foregroundColor, value: color , range: range)
}
return attributedString
}
}
And now you can use it anywhere in your code, like this:
let main_string = "By continuing you agree to our Term of use and Privacy Policy "
let string_to_color = ["By continuing you agree to our","and"]
privacyL.attributedText = main_string.colored(string_to_color, with: UIColor.red)
Related
I am trying to change my string value in an array after shuffling another array, how am i to do this?
Example:
var array1 = [1,2,3,4,5,6]
var stringarray = ["\\(array1[0]) = 1")
array1.shuffle()
print(stringarray)
How am i to change the original stringarray value to the new shuffled value?
Thank you
The task:
#IBAction func nextQuestion(_ sender: Any) {
if levelSelected == 1 {
questionLabel.text = standardRules.randomElement()
}
players.shuffle()
print(players)
standardRules has a string value that takes the value of players[0]
Essentially what i am trying to do is this:
I am trying to grab 2 random values that are not the same in a string like this
var players = ["Jack, John, Michael, Peter"]
var playersArray = ["\(players.randomElement) and \(players.randomElement) has to battle")
How am i to do this, so it grabs 2 different values?
You could make it very easy by this code :
var players = ["Jack", "John", "Michael", "Peter"]
// get the first random element
var random1 = players.randomElement()
//delete that element so you can't duplicate it
players = players.filter{$0 != random1}
//get your second radom element
var random2 = players.randomElement()
//add your element again
players.append(random1!)
Looks like you're missing some core concepts of the Swift language. When you create your stringarray it makes a call of array1.description and stores the result into an array. From that point any modifications of the original array will not change anything in stringarray.
So if you want to pick two different players from an array you need to do something like that:
let index1 = Int.random(in: (0 ..< players.count))
var index2: Int
repeat {
index2 = Int.random(in: (0 ..< players.count))
} while index1 == index2
let matchText = "\(players[index1]) and \(players[index2] has to battle)"
I would try replacing:
var stringarray = ["\\(array1[0]) = 1")
array1.shuffle()
with:
var stringarray = array1.shuffle()
I would like to make a function that generates an array consists of Strings, and integers in range combined as a whole String. For example:
let fruit = "apple"
let numbers = Array(1...10)
let format = ".jpg"
->
["apple1.jpg", "apple2.jpg", "apple3.jpg", ..... "apple10.jpg"]
How can I combine a defined String with range of integers and put them in an array? Apologize for a newbie question. Much appreciated. <3
The simplest solution is you can directly use map on your range.
let array = (1...10).map({ "apple\($0).jpg" })
print(array) //["apple1.jpg", "apple2.jpg", "apple3.jpg", ..... "apple10.jpg"]
Use this:
func mergeStringAndInt(_ prefix: String, intArray: [Int], postfix: String) -> [String] {
return intArray.map {String(format: "%#%d%#", prefix, $0, postfix )}
}
You can do the like -
func resultArray() -> NSMutableArray {
var your_Array = NSMutableArray()
for item in numbers {
let combinedStr = "\(fruit)\(item).\(format)"
your_Array.add(combinedStr)
}
return your_Array
}
for tempExportData in exportDataArray {
let tmpRegNO:NSString = (tempExportData as AnyObject).object(forKey: kRegisteredNo) as! NSString
print("tmpRegNO is",tmpRegNO)
var tmpNoArray:Array = [String]()
tmpNoArray.append(tmpRegNO as String)
print("Count is",tmpNoArray.count)
print("ARRAY is",tmpNoArray)
}
I am trying to add string value i.e tmpRegNO to the Array tmpNoArray.
In this I can able to add only one value to the array at a time.
How to add the next value to that array when it is looping for second time.
As already mentioned you have to declare the array before entering the loop.
Your code is very objectivecish. This is a swiftier version. Don't annotate types the compiler can infer and use key subscription rather than ugly casting to AnyObject and objectForKey:.
var tmpNoArray = [String]()
for tempExportData in exportDataArray {
let tmpRegNO = tempExportData[kRegisteredNo] as! String
print("tmpRegNO is",tmpRegNO)
tmpNoArray.append(tmpRegNO)
print("Count is",tmpNoArray.count)
print("ARRAY is",tmpNoArray)
}
You can even write the whole expression in one line:
let tmpNoArray = exportDataArray.flatMap { $0[kRegisteredNo] as? String }
You need move the tempNoArray initialization outside of your for in loop, if not the your array will be initialized once for every item in your exportDataArray remaining only the las item as consequence
You need something like this
var tmpNoArray:Array = [String]()
for tempExportData in exportDataArray{
if let tmpRegNO = tempExportData[kRegisteredNo] as? String
{
print("tmpRegNO is",tmpRegNO)
tmpNoArray.append(tmpRegNO as String)
print("Count is",tmpNoArray.count)
print("ARRAY is",tmpNoArray)
}
}
I would to know how to get key if I have the values. Which class get higher marks?
let higherMarks = [
"ClassA": [10,20,30,40,50,60],
"ClassB": [15,25,35,45,55,65],
"ClassC": [18,28,38,48,58,68],
]
var largest = 0
var className = ""
for (classTypes, marks) in higherMarks {
for mark in marks {
if mark > largest {
largest = mark
}
}
}
print(largest)
What I'm saying in my comment is that you need to get the classTypes when you get the mark. Because when you get the higher mark, you want to also get the corresponding key value.
Keeping your code's logic I would do something like this:
let higherMarks = [
"ClassA": [10,20,30,40,50,60],
"ClassB": [15,25,35,45,55,65],
"ClassC": [18,28,38,48,58,68],
]
func findBestClass(in results: [String: [Int]]) -> (name: String, score: Int) {
var largest = 0
var type = ""
for (classType, marks) in results {
if let max = marks.max(), max > largest {
largest = max
type = classType
}
}
return (type, largest)
}
let best = findBestClass(in: higherMarks)
print("The best class is \(best.name) with a score of \(best.score).")
I just replaced your inner loop with .max() and changed the name of the key variable because it should not be plural. My method also returns a tuple because I find it relevant in this situation. But I didn't change your logic, so you can see what I meant by "also get the classTypes".
My code already generates a random String of an array when I press a button, but sometimes a String gets repeated. What do I have to do so that the String "Mango" only gets called again when all the other Strings where already called without using a shuffle, I want to call one String at a time?
Example: "Mango", "Kiwi", "Banana", "Pineapple", "Melon", "Mango", "Kiwi",.....
Here is my code:
var array = ["Mango", "Banana", "Apple","Kiwi", "Melon", "Pineapple"]
let fruits = Int(arc4random_uniform(UInt32(array.count)))
print(array[fruits])
In order to avoid repetitions you need to keep track of which fruits have previously been seen. There are several ways to do that an all of the proposed solutions do it in one way or another.
For your specific use case, you will need this tracking to be retained outside of the code executed by the button (in your view controller for example).
Here is a generalized structure that could help with this:
(you can define it inside the view controller if this is a one-time thing or outside of it if you intend to reuse the mechanism elsewhere)
struct RandomItems
{
var items : [String]
var seen = 0
init(_ items:[String])
{ self.items = items }
mutating func next() -> String
{
let index = Int(arc4random_uniform(UInt32(items.count - seen)))
let item = items.remove(at:index)
items.append(item)
seen = (seen + 1) % items.count
return item
}
}
To use it, you would declare a variable (in your VC) that will keep track of your fruits:
var fruits = RandomItems(["Mango", "Banana", "Apple","Kiwi", "Melon", "Pineapple"])
And in the button's code use that variable (fruits) to print a non-repeating fruit name at each execution
func buttonPressed() // <- use your function here
{
print( fruits.next() )
}
You need to implement some logic. It's quite easy if you think harder. Run this in your Playground, or if you fully understand this block of code, you can do this in your project already.
var array = ["Mango", "Banana", "Apple","Kiwi", "Melon", "Pineapple"]
var selectedIndices = [Int]()
for _ in 1...20 {
let randomFruitIndex = Int(arc4random_uniform(UInt32(array.count)))
// Print only if not yet printed once
if !selectedIndices.contains(randomFruitIndex) {
print(array[randomFruitIndex])
selectedIndices.append(randomFruitIndex)
}
// Reset
if selectedIndices.count == array.count {
print("----- CLEARING SELECTED INDICES----")
selectedIndices.removeAll()
}
}
So as you can see, we are adding each generated random number (in your case, it's the fruits variable.) into an array of Int. Then if the number of selectedIndices is equal to the count of the array of fruits, clear all the stored selectedIndices.
OUTPUT:
Pineapple
Melon
Mango
Kiwi
Banana
Apple
----- CLEARING SELECTED INDICES----
Mango
Melon
This is an adaption from the accepted answer of the linked topic in my comment:
var source = ["Mango", "Banana", "Apple","Kiwi", "Melon", "Pineapple"]
var usedElements = [String]()
func choosePseudoRandomElement() -> String {
if source.count == 0 {
source = usedElements
usedElements = []
}
let randomIndex = Int(arc4random_uniform(UInt32(source.count)))
let randomItem = source[randomIndex]
usedElements.append(randomItem)
source.remove(at: randomIndex)
return randomItem
}
for _ in 1...18 {
print("Item: \(choosePseudoRandomElement())")
}
One potential issue with this solution is that it may happen, that the last element of one complete iteration also occurs as the first element of the second iteration. You can handle that case by comparing the randomly chosen item with the item which was chosen before (use a while loop until the items doesn't match anymore).
Also, this does remove elements from the source array. If you do not want that, create a copy of the source array.