Update UILabel with Global NSString - iphone

In my delegate i have an NSInteger healthInt; and NSMutableString * healthString. In appDelegate.m i have set healthString to hold the changing value of healthInt. Then in a different view i have UILabel * healthLabel. And I have set healthLabel to display healthString with the following code.
healthLabel.text = [NSString stringWithFormat:#"%#", appDelegate.healthString];
This works and displays the number 100, which is what i have set healthInt to in the appDelegate. But when a UIImageView mainSprite collides with a different ImageView healthInt should decrease by two. Which it does, i can tell because i can see it happen in the log. The log may change and display the values of the slowly decreasing healthInt, but the healthLabel doesn't update as healthInt decreases. My question is how can i get this healthLabel to update as healthInt decreases? I have tried just putting in that code for the collision detection between mainSprite and the other ImageView that causes damage but that doesn't seem to work. Thanks!

If you want to get your UILabel to update every time healthInt or healthString changes, there's a couple ways to do this.
One way is to go into the setter method for healthInt (or create one instead of using #synthesize) and broadcast a NSNotification for each time your healthInt number changes. And then when the view with that label is visible, have an observer registered pick up on that notification and make a change to your label.
Another way is to use Key Value Coding & bind the UILabel's text field to healthString. Here is a related question that may help you use this possible solution.

Related

UILabel does not wrap (if I change default font size)

I am working on a simple storyboard prototype. My TableViewController uses Dynamic Prototype as Content.
I have a cell with 4 label of which two will be set in code (the label text). The height of the cell will be calculated in code too. The Line Breaks are set to Word Wrap and everything's working fine with the default values (System 17.0):
see here:
..but if I change the Font Size of the "Fantasy Street..." label it will not break any more instead it just will be cut off!
see here: with System Font 16
Lines are set to 0
Word Wrap is still active
.. I also tried to do it manually in code but no change.
Does anyone have an explanation for that?
****edited:** when I add
myLabel.frame = CGRectMake(t.origin.x, t.origin.y, t.size.width, t.size.height *2);
to the cellForRowAtIndexPath I still see the cut off label. But if I then scroll the table view so the label is outside the viewable area shortly it will be displayed with the complete text when it is visible again.
By the way, I am working with viewTags, so I don't have a dedicated Cell Class e.g. UILabel *myLabel = (UILabel *)[cell viewWithTag:2];
You should check UILabel width; the width should be less than that of the value. Then like this:
(void) viewWillLayoutSubviews {
_landPhoneTips.preferredMaxLayoutWidth = _landPhoneTips.bounds.size.width;
}
I spent hours dealing with this identical problem before finally sorting it out last evening. After changing the font size, you must select the UILabel within the storyboard and select Edit > Size to Fit Content, even if you had already previously done so! In doing so you apparently reset some setting that gets messed up when changing the font size. Once done, the UILabel will wrap as it did previously.

Remove labels using WhirlyGlobe

I am using the (really cool) WhirlyGlobe (https://code.google.com/p/whirlyglobe/) 3D globe display for iPhone in a new application. I can add labels at certain locations using the code shown below. I want to be able to go back and remove a label I added earlier. The Documentation (http://whirlyglobedocs.s3-website-us-east-1.amazonaws.com/html/interface_label_layer.html#ac17e1ec72e70eec416cb2cac833f46fa) shows a removeLabel method but I cannot seem to get it to work. I can add but not remove Labels. I tried looping through all subviews but cannot find these SimpleLabel instances. Can someone please help me understand how to remove a label? I haven't had much luck finding many examples. Thank you!
// Current position
float lat = [[values objectAtIndex:8] floatValue];
flaot lon = [[values objectAtIndex:9] floatValue];
// Create a SingleLabel at this Lat / Lon pair location
SingleLabel *interimLabel = [[[SingleLabel alloc] init] autorelease];
interimLabel.text = [NSString stringWithFormat:#"PRN %d",[[values objectAtIndex:1] intValue]];
[interimLabel setLoc:GeoCoord::CoordFromDegrees(lon, lat)];
[locationArray addObject:interimLabel];
[allLabels addObject:interimLabel];
When you add a single label or a group of labels to the label layer, you'll get back a SimpleIdentity. Keep that around somewhere. Then, when you want to delete the label (or group of labels) from the label layer, you pass back in that SimpleIdentity.
What's going on is this. WhirlyGlobe batches drawable data like a mofo. Your SingleLabel objects no longer exist as soon as the Label Layer has crunched them down into as few Drawables and it can get away with. So to refer to those labels, you have to keep around the unique ID.
Now if you want to delete these labels separately or individually change their appearances, then you've got to add them one by one. One label to one SimpleIdentity. Otherwise there's no way to refer to them individually.
For speed, I recommend grouping as many of them together as you can get away with. If that's just too complex for now, add them one by one and then make a note to come back. So when you say "Why isn't this running as fast as I'd like" you can then say "Ooooo, right."

1 tag multiple UILabels in xcode, iphone

So I have many labels in my app that contain the same text. I am localizing them, and would like to know if its possible for me to tag them all with the same number and assign the new text that way.
currently when I have tested it only recognizes the first label with the tag.
I really want to save myself from having all these repeat outlets
Yes,
You can tag them with a integer number because in inheritance tree NSObject is the super class to UIView,
And NSObject has an integer property called tag .
USE tag like below
myLabel1.tag = 1;
myLabel2.tag = 2;
...........
...........
myLabeln.tag = n;
It is a little inclear from your question what your problem is, but I am assuming from your question that you want to be able to update all your UILabels which are located at different places within your app with the same text.
You could create a global header file that contains an NSString which holds the text for all your labels, and then #include this header in all view controllers which contain labels which need to show this this text. Then whenever you update the NSString in the global header you update any labels that are currently in view or about to be viewed aswell. This can be done from within your IBAction methods, as well as implimenting the viewWillAppear:(BOOL)animated method. Whenever a view that contains relevent labels comes into view, it will apply the value of the NSString in your global header and update its labels.

UITextView or UIWebView: How to display pages of a simple editable TXT file

All I want to have is a full-screen simple text editor. However, I don't want the scrolling component, but rather let the user flick through the pages (instead of scrolling). So I need to import or open a TXT and then format it by braking it down (e.g. by dividing its contents to 10 lines per screen/page).
My question is how I will display the txt? UITextView is scrollable (even though I can disable this in IB)... I did not find any method for UIWebView to let it format my contents on different 'pages' or screens.
Where will I need to start? Ideally I'd need some sample code. All the samples for UIWebView do not tell me anything about how to format editable text on several pages.
So all I really want is an UITextView which is not scrollable and opens up a new page/screen if I run out of space on the first page/screen.
Thanks for any help to get me started.
first thing first ...... there is no particular method to achieve this
1.You need to break your single string into multiple strings, to do that you can use
int pageNumber; // suppose this keep track of on what page you are
int count; //suppose this keep track of how long string your one screen support
NSString* completeString; //suppose this is your string
NSRange range = NSMakeRange(pageNumber * count, count);
NSString* temp = [completeString substringWithRange:range];
2.Now instead of using UITextView (if you don't want user interaction ) you should use UILable
just change the property of UILabel (this one is of your interest)
UILabel* myLabel; //suppose this is that label
myLabel.numberOfLines = 0; //this will chage your label to go multyline.
You should be able to achieve this by putting your UITextView into a UIScrollView and setting pagingEnabled on the UIScrollView to YES.

How do I adjust a UIButton relative to its current position?

I'm trying to work with a scroll view controller that needs to adjust it's contents based on user interaction -- specifically, by adding a 'done' button while the user is working with a UITextView, then removing it when they are done. The problem is making room for the button in question. What I'd like to do is...
control.layer.position.x-=50;
for every single control that is 'below' the one I'm working with. Unfortunately, that doesn't work. As far as I can tell, I'm going to have to do something more like...
control.layer.position=CGPointMake(newX, newY);
This creates a maintainability nightmare; instead of being able to rearrange buttons inside UIBuilder at will, I'm going to have to change their positions in the code as well. Unfortunately, no matter what variant of the first type of code I use, the result doesn't work; I'm informed that I need an lvalue to the left of the assign or that I'm trying to manipulate incompatible types.
It'll be more verbose than the -= solution, but why don't you just calculate newX and newY based on their old values?
e.g.
CGPoint oldPosition = control.layer.position;
control.layer.position = CGPointMake(oldPosition.x - 50, oldPosition.y);
The button comes with a center property, so:
button.center = CGPointMake(button.center.x - 50, button.center.y);
should do the trick