【问题标题】:increasing font size continuously不断增加字体大小
【发布时间】:2013-03-02 16:34:36
【问题描述】:

如何在按住按钮时不断增加UILabel/UITextView/...的字体大小

我有一个:@property(nonatomic) CGFloat fontSize;ViewDidLoad 中初始化为 17

单击按钮时正在调用此函数:

- (IBAction)increaseFontSize:(id)sender {
    [self.label setFont:[UIFont fontWithName:@"Helvetica" size:self.fontSize++]];
    NSLog(@"FONT SIZE: %f", self.fontSize);
}

所以我需要的是按住按钮时不断增加字体,我想我应该以某种方式反复调用increaseFontSize:方法

【问题讨论】:

  • 我怀疑您必须在 touchDown 上设置一个计时器,然后在计时器滴答声上递增,直到 touchUp。
  • 您是否使用.xib?
  • 不,我正在使用 .storyboard

标签: objective-c font-size


【解决方案1】:
  1. 不要使用后自增运算符;这仅适用于整数。
  2. 您需要存储当前字体大小,以便在下一次迭代中增加:

瞧:

- (IBAction)increaseFontSize:(id)sender
{
    self.fontSize = self.fontSize + 1.0;
    [self.label setFont:[UIFont fontWithName:@"Helvetica" size:self.fontSize]];
    NSLog(@"FONT SIZE: %f", self.fontSize);
}

【讨论】:

  • 是的,但是如果我按住按钮不会重复增加字体
  • @AlexyIbrahim 是的。
  • 字体越来越大。完美的逻辑。只需要更改按钮事件选择。
【解决方案2】:

逻辑在上述答案中是完美的。
只需将 Button Event 设置为“Touch Drag Inside”即可。

根据我的理解,它在我的测试应用程序中工作。

谢谢。

【讨论】:

    【解决方案3】:

    ViewDidLoad:中添加这两行

     [self.button addTarget:self action:@selector(ActionStart:) forControlEvents:UIControlEventTouchDown];
        [self.button addTarget:self action:@selector(ActionEnd:) forControlEvents:UIControlEventTouchUpInside];
    

    然后写这个

    -(IBAction) ActionEnd:(id)sender{
        [myTimer invalidate];
        myTimer = nil;
    }
    -(IBAction) ActionStart:(id)sender{
        myTimer=[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(increaseSize) userInfo:nil repeats:YES];
    }
    -(void)increaseSize{
        self.fontSize = self.fontSize + 1.0;
        [self.label setFont:[UIFont fontWithName:@"Helvetica" size:self.fontSize]];
    }
    

    试试这个。 它会起作用的。

    【讨论】:

    • 这应该可以,但我认为第一种方法应该是 -(IBAction) ActionEnd:(id)sender
    猜你喜欢
    • 2018-07-16
    • 2015-02-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-25
    • 2017-08-31
    • 2018-04-18
    • 2018-12-12
    • 2018-12-29
    相关资源
    最近更新 更多