gpt4 book ai didi

objective-c - 如果我们使用系统字体,如何在 iOS 7 中重新排列字体大小?

转载 作者:太空狗 更新时间:2023-10-30 04:01:00 24 4
gpt4 key购买 nike

尺寸不适用。

如果我想让字体大于或小于怎么办?

我该怎么办?

enter image description here

我认为这取决于 UILabel 的大小。但是,调整 UILabel 的大小将不起作用。

最佳答案

iOS 7 使用一种叫做动态类型的东西。无需指定确切的字体及其大小和所有内容,您可以通过语义描述它,例如,该字体用于标题、正文或任何您想要的。实际字体取决于可以动态更改的各种参数,其中之一是用户在“首选项/常规/文本大小”中选择的首选字体大小。您不能只选择标题的大小,因为那样会使整个概念变得毫无意义。这不是您的选择,而是用户的选择。您只需要倾听用户的选择并做出相应的回应。但是,您可以通过编程方式缩放首选字体。 UIFontDescriptor对象用于获取当前标题大小,之后 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

关于objective-c - 如果我们使用系统字体,如何在 iOS 7 中重新排列字体大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19993022/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com