Partially Bold Text in UILabel [duplicate] - ios

This question already has answers here:
How to format certain words of a cell's text label
(3 answers)
Closed 5 years ago.
I have a tableView which has cells populated by two arrays :
var names = ["Marie : ", "Nicolas : " , "Sarah : "]
var colors = ["White" , "Blue" , "Red"]
My cells are populated by my arrays in a single UILabel named "info" :
cell.info.text = names[indexPath.row] + colors[indexPath.row]
Everything works fine, I just don't manage to have my "names" array to appear in bold without touching to the "colors" array.
What would be the best approach to do this ?
Thanks in advance for any help !

If you just want the names to appear in bold, use the UILabel attributedText attribute to set an attributed string instead of a plain text string.
Something like:
let name = names[indexPath.row]
let color = colors[indexPath.row]
let attributedText = NSMutableAttributedString(string: name, attributes:[NSFontAttributeName : UIFont.boldSystemFont(ofSize:15)])
attributedText.append(NSAttributedString(string:color))
cell.info.attributedText = attributedText
Of course, you'd need to set the font in the above for the bold part according to how you have the cell styled.

Related

Have background color wrap UILabel [duplicate]

This question already has answers here:
How to highlight only text in UILabel - IOS
(4 answers)
Closed 5 years ago.
I am trying to display the following
But when setting the text background color it extends the entire background and fills much than I would like as I am just trying to apply a "highlight" text feature. Any ideas would be greatly appreciated
You can do it as - first iterates line in your text and then apply background color with lines. Here i used Multi-line String Literals.
By using NSBackgroundColorAttributeName apply color to lines.
#IBOutlet weak var myLabel: UILabel!
myLabel.text = """
GET
MORE LIVES
"""
myLabel.numberOfLines = 0
let attributeString = NSMutableAttributedString(string: myLabel.text!)
let labelText = myLabel.text!
var lines: [String] = []
labelText.enumerateLines { line, _ in
lines.append(line)
}
print(lines)//lines in your text
var startIndex = 0
for value in lines {
//Apply background color to lines
attributeString.addAttribute(NSBackgroundColorAttributeName, value: UIColor.red, range: NSRange(location: startIndex, length: value.characters.count))
startIndex = startIndex + value.characters.count + 1
//startIndex will locate new line's first index
}
//Assign attributedText to your label
myLabel.attributedText = attributeString
We cannot wrap the colour of a view in iOS, The possibilities are
Keeping duplicate UILabels as Nathan said.
Designing a canvas and and assign it to "UIImageView".
I think last possibility is the better way to implement
You can also view my GitHub repository regarding this query.

How do I change the font attributes of certain sections of a UILabel text in Swift? [duplicate]

This question already has answers here:
Example of NSAttributedString with two different font sizes?
(4 answers)
ios swift: Is it possible to change the font style of a certain word in a string?
(3 answers)
Closed 7 years ago.
Example:
let dummy = UILabel()
dummy.text = "this is a String"
I want the view to show something like this. Also, is it possible to change the font size of "String" in dummy?
this is a String
You can use NSAttributedString to do that:
let dummy = UILabel()
let theString = "this is a String"
let attributedString = NSMutableAttributedString(string: theString, attributes: [NSFontAttributeName:UIFont.systemFontOfSize(12.0)])
let boldFontAttribute = [NSFontAttributeName: UIFont.boldSystemFontOfSize(24.0)]
attributedString.addAttributes(boldFontAttribute, range: NSMakeRange(10, theString.characters.count))
dummy.attributedText = attributedString

UILabel text with different font size and color [duplicate]

This question already has answers here:
How do I make an attributed string using Swift?
(31 answers)
Closed 7 years ago.
Howto set different font size and color in a UILabel with Swift?
I need to color the first char of the string with different color and size than the rest of the string.
Suppose you want to have a smaller and gray currency symbol like this:
Just use a NSMutableAttributedString object:
let amountText = NSMutableAttributedString.init(string: "€ 60,00")
// set the custom font and color for the 0,1 range in string
amountText.setAttributes([NSFontAttributeName: UIFont.systemFontOfSize(12),
NSForegroundColorAttributeName: UIColor.grayColor()],
range: NSMakeRange(0, 1))
// if you want, you can add more attributes for different ranges calling .setAttributes many times
// set the attributed string to the UILabel object
myUILabel.attributedText = amountText
Swift 5.3:
let amountText = NSMutableAttributedString.init(string: "€ 60,00")
// set the custom font and color for the 0,1 range in string
amountText.setAttributes([NSAttributedString.Key.font: UIFont.systemFont(ofSize: 12),
NSAttributedString.Key.foregroundColor: UIColor.gray],
range: NSMakeRange(0, 1))
// if you want, you can add more attributes for different ranges calling .setAttributes many times
// set the attributed string to the UILabel object
// set the attributed string to the UILabel object
myUILabel.attributedText = amountText

How can I bold part of a string in Swift? [duplicate]

This question already has answers here:
Any way to bold part of a NSString?
(11 answers)
Closed 7 years ago.
If I create a label like this:
var label = UILabel(...)
label.text = first_name + ", " + age
self.view.addSubview(label)
How can I change the age of the label to bold, but keep the first_name normal weight?
You can use an attributed string. Here, the bold size is 15, but you can also change that point size if you want.
var age = "13"
var att = [NSFontAttributeName : UIFont.boldSystemFontOfSize(15)]
var boldAge = NSMutableAttributedString(string:age, attributes:att)
// Assigning to a UILabel
yourLabel.attributedText = boldAge
so now you would want to assign your text like this:
label.attributedText = first_name + ", " + boldAge

Attributed Strings for UILabels in Xcode 6

I wanted to change the text of a label that says "hello world" such that "hello" is red, and "world" is blue. I'm doing this programmatically and obviously simplifying this example.
Is there a way to do this?
My alternative was to just create two uilabels, and set the attributes of each one accordingly, and then just add them together next to each other, but I thought it would be great if I could just assign the text of a label as per above instead.
By the way it doesn't have to be a UILabel as long as I can just render this text inside a UITableView row.
Thanks!
Here is a small example on how to use NSAttributedString:
var resultString = NSMutableAttributedString(string: "Hello ", attributes: [NSForegroundColorAttributeName : UIColor.redColor()])
var worldString = NSMutableAttributedString(
string: "World",
attributes: [
NSForegroundColorAttributeName : UIColor.redColor(),
NSFontAttributeName : UIFont.boldSystemFontOfSize(13.0)
]
)
resultString.appendAttributedString(worldString)
Then you can assign it to UILabel:
UILabel().attributedText = resultString

Resources