I am trying to present the user with a TableView, within each cell of the table view there is an image and a textView, I am providing the textView.text with a single string for each cell, however within each string, I'd like that there could be multiple fonts. For example, one part of the string will have a size of 20 and bold and the rest will have a size of 14 and Regular. You can see what I have in my app in the following image
As you can see, I tried to edit the "How do you trade stocks?" part of the string with NSMutableAtributtedString, however the output was the settings of it rather than the string with the selected properties. The code I used is the following:
let title1 = "How do you trade stocks?"
let atributtedTitle1 = NSMutableAttributedString(string: title1)
atributtedTitle1.addAttributes([NSAttributedStringKey.foregroundColor: UIColor.black, NSAttributedStringKey.font: UIFont(name: "AvenirNext-Bold", size: 20)!], range: getRangeOfSubString(subString: title1, fromString: title1))
var textViews:[String] = ["\(atributtedTitle1)The stock market is one of the best ways you can build up wealth. The stock market is one of the best ways you can build up wealthThe stock market is one of the best ways you can build up wealthThe stock market is one of the best ways you can build up wealthThe stock market is one of the best ways you can build up wealthThe stock market is one of the best ways you can build up wealth. The stock market is one of the best ways you can build up wealthThe stock market is one of the best ways you can build up wealthThe stock market is one of the best ways you can build up wealthThe stock market is one of the best ways you can build up wealthThe stock market is one of the best ways you can build up wealth. The stock market is one of the best ways you can build up wealthThe stock market is one of the best ways you can build up wealthThe stock market is one of the best ways you can build up wealthThe stock market is one of the best ways you can build up wealthThe stock market is one of the best ways you can build up wealth. The stock market is one of the best ways you can build up wealthThe stock market is one of the best ways you can build up wealthThe stock market is one of the best ways you can build up wealthThe stock market is one of the best ways you can build up wealthThe stock market is one of the best ways you can build up wealth. The stock market is one of the best ways you can build up wealthThe stock market is one of the best ways you can build up wealthThe stock market is one of the best ways you can build up wealthThe stock market is one of the best ways you can build up wealth"]
Loading the textView text usually as it's done in a tableView with
cell.descriptionTextView.text = textViews[indexPath.row]
And the function I used to calculate the range in NSMutableAtributtedString is as follows:
func getRangeOfSubString(subString: String, fromString: String) -> NSRange {
let sampleLinkRange = fromString.range(of: subString)!
let startPos = fromString.distance(from: fromString.startIndex, to: sampleLinkRange.lowerBound)
let endPos = fromString.distance(from: fromString.startIndex, to: sampleLinkRange.upperBound)
let linkRange = NSMakeRange(startPos, endPos - startPos)
return linkRange
}
How could I have multiple fonts and sizes in a single string so I can provide it to a single textView?