【问题标题】:How do I retrieve an attributed string from UITextField?如何从 UITextField 检索属性字符串?
【发布时间】:2023-04-09 10:46:01
【问题描述】:

当我运行这个程序时,我没有得到想要的结果。我正在尝试构建的是一个属性程序,它在 UITextField 中从用户那里获取文本,当他们单击更新按钮时,它会在文本字段上方的 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

【问题讨论】:

    标签: objective-c cocoa-touch uitextfield nsattributedstring


    【解决方案1】:

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

    一个很好且简单的方法是单击 IB 中的文本字段并更改其 Text 属性:

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

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

    您可以轻松地将其组合成一种方法(我相信您知道),但我将它们分开,就像您的示例一样。最后,只需验证您的所有 IBOutlets 都已正确链接。

    【讨论】:

      猜你喜欢
      • 2016-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-26
      • 1970-01-01
      • 2015-10-29
      • 1970-01-01
      相关资源
      最近更新 更多