大小不适用。
如果我想要字体比较大或小怎么办?
我该怎么做?
我原以为这取决于UILabel的大小。但是,调整UILabel的大小并不能起作用。
大小不适用。
如果我想要字体比较大或小怎么办?
我该怎么做?
我原以为这取决于UILabel的大小。但是,调整UILabel的大小并不能起作用。
fontWithDescriptor:size:
方法获得一个新字体,它具有相同的描述符,但是具有新的缩放大小。@interface SomeViewController ()
@property (weak, nonatomic) IBOutlet UILabel *headlineLabel;
@end
@implementation SomeViewController
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
// We need to setup our fonts whenever controller appears.
// to account for changes that happened while
// controller wasn't on screen.
[self setupFonts];
// Subscribing to UIContentSizeCategoryDidChangeNotification
// to get notified when user chages the preferred size.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(preferredFontChanged:)
name:UIContentSizeCategoryDidChangeNotification
object:nil];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillAppear:animated];
// Unsubscribe from UIContentSizeCategoryDidChangeNotification.
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIContentSizeCategoryDidChangeNotification
object:nil];
}
- (void)setupFonts
{
// In this method we setup fonts for all the labels and text views
// by calling 'preferredFontForTextStyle:scale:'.
self.headlineLabel.font = [self preferredFontForTextStyle:UIFontTextStyleHeadline scale:0.8];
}
- (UIFont *)preferredFontForTextStyle:(NSString *)style scale:(CGFloat)scale
{
// We first get prefered font descriptor for provided style.
UIFontDescriptor *currentDescriptor = [UIFontDescriptor preferredFontDescriptorWithTextStyle:style];
// Then we get the default size from the descriptor.
// This size can change between iOS releases.
// and should never be hard-codded.
CGFloat headlineSize = [currentDescriptor pointSize];
// We are calculating new size using the provided scale.
CGFloat scaledHeadlineSize = lrint(headlineSize * scale);
// This method will return a font which matches the given descriptor
// (keeping all the attributes like 'bold' etc. from currentDescriptor),
// but it will use provided size if it's greater than 0.0.
return [UIFont fontWithDescriptor:currentDescriptor size:scaledHeadlineSize];
}
- (void)preferredFontChanged:(NSNotification *)notification
{
[self setupFonts];
}
@end
label.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:20 ];
OR
label.font = [UIFont systemFontOfSize:20];
这里列出了所有的iOS系统字体。
不确定您所说的“大小不适用”是什么意思。
您可以在.xib中更改UILabel的字体大小。打开它,在右侧的实用程序面板中打开属性选项卡。您将在那里看到您的字体设置,也可以更改大小。
如果您想以编程方式执行此操作,则命令为:
float fontSize = (whatever you want the size to be);
myUILabel.font = [UIFont systemFontOfSize:fontSize];
label.font = [[UIFont preferredFontForTextStyle:UIFontTextStyleHeadline] fontWithSize:label.font.pointSize * scale];
其中scale
是您调整原始字体大小的比例。