Skip to content

Instantly share code, notes, and snippets.

@MarksCode
Created April 22, 2017 08:24
Show Gist options
  • Select an option

  • Save MarksCode/139f7203b95bd5f445e653892b4d5553 to your computer and use it in GitHub Desktop.

Select an option

Save MarksCode/139f7203b95bd5f445e653892b4d5553 to your computer and use it in GitHub Desktop.
I want to set a label's text with a string that's partly bold. The words I want to make bold all begin with the same letter, say "~".
For example I could have the string, "This ~word is bold, and so is ~this"
Then the label's text would contain the string "This word is bold, and so is this" where "word" and "this" are bold.
Does anybody know if it's possible to make a function like this? I tried the following:
func makeStringBoldForLabel(str: String) {
var finalStr = ""
let words = str.components(separatedBy: " ")
for var word in words {
if word.characters.first == "~" {
var att = [NSFontAttributeName : boldFont]
let realWord = word.substring(from: word.startIndex)
finalStr = finalStr + NSMutableAttributedString(string:realWord, attributes:att)
} else {
finalStr = finalStr + word
}
}
}
but get the error:
`Binary operator '+' cannot be applied to operands of type 'String' and 'NSMutableAttributedString'`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment