panthor314 panthor314 - 2 years ago 102
Swift Question

How to format certain words of a cell's text label

I have a xamarin iOS project where I am displaying a cell with x lines of text. I currently define my cells like so:

public override UITableViewCell GetCell (UITableView tableView, Foundation.NSIndexPath indexPath)
UITableViewCell cell = tableView.DequeueReusableCell ("cells");
cell.TextLabel.Lines = 0;
cell.TextLabel.Text = "Bold: " + info1 + "Bold: " + info2;


Which displays the cell like:

|Bold: Testing |
|Bold: Testing2 |

I want to be able to turn just the
text to the bold font and keep the other info in normal text font. Is this possible given my current approach?

Answer Source

You could do like this:

let attributedText = NSMutableAttributedString(string: "YOUR FIRST TEXT STRING", attributes: [NSFontAttributeName: UIFont.boldSystemFontOfSize(20.0), NSForegroundColorAttributeName: UIColor.redColor()])
let anotherString = NSAttributedString(string: "YOUR SECOND TEXT STRING", attributes: [NSFontAttributeName: UIFont.systemFontOfSize(20.0), NSForegroundColorAttributeName: UIColor.blueColor()])
cell.TextLabel.AttributedText = attributedText

You can change your font size as well as color.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download