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

问题描述:

尺寸不适用。如果我们使用系统字体,如何重新排列iOS 7中字体的大小?

如果我想让字体变大或变小,那么怎么办?

我该怎么办?

enter image description here

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

不确定您的意思是“尺寸不适用”。

您可以在.xib中更改UILabel的字体大小。打开它,并在右侧的实用程序面板中打开属性选项卡。你会在那里看到你的字体设置,你也可以改变它的大小。

如果你想这样做编程的命令是:

float fontSize = (whatever you want the size to be); 
myUILabel.font = [UIFont systemFontOfSize:fontSize]; 

希望你需要这个,

label.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:20 ]; 
      OR  
label.font = [UIFont systemFontOfSize:20]; 

所有ios system fonts这里。

+0

我不想要Verdana。我想要系统字体。 –

+0

@JimThio:更新.. –

+0

OP询问有关更改TextStyles的大小。你的答案是用固定的字体取代,这是不正确的。 –

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 

你需要这样的:

label.font = [[UIFont preferredFontForTextStyle:UIFontTextStyleHeadline] fontWithSize:label.font.pointSize * scale]; 

其中scale是有多少你调整原来的字体大小。