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);
Related
I am new to iOS Development.I am having problem with font size with phone screen size.For example Font size in iPhone 8 Plus looks fine but that text size is bigger in iPhone SE.I tried check Dynamic Type to Automatically Adjusts Font.And try to play with Autoshrink in StoryBoard.And i also tried to Add Font Variation in storyBoard.But I didnt get any good solution.Hope you understand my problem.Thanks in advance
Try this
class func dynamicFontSizeForIphone(fontSize : CGFloat) -> CGFloat
{
var current_Size : CGFloat = 0.0
current_Size = (UIScreen.main.bounds.width/320) //320*568 is my base
let FinalSize : CGFloat = fontSize * current_Size
return FinalSize
}
hope this work
You can change font size by using constraints.
1.take a label give its basic two constraint to satisfy. give one more constraint of equal.width to parent view. keep width as wide as your label text is.(a bit more than label text).
In attribute inspector there is a property name 'auto shrink' set it to 'minimum font size'
thats it.
Note: This will work fine if your Label text is constant. For changeable text there will be other approaches.
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.
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.
I am having the following code snippet in my application screen to show a label text in some bigger and good font. It looks good to me. But i also want to make this font to "bold" as well. Currently it doesn't show in boldfont as well. Could someone guide me how can i provide the label field text with the same font but also with bold ??? (or) Please tell me how can i make the label text with any other bigger size(24) font with bold?
LabelField subTitleLabel = new LabelField ("My App Sub Title", LabelField.FIELD_LEFT);
final FontFamily fontFamily[] = FontFamily.getFontFamilies();
final Font font10 = fontFamily[1].getFont(FontFamily.CBTF_FONT, 24);
subTitleLabel.setFont(font10);
subTitleLabel.setMargin(25, 0, 0, 10);
horEventFldManager.add(subTitleLabel);
This assumes that subTitleLabel is already set to the font that you want. It is just resetting it to a Bold version of that font.
subTitleLabel.setFont(subTitleLabel.getFont().derive(Font.BOLD));