如何从UITextField中检索属性字符串?

时间:2013-07-18 22:36:40

标签: objective-c cocoa-touch uitextfield nsattributedstring

运行此程序时,我无法获得所需的结果。我想要构建的是一个attributer程序,它从UITextField中的用户获取文本,当他们单击Update按钮时,它会在文本字段上方的UILabel中显示文本。一旦我开始工作,我将添加按钮来编辑文本。

#import "TextChangerViewController.h"

@interface TextChangerViewController ()
@property (strong, nonatomic) IBOutlet UITextField *textInput;
@property (strong, nonatomic) IBOutlet UILabel *textOutput;
@property (strong, nonatomic) IBOutlet UIButton *updateButton;


@end

@implementation TextChangerViewController


- (void)updateText:(NSAttributedString *)input
{
    [self.textOutput setAttributedText:input];
}
- (IBAction)updateDisplayText:(UIButton *)sender
{

    [self updateText:[[NSAttributedString alloc]initWithString:self.textInput.text]];
}

@end

1 个答案:

答案 0 :(得分:0)

我假设您正在使用IB。如果你不是那么请纠正我。

执行此操作的一种简单方法是单击IB中的文本字段并更改其Text属性:

enter image description here

同样以完全相同的方式为您的UILabel 执行此操作。然后在你的方法中:

- (void)updateText:(NSAttributedString *)input
{
    self.textOutput.attributedText = input;
}

- (IBAction)updateDisplayText:(UIButton *)sender
{
    [self updateText:self.textInput.attributedText];
}

你可以很容易地将它组合成一种方法(我相信你知道),但我把它们与你的例子分开。最后,只需验证所有IBOutlets是否已正确关联。

相关问题