【问题标题】:Create price amount UILabel for iOS [duplicate]为iOS创建价格金额UILabel [重复]
【发布时间】:2013-08-02 13:26:01
【问题描述】:

我想知道是否有一种简单的方法可以在不使用子字符串和范围的情况下做到这一点。基本上我想完成在 UILabel 中输入价格金额。

例子:

当用户键入时,我想输入价格,显然那需要从右到左,一旦我得到美元 - 尽可能多地附加。我看了这个answer,但没有得到任何结果。这有点棘手,因为美元符号和小数点是 UILabel 的一部分,而现在我只是设置 UILabel = textfield.text 的文本。任何想法表示赞赏。

【问题讨论】:

标签: iphone ios objective-c uilabel


【解决方案1】:

这很简单。您可以自定义数字键盘。

方法是这样的:

  1. 为键盘上滑添加通知

    [[NSNotificationCenter defaultCenter] addObserver:self 选择器:@selector(keyboardWillShow:) 名称:UIKeyboardWillShowNotification 对象:无];

完成整个操作后,不要忘记将观察者从通知中心的适当位置移除:

[[NSNotificationCenter defaultCenter] removeObserver:self];

2.我们在keyboardWillShow方法中所要做的就是定位键盘视图并将我们的按钮添加到其中。正如其他人已经发现的那样,键盘视图是我们应用程序的第二个 UIWindow 的一部分(参见这个线程)。所以我们引用那个窗口(在大多数情况下它将是第二个窗口,所以下面代码中的objectAtIndex:1 很好),遍历它的视图层次结构,直到我们找到键盘并将按钮添加到它的左下角:

- (void)keyboardWillShow:(NSNotification *)note {  
    // create custom button
    UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
    doneButton.frame = CGRectMake(0, 163, 106, 53);
    doneButton.adjustsImageWhenHighlighted = NO;
    [doneButton setImage:[UIImage imageNamed:@"DoneUp.png"] forState:UIControlStateNormal];
    [doneButton setImage:[UIImage imageNamed:@"DoneDown.png"] forState:UIControlStateHighlighted];
    [doneButton addTarget:self action:@selector(doneButton:) forControlEvents:UIControlEventTouchUpInside];

    // locate keyboard view
    UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
    UIView* keyboard;
    for(int i=0; i<[tempWindow.subviews count]; i++) {
        keyboard = [tempWindow.subviews objectAtIndex:i];
        // keyboard view found; add the custom button to it
        if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES)
            [keyboard addSubview:doneButton];
    }
}

【讨论】:

  • 这个完成按钮是否出现在 iOS5、iOS6 和 iOS7 上?
  • 是的!此按钮将出现在所有 iOS SDK 版本上。
  • 不错!工作得很好
【解决方案2】:

您可以使用 AttributedString 在 UILabel 上显示 $ 符号。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-22
    • 2016-12-22
    • 2011-10-01
    相关资源
    最近更新 更多