Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/96.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 以编程方式将%sign添加到UITextField_Objective C_Ios_Cocoa Touch - Fatal编程技术网

Objective c 以编程方式将%sign添加到UITextField

Objective c 以编程方式将%sign添加到UITextField,objective-c,ios,cocoa-touch,Objective C,Ios,Cocoa Touch,我想知道如何将%符号添加到UiTextfield中插入的任何数字中,以便用户知道它是一个百分比,类似于在excel中设置单元格类型时$或%符号的工作方式 我已经查看了所有的堆栈溢出,但想知道除了使用观察者追加%之外,是否还有其他方法。您想要的是UITextField的rightView属性。下面是我编写的一个小类别,可以帮助您为文本字段设置永久前缀或后缀: @implementation UITextField (Additions) - (void)setPrefixText:(NSStri

我想知道如何将%符号添加到UiTextfield中插入的任何数字中,以便用户知道它是一个百分比,类似于在excel中设置单元格类型时$或%符号的工作方式


我已经查看了所有的堆栈溢出,但想知道除了使用观察者追加%之外,是否还有其他方法。

您想要的是
UITextField
rightView
属性。下面是我编写的一个小类别,可以帮助您为文本字段设置永久前缀或后缀:

@implementation UITextField (Additions)

- (void)setPrefixText:(NSString *)prefix
{
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
    [label setBackgroundColor:[UIColor clearColor]];
    [label setFont:[UIFont fontWithName:self.font.fontName size:self.font.pointSize]];
    [label setTextColor:self.textColor];
    [label setAlpha:.5];
    [label setText:prefix];

    CGSize prefixSize = [prefix sizeWithFont:label.font];
    label.frame = CGRectMake(0, 0, prefixSize.width, self.frame.size.height);

    [self setLeftView:label];
    [self setLeftViewMode:UITextFieldViewModeAlways];
    [label release];
}

- (void)setSuffixText:(NSString *)suffix
{
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
    [label setBackgroundColor:[UIColor clearColor]];
    [label setFont:[UIFont fontWithName:self.font.fontName size:self.font.pointSize]];
    [label setTextColor:self.textColor];
    [label setAlpha:.5];
    [label setText:suffix];

    CGSize suffixSize = [suffix sizeWithFont:label.font];
    label.frame = CGRectMake(0, 0, suffixSize.width, self.frame.size.height);

    [self setRightView:label];
    [self setRightViewMode:UITextFieldViewModeAlways];
    [label release];
}

@end

顺便说一句:4099目前的结果。

使用
UITextFieldDelegate
textfieldDendediting:
方法

[NSString stringWithFormat:@"%d%%", number];
例如:

- (void)textFieldDidEndEditing:(UITextField *)textField {

    NSString *oldText = textField.text;

    textField.text = [NSString stringWithFormat:@"%@ %%",oldText];
}

这个解决方案非常好,非常感谢,它很好地使用了类别。iOS 7中值得注意的是,它将右/左视图向下偏移了1px。我已通过将标签插入另一个视图并将其偏移1px来修复此问题。谢谢您的帮助,但我希望避免在UITextField中手动添加百分号。