Skip to content

Instantly share code, notes, and snippets.

@jonathanduty
Created May 18, 2016 11:04
Show Gist options
  • Select an option

  • Save jonathanduty/c6a54eded71fb2de4408f0b61b53aad1 to your computer and use it in GitHub Desktop.

Select an option

Save jonathanduty/c6a54eded71fb2de4408f0b61b53aad1 to your computer and use it in GitHub Desktop.
create a 10.99 (with small "99") string that can be placed in a label.
func formatPriceString(costString:String,costLabel:UILabel) {
if let range = costString.rangeOfString(".") {
let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: costString)
let pos = costString.startIndex.distanceTo(range.startIndex)
let centsRange = NSMakeRange(pos, 3)
let smallFont = UIFont(name: costLabel.font.fontName,size:10)
let offsetAmount = self.costLabel.font.capHeight - smallFont!.capHeight
attributeString.addAttribute(NSFontAttributeName, value: smallFont!, range: centsRange)
attributeString.addAttribute(NSBaselineOffsetAttributeName, value: offsetAmount, range: centsRange)
self.costLabel.attributedText = attributeString
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment