简体   繁体   English

如何动态读取和切换UILabel的字体大小?

[英]How to Read and Chage Font Size of an UILabel Dynamically?

I have a problem at changing the font size in an UILabel dynamically. 我在动态更改UILabel中的字体大小时遇到​​问题。 In my app, I have two labels. 在我的应用程序中,我有两个标签。

  1. The first label's size is 90*59 第一个标签的尺寸为90 * 59
  2. The second label's size is 59*59 第二个标签的尺寸是59 * 59

Now the problem is, The first label's font size may vary during run time. 现在的问题是,第一个标签的字体大小可能会在运行时变化。 I have to change the second label's font size according to the first label's font size. 我必须根据第一个标签的字体大小更改第二个标签的字体大小。

I have used the following method but it doesn't works. 我使用了以下方法,但它不起作用。

    CGFloat secondLabelsize = firstLabel.font.pointSize;
    [seconLabel setFont: [UIFont fontWithName: @"Exo-Light" size:secondLabelsize]];

    viewDidLod
    {
    [hr_lbl setFont: [UIFont fontWithName: @"Exo-Light" size:55]];
    }

Every time i have got label size as 55 only. 每次我的标签尺寸只有55。 Pls suggest me a to get current font size(first label) instead of defined font size at viewdidload. 请建议我在viewdidload获取当前字体大小(第一个标签)而不是定义的字体大小。

Here is the answer, 这是答案,

label2.frame.size.width == label1.frame.size.width; label2.frame.size.width == label1.frame.size.width;

Note : if i use singe "=" its an error. 注意:如果我使用singe“=”则出错。 If i use "==" it gives a warning but gives expected output too. 如果我使用“==”它会发出警告,但也会给出预期的输出。 Really confused what's happening there. 真的很困惑那里发生的事情。 Any one please guide me to understand this. 任何人请指导我理解这一点。

  1. The @"Exo-Light" font is not able to be set to 55. It'a a font issue. @“Exo-Light”字体无法设置为55.这是一个字体问题。 Try using sandart @"Helvetica" to make sure the code works, then find a font closer to your exo-light (that will behave correctly, of course). 尝试使用sandart @“Helvetica”来确保代码正常工作,然后找到一个更接近外部灯光的字体(当然会表现正常)。

  2. Also make sure the labels are properly hooked up in xib. 还要确保标签正确连接在xib中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM