Is it possible to change only the height of a font? I imported a custom font of .ttf data type.
So far I am using this code:
let font = UIFont(name: "importedFont", size: 25.0)
label.font = font
EDIT! some clarifications: I don't want to change the width of the text because I am running low on space in x direction.
You can alter the layer for the label using affine transforms so that it only scales the text in one direction.
label.transform = CGAffineTransformMakeScale(1.0, 4.0) //(x-scale, y-scale)
Your question is very unclear so I'm not sure if this will answer it for you.
Related
In my project, i am using scaling for UI components. I am able to scale the text for UIlabel like below and it's working in all device:
1. Autoshrinks - minimum font scale set it to 0.5
2. No of lines - 0
3. Enable dynamic type in attribute inspector
4. adjustFontSizeToWidth to true
But when i am trying to adjust font for UI Button using beolow steps and i am not able to scale the text for UI button.
button.titleLabel?.numberOfLines = 1 // Tried with 0 also
button.titleLabel?.adjustsFontSizeToFitWidth = true
button.titleLabel?.lineBreakMode = // tried differenet linebreakmode
Could anyone have an idea of scaling UI Button text?
Are you sure it's not working?
Edit - After comments...
UIKit elements such as UILabel / UIButton / etc to not have a built-in "auto-adjust font height" property.
I don't work for Apple, so just guessing that is (at least in part) due to the fact that, in general...
Based on screen height, the UI is designed to either:
provide more or less information, e.g. more rows in a table, or
adjust vertical spacing between elements
That doesn't mean you can't or shouldn't adjust your font sizes... it just means you have to do it manually.
Couple options:
set the font-size at run-time, as suggested by Duncan
use a UIAppearance proxy to set the font-size, again at run-time
in either case, you could use a height-to-fontSize table or a "percentage" calculation.
Another option would be a custom class that sets the font-size based on the constrained button height.
Here's a simple example (note: for demonstration purposes only):
class AutoFontSizeButton: UIButton {
override func layoutSubviews() {
super.layoutSubviews()
guard let fnt = titleLabel?.font else { return }
// default system type button has 6-pts top / bottom inset
// and font size is 15/18ths of that height
let h = ((bounds.height - 12.0) * (15.0 / 18.0)).rounded()
let fs = fnt.pointSize
if h != fs {
titleLabel?.font = UIFont(descriptor: fnt.fontDescriptor, size: h)
}
}
}
Result - the top three (yellow) buttons are 30, 40 and 50-points in height, with the default font-size of 15. The bottom three (green) buttons are again 30, 40 and 50-points in height, but the font-size is automatically set at run-time:
I don't think there is a way to get the font to auto-size. However, if you set the button's titleLabel.font to a specific font size the button will update to use the new font size, including resizing the button.
Use code like this:
let size: CGFloat = useLargeFont ? 50.0 : 17.0 //Change as needed
if let buttonFont = button.titleLabel?.font {
button.titleLabel?.font = buttonFont.withSize(size)
}
I am using Custom Font in iOS application (Xamarin.Forms App). It is working fine but the text is clipped at the bottom of UILabel. It clearly cuts letter "g"
Custom Font: FuturaStd-Light.ttf
Actual Image In App:
Edited Image to ensure not height issue: Added Background color to Label to ensure it is not height issue.
I read here that to adjust ascender and descender property of Font. But still it doesn't help. Any help would be really appreciated.
Note: It is looking good if we remove custom font. The issue occurs only for the custom font that i am using.
you could try to use this:
NSMutableParagraphStyle paragraphStyle = new NSMutableParagraphStyle();
paragraphStyle.MinimumLineHeight = 24 + 1;//24 is your font size
NSMutableDictionary attributes = new NSMutableDictionary();//NSParagraphStyleAttributeName
attributes.SetValueForKey(paragraphStyle,new NSString("NSParagraphStyleAttributeName"));
label.AttributedText = new NSMutableAttributedString(label.Text,attributes);
My designer send me sketch file which says 'Line height: 22' for label. How can i achieve this in xcode interface builder.
Is there any way to define this line height using code or UI builder.
#bbjay did put me on the right track.
If you want to obtain the exact result of Sketch, the formula is:
paragraphStyle .lineSpacing = sketchLineHeight - font.lineHeight
Provided that the font was given sketchFontSize
I've found the following formula to work well for me.
It converts form Sketch line height to iOS line spacing:
lineSpacing = sketchLineHeight - sketchFontSize - (font.lineHeight - font.pointSize)
In code, for your case this would be:
let font = UIFont.systemFont(ofSize: 18) // or whatever font you use
textLabel.font = font
let attributedString = NSMutableAttributedString(string: "your text")
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineSpacing = 22 - 18 - (font.lineHeight - font.pointSize)
attributedString.addAttribute(.paragraphStyle, value: paragraphStyle, range: NSMakeRange(0, attributedString.length))
textLabel.attributedText = attributedString
Line height is coming from CSS, so your designer must have a web designer background. On the mobile platforms, we do not specify line height, but line spacing.
In general NSMutableParagraphStyle offers capabilities to modify multiline labels for iOS.
NSMutableParagraphStyle has a property called maximumLineHeight, but this will only set the maximum line height to a certain value, if the containment of the label would exceed a certain value.
To set this up in IB, you need to add the label, and change the Text property to Attributed. Than click on paragraph style icon, and set the line spacing for the label. Looking at the design, it is around 2 points of line spacing, what you need. You can either ask your designer to provide you with line spacing attribute or try to find the right line spacing value by randomly trying out different values.
In storyboard, use the Atributed style of UILabel. Below is example with 2.5 line height
I know that for changing the font of UINavigationBar he will simply do
self.navigationController!.navigationBar.titleTextAttributes = [NSFontAttributeName: UIFont(name: "MavenProBold", size: 15)!, NSForegroundColorAttributeName: UIColor.whiteColor()]
Tho, i'm trying to change only the font type, and keeping iOS default font size(which i guess he is dynamic among the devices).
Any suggestions?
If you change the font type, you will have to specify a font size. Generally you have to do this manually.
If you want to match the size of another font you see, say in another UILabel, you could do:
CGFloat fontSize = self.label.font.pointSize; // obj-c
let fontSize = CGFloat(label.font.pointSize) // swift
And then assign the size of the font you are setting to be this.
UPDATE
After thorough investigation, the common font size of the UINavigationBar is 34px, which is 17 in your Xcode editor. Remember that you can manually override this view, but that is the default font size.
I have a multi line label with a set size (300 x 300).
I want to adjust the label's font size programmatically according to how long the label's text is and how big the label is.
Here are 2 examples of the same sized labels with different length text strings
Refer to NSString.boundingRectWithSize:options:attributes:. Here you need to decrement the font size stepwise until the string will fit into the original frame.
var paragraph = NSMutableParagraphStyle()
let layout = [NSFontAttributeName:NSFont.systemFontOfSize(0), NSParagraphStyleAttributeName: paragraph, ]
paragraph.lineBreakMode = NSLineBreakMode.ByWordWrapping
var a = NSString(string: "My long text")
let rect = NSMakeSize(30, 20)
let bb = a.boundingRectWithSize(rect, options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: layout)
Place the above in Playground and modify rect and the font to see what happens.
I know the Question is old but....
Just use the option AutoShrink and set the minimum font size/scale in the Attributes Inspector, it will scale the font as large as possible to fit the size of the label.