How to add m² symbol at the end textField in swiftUI - swift

how is it possible to add a m² at the end of the textfield?
For example, to add a € character at the end of a textfield, I was able to solve it with the numberFormatter.
textfield with euro sign

You have several options. The simplest solution is to use Unicode characters. Just use the superscripted version of the number 2 (U+00B2):
self.textField.text = "112 m\u{00B2}"
Alternatively you can use NSAttributedString or NSMutableAttributedString:
let defaultFont = UIFont.systemFont(ofSize: 15)
let superscriptedFont = UIFont.systemFont(ofSize: 10)
let text = NSMutableAttributedString(string: "112 m", attributes: [.font: defaultFont])
text.append(NSMutableAttributedString(string: "2", attributes: [.font: superscriptedFont, .baselineOffset: 5]))
self.textField.attributedText = text

Related

How to set TextField's placeholder text to fit into the field

I have a textfield for searching phrases. This textfield has a placeholder with a little bit longer text (in Japanese). It is fine at iPhone 11, but it shortened at SE with .... I would like to know how set autoscaling with factor like 0.6 for this text.
Use placeholder programmatically:
let attributedPlaceholder = NSAttributedString(string: yourText,
attributes: [NSAttributedString.Key.foregroundColor: UIColor.darkGray])
label.adjustsFontSizeToFitWidth = true
label.minimumScaleFactor = 0.6
label.attributedText = attributedPlaceholder

Multiline multicolor attributed text in swift

How can I build something like this using attributed text? I tried adding line feed (\n), it didn't work, just displayed first line and cropped next two, removing \n will display two parts in single line:
let mutableAttributedString = NSMutableAttributedString()
let regularAttribute = [NSAttributedStringKey.font: UIFont(name: "Avenir-Light", size: 45.0), NSAttributedStringKey.foregroundColor: UIColor.yellow]
let boldAttribute = [NSAttributedStringKey.font: UIFont(name: "Avenir-Light", size: 25.0), NSAttributedStringKey.foregroundColor: UIColor.white]
let mutableAttributedString2 = NSMutableAttributedString()
let boldAttributedString = NSAttributedString(string: "person", attributes: boldAttribute)
let regularAttributedString = NSAttributedString(string: "\(self.requestCount)", attributes: regularAttribute)
mutableAttributedString2.append(boldAttributedString)
mutableAttributedString2.append(NSAttributedString(string: "\n"))
mutableAttributedString2.append(regularAttributedString)
self.btnStatsPeople.setAttributedTitle(mutableAttributedString2, for: .normal)
UILabel has one line by default.
This property controls the maximum number of lines to use in order to
fit the label’s text into its bounding rectangle. The default value
for this property is 1. To remove any maximum limit, and use as many
lines as needed, set the value of this property to 0.
UIButton creates default UILabel. So using 0 lines is a solution for your problem:
self.btnStatsPeople.titleLabel?.numberOfLines = 0

best way to customize uilabel and make it internationalizable

I'm changing my app to make it localizable/internationalizable, by adding multiple languages, so I have some texts on the storyboard and others in the swift files, so I have to make them internationalizable, but I don't know how to keep a customized font and make it internationalizable.
I have created these:
So if I understand the first strings files are for UILabel which are written in the storyboard. The second is for text in swift files also UIlabel linked in.
But I have to customize, fonts, colours... for some texts, and I don't know how to do for the UILabel in the storyboard, but for swift files I did it:
var main_string = "VOTRE EXPERT DU\nREGROUPEMENT DE CRÉDITS\nEN FRANCE DEPUIS 1998."
var string_to_color = "1998"
var string_to_bold = "REGROUPEMENT DE CRÉDITS"
var range_color = (main_string as NSString).range(of: string_to_color)
var range_bold = (main_string as NSString).range(of: string_to_bold)
let attributedString = [ NSAttributedStringKey.font: UIFont(name: "gotham-book", size: 18.0)! ]
let myString = NSMutableAttributedString(string: "VOTRE EXPERT DU\nREGROUPEMENT DE CRÉDITS\nEN FRANCE DEPUIS 1998.", attributes: attributedString)
NSLocalizedString("Welcome", comment: "")
myString.addAttribute(NSAttributedStringKey.foregroundColor, value: UIColor.orange , range: range_color)
let font = UIFont(name: "gotham-bold", size: 18.0)
myString.addAttribute(NSAttributedStringKey.font, value: font, range: range_bold)
accueilTopLabel.attributedText = myString
But how can I manage different languages with? Because I use ranges so it looks difficult. What the best way to manage it?

Putting a superscript in a label in xcode8

I am creating a math app and would like to have a superscript 2. I have been unable to find a solution for xcode 8 that isn't a base line off set
finalFunc.text = "Converted: \(printA)(x+\(printB))^2+\(printC)"
Updated for Swift4
let font:UIFont? = UIFont(name: "Helvetica", size:20)
let fontSuper:UIFont? = UIFont(name: "Helvetica", size:10)
let attString:NSMutableAttributedString = NSMutableAttributedString(string: "xcode8", attributes: [NSAttributedString.Key.font:font!])
attString.setAttributes([NSAttributedString.Key.font:fontSuper!,NSAttributedString.Key.baselineOffset:10], range: NSRange(location:5,length:1))
labelname.attributedText = attString
Use this code -
let font:UIFont? = UIFont(name: "Helvetica", size:20)
let fontSuper:UIFont? = UIFont(name: "Helvetica", size:10)
let attString:NSMutableAttributedString = NSMutableAttributedString(string: "xcode8", attributes: [NSFontAttributeName:font!])
attString.setAttributes([NSFontAttributeName:fontSuper!,NSBaselineOffsetAttributeName:10], range: NSRange(location:5,length:1))
label1.attributedText = attString;
Output -
For a simple to use Swift solution, you might want to checkout HandyUIKit. After importing it into your project (e.g. via Carthage – see instructions in README) you can do something like this:
import HandyUIKit
let someFont = UIFont.systemFont(ofSize: 20, weight: .medium)
"Converted: \(printA)(x+\(printB))^{2}+\(printC)".superscripted(font: someFont)
The last line will return an NSAttributedString which will look exactly like what you're looking for. Just assign it to a UILabels attributedText property and that's it!
If you're looking for subscripting a text, simply use subscripted(font:) instead. It will recognize structures like CO_{2}. There's also superAndSubscripted(font:) if you want to combine both.
See the docs for more information and additional examples.

Swift UITextView with different formatting

Sorry for a basic question, but I'm not sure where to start. Is the following possible in Swift?
In a UITextView (Not a label, as in the possible duplicate), different bits of text having different formatting: for instance, a line of large text in the same UITextView as a line of small text. Here is a mockup of what I mean:
Is this possible in one UITextView?
You should have a look at NSAttributedString. Here's an example of how you could use it:
let largeTextString = "Here is some large, bold text"
let smallTextString = "Here is some smaller text"
let textString = "\n\(largeTextString)\n\n\(smallTextString)"
let attrText = NSMutableAttributedString(string: textString)
let largeFont = UIFont(name: "Arial-BoldMT", size: 50.0)!
let smallFont = UIFont(name: "Arial", size: 30.0)!
// Convert textString to NSString because attrText.addAttribute takes an NSRange.
let largeTextRange = (textString as NSString).range(of: largeTextString)
let smallTextRange = (textString as NSString).range(of: smallTextString)
attrText.addAttribute(NSFontAttributeName, value: largeFont, range: largeTextRange)
attrText.addAttribute(NSFontAttributeName, value: smallFont, range: smallTextRange)
textView.attributedText = attrText
The result: