0

I am trying to make a calculator app and currently all numbers entered are displayed on a UILabel. When the "Power (^)" button is pressed, I want it to make the following text a superscript to what was previously entered (so I want it to be smaller and by the top right corner). So if I enter "2" then "^" then "0", I want it to show 2° (2 to the power 0).

I haven't tried much since I am not even sure where to start, but I was thinking maybe I could create a new UILabel which is smaller and in the top right corner of the last piece of text entered, but I don't know how to know where to place the little UILabel (I don't know how to get the position of the last letter/number).

I have the same problem with the divide, when divide is pressed I want it to be in the form of a fraction with text in the numerator, a horizontal line, and then text under for the denominator. But I have a feeling I will be able to solve both of these if I can figure out how to just solve one.

This is what my app currently looks like >> enter image description here

Ken
  • 1,155
  • 2
  • 19
  • 36

2 Answers2

1
   let font:UIFont? = UIFont.systemFont(ofSize: 20.0)
   let fontSuper:UIFont? = UIFont.systemFont(ofSize: 10.0)
   let attString:NSMutableAttributedString = NSMutableAttributedString(string: "YourLabel", attributes: [.font:font!])
   attString.setAttributes([.font:fontSuper!,.baselineOffset:10], range: NSRange(location:4,length:5))
   yourLabel.attributedText = attString
0

I think you don't need another UILabel. There's a answer regarding this in the below link : How to use subscript and superscript in Swift

SM Abu Taher Asif
  • 2,221
  • 1
  • 12
  • 14
  • This should be a comment, not an answer. If it is a duplicate question, [vote to close](/help/privileges/close-questions) as such and/or leave a comment once you [earn](//meta.stackoverflow.com/q/146472) enough [reputation](/help/whats-reputation). If not, *tailor the answer to this specific question*. – double-beep Jun 19 '19 at 09:30
  • I don't have 50 reputations needed for commenting and this is a duplicate question to the link shared in my answer – SM Abu Taher Asif Jun 19 '19 at 09:42