how to subtract a UILabel from another UILabel - uilabel

I have a totalPoints UILabel and I want to subtract another label from it every time I click a button here is the code
totalPoints -= String(Cost)
Thanks in advance!

You can't subtract one from the other, because UILabel deals with String.
Use something like this:
var totalPoints: Int = 100
var cost: Int = 4
then do the math in the function, and update the UILabel accordingly:
myLabel.text = String(totalPoints)

Related

How to add cell count in UILabel in Objective C

I am new in iOS and I am facing problem regarding to count cell of UITableView and add it in UILabel.
I need to do it as shown in Image
As in the Image there is count 1,2,3,4 .Did anybody done this.Thanks in Advance!
In your cellForRowAtIndexPath you just set the UILabel of the cell, like so:
cell.countLabel.text = [NSString stringWithFormat:#"%ld)", (long)indexPath.row +1];
Xcode 8.0 in
cell.countLabel.text =[nsstring stringwithformate:#"%d",indexpath.row+1];

Programmatically remove UILabel that has autolayout from View

I parse Json data and I set it on some UILabels, What I want is that when there's no text for a specific label It has to be removed from the UIView, I tried with 2 methods but with no results because the labels has constraints.
What I tried is:
Set their frame to 0 and the height constraint to 0
CGRect noFrame = _prepTime.frame;
noFrame.size.width = 0;
noFrame.size.height = 0;
[_prepTime setFrame:noFrame];
prepTimeHeight = 0;
But the height of the UILabel still remains,
The I tried with:
[_prepTime removeFromSuperView];
With this one the UILabel gets removed but the interface changes since it has constraints and by removing the UILabel I destroy the layout.
Is there any method to remove the UILabel from the view even if it has constraints?
You can set hidden if you don't want to affect all other subviews.
When using layout constraint it's not a good idea to edit the frame of your element. Instead you should add a IBOutlet property for your NSLayoutConstraint and link it to the height constraint of your label in your storyboard, then edit the constant value of the NSLayoutConstraint.
Someting like that in your viewcontroller #implementation:
#property IBoutlet NSLayoutConstraint *myLabelHeightConstraint;
and later when you want to hide your label :
self.myLabelHeightConstraint.constant = 0;
[self.view layoutIfNeeded];

How to content size of UIScrollview in Autolayout

i want to add 5 views of same size of simulator using UIScrollview. How could i can do? Specially content size of UIScrollview want to set which is approximately equals (320,2840) ?
Looking at your requirement, you need the srollview to scroll vertically as (320,2840)(w,h). While doing this auto layout doesnt create any problem as once your scroll view is set, the 5 views inside id doesnt need to be set via autolayout.
1) First create a Scrollview and set its position on a view. create and IBOutelet for scrollView and then in our viewDidAppear sets its content size to 320,2840 like this
[scrollView setContentSize:CGSizeMake(320, 2840)];
and then create your UIViews in a for loop and add them inside your scrollview with a help a variable say "y" which sets your UIviews y co-ordinate everytime before adding it to your scrollview.
like this
int y = 0;
for (int i = 0; i < 5; i++) {
UIView *insideviews = [[UIView alloc]init];
insideviews.frame = CGRectMake(0, y, 320, scr.frame.size.height);
insideviews.tag = i; // to distinguish them and use later
[scr addSubview:insideviews];
y = y + scr.frame.size.height;
}
thats its, if you still have any problem tell me else you can accept the answer :) Good Luck

UILabel in Custom Cell doesn't wrap

I want to show a string with more words in a label.
Here is my code in the viewDidLoad:
startLabel.lineBreakMode = NSLineBreakByWordWrapping;
startLabel.numberOfLines = 0;
startLabel.text = string;
I also connected the label to the interface builder and synthesized it in my viewController.
But when the string is too long just the words who totally fit in the label get displayed and the rest gets cut off.
What am I doing wrong here?

UILabel Highlighting Text

Can anyone quickly help me? I've been playing with this for hours and don't understand why this isn't working?
I'm trying to update highlighted text in a selected label (which is referenced in an array of UILabels previously defined).
This method is called by a receiving IBAction from a UISlider in the view interface.
However, when I retrieve the selected UILabel object from the array and set its HIGHLIGHTED property, there's no corresponding reaction on the view interface. I'm under the impression that its supposed to automatically redraw the view with text highlighted using the code below.
PS: My connections seem to all be correct the Interface Builder (i.e., IBOutlet UILabels are properly mapped/connected and the UISlider which triggers this method is connected through IBAction).
Am I missing something?
- (IBAction) changeHighlightedLabel: (id)sender
{
// Setup
UILabel *selectedLabel = [[UILabel alloc] init];
selectedLabel.highlightedTextColor = [UIColor greenColor];
// Interpret slider value and round to integer
UISlider *temp = sender;
float unroundedTempValue = [temp value];
float roundedTempValue = roundf(unroundedTempValue);
// Select the UILabel object from the UI Label array based on slider valuer
selectedLabel = [uiLabelArray objectAtIndex:roundedTempValue];
// Highlight the selected label
selectedLabel.highlighted = YES;
}
I've also tried substituting...
selectedCountryLabel = [[uiCountryLabelArray objectAtIndex:roundedTempValue] isHighlighted];
... for the last line. Still doesn't work.
Any feedback or help please? Thanks.
You are creating a UILabel and setting highlightedTextColor property to that first and then you are overwriting that with a UILabel from array. Since you are not setting any highlightedTextColor this time, the highlighted property will not work on label.
Change it as follows.
- (IBAction) changeHighlightedLabel: (id)sender
{
// Interpret slider value and round to integer
UISlider *temp = sender;
float unroundedTempValue = [temp value];
float roundedTempValue = roundf(unroundedTempValue);
// Select the UILabel object from the UI Label array based on slider valuer
selectedLabel = [uiLabelArray objectAtIndex:roundedTempValue];
// Highlight the selected label
selectedLabel.highlightedTextColor = [UIColor greenColor];
selectedLabel.highlighted = YES;
}