Swift 3.0 convert Double() to NSMutableAttributedString


in advance thanks for help.

I am trying to make calculator application (for specific purposes) and I would like to know, if there exist a way how to convert Double() to NSMutableAttributedString. I need this for label output answer.

Reason of using NSMutableAttributedString is because I would like to have answer with subscripts and upper-scripts.

//example of my code var a = Double(), b = Double(), c = Double() a = Double(textField1.text!) b = Double(textField2.text!) c = a + b let font:UIFont? = UIFont(name: "Courier", size:12) let fontSuper:UIFont? = UIFont(name: "Courier", size:10) //for x_1 (subscript for "1") x1_t:NSMutableAttributedString = NSMutableAttributedString(string: "x1", attributes: [NSFontAttributeName:font!]) x1_t.setAttributes([NSFontAttributeName:fontSuper!,NSBaselineOffsetAttributeName:-4], range: NSRange(location:1,length:1)) var result = NSMutableAttributedText() // what to do to get output for a label like "x_1 = String(c) m"

If there exist another way like append String() to NSAtributedString() - I am looking forward for answers.


As I understand it, your input strings (named "prestring1" and "afterstring1" in your own answer) could just be normal strings without attributes, because you only need the final result to be an attributed string.

This would drastically simplify your function, for example you could use string interpolation first and then only make an attributed string and move up (or down) the last part (or any part you want, I'm using an hardcoded range in my example but it's just an example).


let randomstring = "Random =" let afterstring = "m2" let result: Double = 42.1 func stringer (pre: String, result: Double, post: String) -> NSMutableAttributedString { let base = "\(pre) \(result) \(post)" let mutable = NSMutableAttributedString(string: base) mutable.addAttribute(NSBaselineOffsetAttributeName, value: 4, range: NSRange(location: mutable.length - 2, length: 2)) return mutable } let attributedString = stringer(pre: randomstring, result: result, post: afterstring)


<a href="https://i.stack.imgur.com/fQkQ4.png" rel="nofollow"><img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/fQkQ4.png" data-original="https://i.stack.imgur.com/fQkQ4.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /></a>


I am still not quit sure how to do it, but I could create simple function, which is approximately doing what I need. Here I am sharing my answer in case someone has the same question, but in case someone knows better answer, share it with others :)

var randomstring = "Random =" var prestring1 = NSMutableAttributedString(string: randomstring) var afterstring1 = NSMutableAttributedString(string: "m2") var result1 = Double() result1 = 42.1 func stringer (prestring: NSMutableAttributedString, result: Double, afterstring: NSMutableAttributedString) -> NSMutableAttributedString { var mutableatributedresult = NSMutableAttributedString(string: String(result)) var mutableaddition = NSMutableAttributedString(string: " ") var alltext = NSMutableAttributedString() alltext.append(prestring) alltext.append(mutableaddition) alltext.append(mutableatributedresult) alltext.append(mutableaddition) alltext.append(afterstring) return alltext } stringer(prestring: prestring1, result: result1, afterstring: afterstring1) //This should give result of "Random = 42.1 m2"

If someone knows better solution I am curious.


  • changing screen brightness via android app
  • How to add a Tkinter Text Variable in canvas text
  • Why is Auth::attempt always returns false in Laravel 5.3?
  • Form not validating correctly zend framework 2
  • R&OS PDF in PHP (name of pdf)
  • Pick and move a node in a pannable/zoomable Pane
  • Monospaced font/symbols for JTextPane
  • Pass two lines of text into InputBox
  • R and readLines of webpage text
  • Twilio : Not able to set channel attribute
  • UITextView initial scroll position is not 0
  • Write text inside UIBezierPath
  • Swift 3.0 convert Double() to NSMutableAttributedString
  • UIAlertController and UIAlertControllerStyleActionSheet customization
  • Font awesome icon button with label below
  • How to connect string from my class to form
  • How to make the horizontal scroll bar appear in a JScrollPane that contains a JTextPane component
  • How would I run this as an apple executable file?
  • C# get font from postscript name
  • Can't get attributed string to work in Swift
  • Create Vertical lines between Uilabels inside of a stack view [duplicate]
  • How to use NSWritingDirectionAttributeName in swift?
  • How to handle Oracle Store Procedure call with Oracle Types as input or output using EclipseLink
  • Force ggplot legend to show all categories when no values are present [duplicate]
  • How to move object along the polygons
  • Java Circle-Circle Collision Detection
  • No projects found to import
  • Adding/Removing Lines from a JPanel
  • Laravel lmutator $this->attributes return 'Undefined index: id'
  • CSS Grid, position absolute an element in a css grid item: IMPOSSIBLE
  • how to get data attributes of dynamically generated element
  • ListItem.Attributes.Add not working
  • How to change the font size of a single index for UISegmentedControl?
  • DirectX11 ClearRenderTargetViewback with transparent buffer?
  • How to set my toolbar fixed while scrolling android
  • Change an a tag attribute in JavaScript based on screen width
  • AT Commands to Send SMS not working in Windows 8.1
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • apache spark aggregate function using min value
  • Sorting a 2D array using the second column C++