简体   繁体   English

标签Objective-C中的SetLeftView

[英]SetLeftView In Label Objective-C

it has been asked before (using textfield, and I'm asking how to include a character not a small icon), and yes I have already tried using this SetLeftView to put a dollar sign '$' or whatever character I want beside the TEXTFIELD. 之前有人问过它(使用文本SetLeftView ,我SetLeftView是如何包含一个字符而不是一个小图标),是的,我已经尝试过使用此SetLeftView在美元SetLeftView放置美元符号“ $”或任何我想要的字符。

However, I do not want a textfield, but a label, and when I apply the same code to do what I want, it returns me an error, I mean Xcode fails to build the code. 但是,我不需要文本字段,而是标签,当我使用相同的代码执行所需的操作时,它返回一个错误,这意味着Xcode无法构建该代码。

Here is my code: 这是我的代码:

 // 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'. 错误:“ UILabel”没有可见的@interface声明选择器“ setLeftView”。 Same error in the line of setLeftViewMode. setLeftViewMode行中的相同错误。

Again, this works if I use a textfield. 同样,如果我使用文本字段,则此方法有效。

My working code (using textfield) 我的工作代码(使用文本字段)

// 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];

The reason you can't apply that because UILabel doesn't have a method named setLeftView as UITextField do. 之所以无法应用,是因为UILabel没有像UITextField那样具有名为setLeftView的方法。

What you can do is : 您可以做的是:

  1. Create two labels next to each other. 彼此相邻创建两个标签。
  2. Set left labels trailingSpace to right label to 0. Arrange other constraints whatever you want. 将左侧标签tailingSpace设置为0,将右侧标签设置为0。根据需要安排其他约束。
  3. Set left label's textAlingment property to NSTextAlignmentRight and right label's to NSTextAlignmentLeft. 将左标签的textAlingment属性设置为NSTextAlignmentRight,将右标签的属性设置为NSTextAlignmentLeft。
  4. Set dolar sign on a left label and numbers to another. 在左侧标签上设置美元符号,然后在另一个标签上编号。

hope this will help u out. 希望这会帮助你。

  1. add a dollar image on your label. 在标签上添加美元图像。
  2. override following method of UILabel 重写UILabel以下方法

     -(CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines { bounds.origin.x =+leftMargin; return bounds; } - (void)drawTextInRect:(CGRect)rect { [super drawTextInRect: CGRectInset(self.bounds, leftMargin , 0)]; } 

Since a label isn't editable by the user anyway, there is no reason not just to add your $ sign to the label itself. 由于标签仍然是用户不可编辑的,因此没有理由不只是将$符号添加到标签本身。

label.text = [@"$" stringByAppedingString:yourText];

if the special symbol should be an image instead, then look at NSTextAttachment & draw attributed Text 如果特殊符号应该是图像,则查看NSTextAttachment并绘制属性文本

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM