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?
Related
I currently have:
#IBOutlet var label: WKInterfaceLabel!
and
let myString = "Swift Attributed String"
let myAttribute = [ NSAttributedStringKey.foregroundColor: UIColor.blue ]
let myAttrString = NSAttributedString(string: myString, attributes: myAttribute)
label.setAttributedText(myAttrString)
Currently this code does not change the text of the label. Is there something obvious I am missing? Thanks for any help!
Your code is working perfectly fine but for only textcolor
You do not need to use attributed property you can directly use setTextColor() property.
label.setTextColor(UIColor.blue)
label.setText(myString)
You can refer WKInterfaceLabel and setTextColor
I have a Label with custom font Unica One-Regular. Since, it is not available in bold font, I am getting problem to make it bold. Is there any way to make it bold?. Here is the font link.
https://fonts.google.com/specimen/Unica+One?selection.family=Unica+One
Thanks in advance.
Sorry I’m on my iPhone but,
Please check the exact name of the font on Xcode, but this code should answer your question :
let attrs:[String:AnyObject] = [NSFontAttributeName: UIFont(name: "Unica One-Bold", size: 12)!]
let boldString = NSMutableAttributedString(string: "text", attributes:attrs)
let lbl = UILabel()
lbl.attributedText = boldString
There is a more recent answer to this question at Is it possible to increase the boldness of the font if the bold font file is not available?.
Short summary: if you have a list of font attributes, you can add a negative stroke width. For example (in Swift):
import AppKit
guard let chosenFont = NSFont(name: "VTypewriter Underwood", size: 24) else {
print("No such font")
exit(1)
}
var fontAttributes = [
NSAttributedString.Key.font:chosenFont,
NSAttributedString.Key.strokeWidth:NSNumber(value:-3.0)
]
let fakelyBoldedText = NSAttributedString(string:"Hello World", attributes:fontAttributes)
This is not optimal; it isn’t really bolding. But with low values for the stroke width (in my experiments, from about -3 to -5, possibly relative to the font width), it does look bolded.
I am trying to change the font of an label with should be displayed in the UI. I've used this lines:
let myAttribute = [ NSFontAttributeName: UIFont(name: "cirth1", size: 18.0)! ]
let buttonTitleNSMut = NSMutableAttributedString(string: "\(buttonTitle)", attributes: myAttribute )
let buttonTitle:String = "\(buttonTitleNSMut)"
let button = createButtonWithTitle(buttonTitle as String)
But my output isn't the label in the different font, but a long string with all the "specs" of the font...
What am I doing wrong? Maybe the fact that in the end I say it is a String?
Try the code snippet below for what you are trying to achieve.
let plainText = "\(buttonTitle)" as NSString
let attributedText = NSMutableAttributedString(string: plainText as String)
attributedText.addAttribute(NSFontAttributeName: UIFont(name: "cirth1", size: 18.0)!)
button?.titleLabel?.attributedText = attributedText
Hope that helps.
I would like to change the Center Text Font and Font Size of a PieChart. I would like to make it as big as it's possible inside the center circle. Is there any on-board feature that helps me to change the font and font size or do i have to overwrite some Framework classes?
Didn't find any useful information in the documentation :(
That's my basic Chart Formating method:
func setAttributes(view: PieChartView){
view.legend.enabled = true
view.descriptionText = ""
view.userInteractionEnabled = false
view.drawSliceTextEnabled = false
view.animate(xAxisDuration: 2.0, yAxisDuration: 2.0, easingOption: ChartEasingOption.EaseInOutBack)
}
Regards!
Swift 3
You can use this code to change font in swift 3.
let myAttribute = [ NSFontAttributeName: UIFont(name: "IranSansMobile", size: 15.0)! ]
let myAttrString = NSAttributedString(string: "My String", attributes: myAttribute)
chart.centerAttributedText = myAttrString
The centered text in pie chart is called centerAttributedText, in PieChartView. It's NSAttributedText, so you can define many custom attributes.
You can simply change its font and size like below:
centerText = #"Whatever you like";
[centerText setAttributes:#{
NSFontAttributeName: [UIFont fontWithName:#"HelveticaNeue-Light" size:12.f],
NSParagraphStyleAttributeName: paragraphStyle
} range:NSMakeRange(0, centerText.length)];
pieChartView.centerAttributedText = centerText;
It's in Objective-C, but should be easy for you to translate into swift, since you only need to care about the attributes
swift 5
var pieChartView = PieChartView()
...
let myAttrString = NSAttributedString(string: "My String", attributes: nil)
pieChartView.centerAttributedText = myAttrString
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: