gpt4 book ai didi

ios - 标签 Objective-C 中的 SetLeftView

转载 作者:行者123 更新时间:2023-11-28 18:56:06 25 4
gpt4 key购买 nike

之前有人问过(使用文本字段,我问的是如何包含一个字符而不是一个小图标),是的,我已经尝试过使用这个 SetLeftView 来放置一个美元符号 ' $' 或我想要在文本字段旁边的任何字符。

但是,我不想要一个文本字段,而是一个标签,当我应用相同的代码来执行我想要的操作时,它会返回一个错误,我的意思是 Xcode 无法构建代码。

这是我的代码:

 // We add a label to the top that will display the results
self.resultLabel = [[UILabel alloc] initWithFrame:CGRectMake(25, 80, TEXTAREA_WIDTH, TEXTAREA_HEIGHT)];
[resultLabel setBackgroundColor:[UIColor clearColor]];
[resultLabel setText:@"01234"];
[resultLabel setFont:[UIFont fontWithName:@"AppleGothic" size:30.0f]];
resultLabel.textColor = RGB(255,255,255);
resultLabel.textAlignment = NSTextAlignmentCenter;
// Add it to the view
[self.view addSubview:resultLabel];


UILabel *dollarSignLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 80, 25, 40)];
dollarSignLabel.text = @"$";
dollarSignLabel.textColor = RGB(255,255,255);
[dollarSignLabel setFont:[UIFont fontWithName:@"AppleGothic" size:30.0f]];
[resultLabel setLeftView:dollarSignLabel];
[resultLabel setLeftViewMode:UITextFieldViewModeAlways];

Error: No visible @interface for 'UILabel' declares the selector 'setLeftView'. Same error in the line of setLeftViewMode.

同样,如果我使用文本字段,这会起作用。

我的工作代码(使用文本框)

// adding a textField for input
UITextField *myTextField = [[UITextField alloc] initWithFrame:CGRectMake(viewHalf-30, 100, 200, 40)];
[myTextField setBackgroundColor:[UIColor clearColor]];
[myTextField setText:@"0"];
[myTextField setFont:[UIFont fontWithName:@"AppleGothic" size:30.0f]];
myTextField.textColor = RGB(255,255,255);
[[self view] addSubview:myTextField];

UILabel *dollarSignLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 25, 40)];
dollarSignLabel.text = @"$";
dollarSignLabel.textColor = RGB(255,255,255);
[dollarSignLabel setFont:[UIFont fontWithName:@"AppleGothic" size:30.0f]];
[myTextField setLeftView:dollarSignLabel];
[myTextField setLeftViewMode:UITextFieldViewModeAlways];

最佳答案

你不能应用它的原因是因为 UILabel 没有像 UITextField 那样的名为 setLeftView 的方法。

你可以做的是:

  1. 创建两个彼此相邻的标签。
  2. 将左侧标签的 trailingSpace 设置为右侧标签的 0。根据需要安排其他约束。
  3. 将左侧标签的 textAlingment 属性设置为 NSTextAlignmentRight,将右侧标签的属性设置为 NSTextAlignmentLeft。
  4. 在左侧标签和数字上设置美元符号。

关于ios - 标签 Objective-C 中的 SetLeftView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32432113/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com