How do I set bold and italic on UILabel of iPhone/iPad?
sectionLabel.font = [UIFont fontWithName:@"TrebuchetMS-Bold" size:18];
There is a list of font names that you can set in place of 'fontWithName' attribute.The link is here
UILabel font : bold and italic
try this code...
UIFont *yourFont = [UIFont fontWithName:@"Helvetica-BoldOblique"
size:[UIFont systemFontSize]];
lblTitle.font = yourFont;
you can use also another fontName for BoldItalik like.
Optima-BoldItalic
,TimesNewRomanPS-BoldItalicMT
,Baskerville-BoldItalic
,HelveticaNeue-BoldItalic
,etc...
Set part of a UILabel's text bold and another part italic
You can do this by using NSMutableAttributedString
NSMutableAttributedString *strText = [[NSMutableAttributedString alloc] initWithString:@"Setting different for label text"];
[strText addAttribute:NSFontAttributeName
value:[UIFont fontWithName:@"Helvetica-Bold" size:22]
range:NSMakeRange(0, 10)];
[strText addAttribute:NSFontAttributeName
value:[UIFont fontWithName:@"Helvetica-Italic" size:22]
range:NSMakeRange(10, 10)];
Swift 4 code:
var strText = NSMutableAttributedString(string: "Setting different for label text")
strText.addAttribute(.font, value: UIFont(name: "Helvetica-Bold", size: 22)!, range: NSRange(location: 0, length: 10))
strText.addAttribute(.font, value: UIFont(name: "Helvetica-Italic", size: 22)!, range: NSRange(location: 10, length: 10))
Make part of a UILabel bold in Swift
You will want to use attributedString
which allows you to style parts of a string etc. This can be done like this by having two styles, one normal, one bold, and then attaching them together:
let boldText = "Filter:"
let attrs = [NSAttributedString.Key.font : UIFont.boldSystemFont(ofSize: 15)]
let attributedString = NSMutableAttributedString(string:boldText, attributes:attrs)
let normalText = "Hi am normal"
let normalString = NSMutableAttributedString(string:normalText)
attributedString.append(normalString)
When you want to assign it to a label:
label.attributedText = attributedString
How to make an italic UILabel without caring about the size?
You can make use of [UIFont systemFontSize]
.
myLabel.attributedText = [[NSAttributedString alloc]
initWithString: @"my text"
attributes: @{
NSFontAttributeName: [UIFont italicSystemFontOfSize:
[UIFont systemFontSize]]}]];
Moreover, there are few more options to choose from:
+ (CGFloat)labelFontSize;//Returns the standard font size used for labels.
+ (CGFloat)buttonFontSize;//Returns the standard font size used for buttons.
+ (CGFloat)smallSystemFontSize;//Returns the size of the standard small system font.
+ (CGFloat)systemFontSize;//Returns the size of the standard system font.
How to set a bold dynamic font type for UILabel?
Ok, it seems the documentation provides a hint. For using custom fonts, I consider a bold
font as a custom font now, one can use the UIFontMetrics
class.
If I pass a "bold custom font" to the scaledFontForFont
method of UIFontMetrics
class I get the desired result with the dynamic font type UIFontTextStyleBody
.
UIFont* boldSystemFont = [UIFont boldSystemFontOfSize:17];
myLabel.font = [[UIFontMetrics metricsForTextStyle:UIFontTextStyleBody] scaledFontForFont:boldSystemFont];
So far I only see this way using code. No idea if this could somehow be setup in the Interface Builder as well.
Related Topics
Adding a View Controller as a Subview in Another View Controller
How to Programmatically Check Whether a Keyboard Is Present in iOS App
How to Get the Color of a Pixel in a Uiimage With Swift
Using Custom Fonts in Wkwebview
How to Perform Wireless Debugging in Xcode 9 With iOS 11, Apple Tv 4K, etc
Cordova App Not Displaying Correctly on Iphone X (Simulator)
Download and Install an Ipa from Self Hosted Url on Ios
Ios7 Uitextview Contentsize.Height Alternative
Error: "The Sandbox Is Not in Sync with the Podfile.Lock..." After Installing Restkit with Cocoapods
How to Hide a Status Bar in Ios
How to Determine Uiwebview Height Based on Content, Within a Variable Height Uitableview
How to Convert from Degrees to Radians
iOS Detect If User Is on an iPad