0

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.

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.

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'. Same error in the line of 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];
  • 2
    There is no `leftView` in [UILabel](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UILabel_Class/). – Pang Sep 07 '15 at 06:20
  • Thanks Pang, is there any way to put my special character beside my label? – user5295097 Sep 07 '15 at 06:27
  • possible duplicate of [How to embed small icon in UILabel](http://stackoverflow.com/questions/19318421/how-to-embed-small-icon-in-uilabel) – Anbu.Karthik Sep 07 '15 at 06:30

3 Answers3

2

The reason you can't apply that because UILabel doesn't have a method named setLeftView as UITextField do.

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.
  3. Set left label's textAlingment property to NSTextAlignmentRight and right label's to NSTextAlignmentLeft.
  4. Set dolar sign on a left label and numbers to another.
Sabrican Ozan
  • 738
  • 3
  • 9
  • ok Sabrican, I'm hardcoding the whole ui, so I don't know how to do such trailing space, but I get your point. So what I did is that I retain the two labels. 1 label for dollar sign, and the other is for the numeric values. I set their textAlignment to Center (yes, both center). But changing their X value of CGRectMake solves the problem :) Thanks guys! – user5295097 Sep 07 '15 at 07:19
1

hope this will help u out.

  1. add a dollar image on your label.
  2. override following method of 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)];
    }
    
Pang
  • 9,564
  • 146
  • 81
  • 122
Anurag Bhakuni
  • 2,379
  • 26
  • 32
  • don't get it - that would would just inset the text, no? – Daij-Djan Sep 07 '15 at 06:52
  • thanks Lea-rner. Gahh, I hate myself for having a small knowledge in oop. :( I'm just a beginner, I don't know how to override in objective-c). – user5295097 Sep 07 '15 at 07:09
  • Yes but why inset it all... You mean you wanna do a custom subclass? – Daij-Djan Sep 07 '15 at 07:10
  • yes... @Daij-Djan absolutely,, the above answer is one of the many ways by which we can achieve it. – Anurag Bhakuni Sep 07 '15 at 08:41
  • @Lea-rner I got it now - I'd urge you to rewrite it a bit "add a dollar image on your label." doesn't make it clear you want a subclass that draws $ -- I guess in draw rect. -- anyway, I'd NOT recommend this as it is 'overkill' ;) but it IS valid (I didn't down vote or sth. ;)) – Daij-Djan Sep 07 '15 at 09:01
1

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

Daij-Djan
  • 49,552
  • 17
  • 113
  • 135
  • Hi Daij-Djan, I get your point. I have two labels, 1 label is the dollar sign, and the 2nd label is the value, basically like a calculator, but not exactly a calculator. So I will be needing the value in the future... Hmmmm... Or I can just manipulate string to get the value. :D What do you think? Sorry I'm noob in REAL software development, specially in objective-c – user5295097 Sep 07 '15 at 07:07
  • Never get a value from a view (except user input) – Daij-Djan Sep 07 '15 at 07:11
  • Have a 'datamodel that holds values and state' – Daij-Djan Sep 07 '15 at 07:12