Using Texture as label in WhirlyGlobe - iphone

WhirlyGlobe is a very useful framework and I would like to display Country Flags instead of names for each countries.
When looking at LabelLayer.h it seems possible to overwrite the icontexture generated from Text label by a picture:
#interface SingleLabel : NSObject {
NSString *text;
WhirlyGlobe::GeoCoord loc;
NSDictionary *desc; // If set, this overrides the top level description
WhirlyGlobe::SimpleIdentity iconTexture; // If non-zero, this is the texture to use as an icon }
But I didn't find any way to use label for picture display.
Can someone please help me understand how to replace Text label by Picture label?
Following #Mousebird first answer here is what I"ve implemented:
// This describes how our labels will look
NSDictionary *labelDesc =
[NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES],#"enable",
[UIColor clearColor],#"backgroundColor",
[UIColor whiteColor],#"textColor",
[UIFont boldSystemFontOfSize:16.0],#"font",
[NSNumber numberWithInt:4],#"drawOffset",
[NSNumber numberWithFloat:0.08],#"height",
[NSNumber numberWithFloat:0.08],#"width",
nil];
// Build up an individual label
NSMutableArray *labels = [[[NSMutableArray alloc] init] autorelease];
SingleLabel *texLabel = [[[SingleLabel alloc] init] autorelease];
Texture *theTex = new Texture(#"icon", #"png");
theTex->setUsesMipmaps(true);
texLabel.text = #"";
texLabel.iconTexture = theTex->getId();
theScene->addChangeRequest(new AddTextureReq(theTex));
[texLabel setLoc:GeoCoord::CoordFromDegrees(5, -3)];
[labels addObject:texLabel];
[self.labelLayer addLabels:labels desc:labelDesc];
Still a problem, the texture is loaded in memory but didn't appear. Putting a string as a text create a Blank square on the first character of the label

Sorry for the delay. I need to set up some sort of feed for stackoverflow.
Anyway, the short answer is there isn't a great way to do it. I'm adding Markers for a client, but that won't be out until 1.2. For now, you should be able to make a label display a texture. That's the iconTexture you're showing there.
To create a texture, do the following:
Texture *theTex = new Texture(#"nameoftexture", #"png");
theTex->setUsesMipmaps(true);
theTexId = theTex->getId();
scene->addChangeRequest(new AddTextureReq(theTex));
And then to use it:
singleLabel.text = #"";
singleLabel.iconTexture = theTexId;

Related

GPUImage change brightness with UISlider

How can i change the brightness of an Image with slider using GPUImageBrightnessFilter ?
I tried,
-(void)updateBrightness {
GPUImageFilter *selectedFilter = nil;
[selectedFilter removeAllTargets];
selectedFilter = [[GPUImageFilter alloc] init];
CGFloat midpoint = [(UISlider *)sender value];
[(GPUImageBrightnessFilter *)settingsFilter setBrightness:midpoint];
UIImage *filteredImage = [selectedFilter imageByFilteringImage:_image_view.image];
fx_imageView.image = filteredImage;
}
There are several problems with the above code.
First, you're not actually using a brightness filter against your image, because you're calling -imageByFilteringImage: on selectedFilter, which is a generic GPUImageFilter that you allocated fresh. Your GPUImageBrightnessFilter of settingsFilter is never used.
Second, you don't want to be allocating a new filter with every time you update a parameter. Allocate your GPUImageBrightnessFilter once and simply update it as values change.
Third, you don't want to keep re-filtering UIImages. Going to and from UIImages is a slow process (and won't work properly when using -imageByFilteringImage: on the same filter, because of some caching I do). Instead, create a GPUImagePicture based on your original image, add a GPUImageBrightnessFilter to that as a target, and target your GPUImageBrightnessFilter at a GPUImageView. Use -processImage every time you update your brightness filter and your updates will be much, much faster. When you need to extract your final image, use -imageFromCurrentlyProcessedOutput.
these may help you...
-(void)viewDidLoad
{
[sliderChange setMinimumValue:-0.5];
[sliderChange setMaximumValue:0.5];
[sliderChange setValue:0.0];
brightnessFilter = [[GPUImageBrightnessFilter alloc] init];
}
-(IBAction)upDateSliderValue:(id)sender
{
GPUImagePicture *fx_image;
fx_image = [[GPUImagePicture alloc] initWithImage:originalImage];
[brightnessFilter setBrightness:self.sliderChange.value];
[fx_image addTarget:brightnessFilter];
[fx_image processImage];
UIImage *final_image = [brightnessFilter imageFromCurrentlyProcessedOutput];
self.selectedImageView.image = final_image;
}

Display JSON as List in ViewController (Not in TableView)

I am producing a JSON string that I need to parse out and display onto the page. My JSON string outputs information about the contents of a CD like this:
[{"song_name":"Song 1","artist":"John","price":"$1"},
{"song_name":"Song 2","artist":"Anna","price":"$2"},
{"song_name":"Song 3","artist":"Ryan","price":"$3"}]
I would like to display the contents in my viewController in a list format displaying the song_name, artist, and price. I do not want to use a tableView to display this information, but would rather just have a list displayed. How might I go about doing this? I would assume that I need to use NSJSONSerialization, but have only used that for a tableView in the past. Thank you!
In addition to other answers, you can use only one label, just create NSMutableString (for dynamicly adding tracks info) with #"\n" between tracks info, pass it to label.text and set UILabel's property numberOfLines to 0
Follow these steps:
Parse the JSON and store the key-value pair(NSDictionary of CDs) in an NSArray (say infoArray)
Add a UIScrollview as a subview on your viewController's view.
Allocate UILabels dynamically, depending on infoArray count. Looking at your data I believe you can initialize labels with static frames i.e your y can remain static.
Add the text from the infoArray on this label.
Still, I would suggest use UITableView only. It is much simpler and a better approach
You make an array of dictionaries using NSJSONSerialization indeed, then you parse this array one by one and create a view of every dictionary. You're probably best of using a method for this:
-(UIView *) listView: (NSString *)songName andArtist:(NSString *)artist andPrice:(NSString *)price andIndex:(int)viewIndex {
//create your view here and do anything you want
UIView *subView = [[UIView alloc] init] autoRelease];
subView.frame = CGRectMake(0, viewIndex * 70, self.view.frame.width, 70);
//add labels and other stuff
// return the view
return subView;
}
The you add it to the current view by setting different Y values so they appear underneath each other by using the viewIndex of the former method... So if you have an array it goes something like this:
for (int i = 0; i < [array count]; i++) {
NSDictionary *dict = [array objectAtIndex:i];
NSString *songName = [dict valueForKey:#"song_name"];
NSString *artist = [dict valueForKey:#"artist"];
NSString *price = [dict valueForKey:#"price"];
UIView *tempView = [self listView:songName andArtist:artist andPrice:price andIndex:i];
[self.view addSubView:tempView];
}
You have to add it all to a scrollview otherwise you will run into the problem of to many rows on the page. Google for UIScrollView if you don't know how.
But I would recommend against this approach.. Tableviews are there with a reason. They are made for this stuff. Because the also provide for scrolling, drawing and refreshing. If you can, use them!

UILabel in different subviews

I'm creating my first app. It will be a catalog of products, that you can scroll through.
I created a UIScrollView with a width of 960 (320*3) and added a UIPageControl. Inside it I added 3 different view, each of them represents one of my products - with all the information I need - name, image, description, price, etc..
I can see the views move, and so I've set the first product with UILabel classes and UIImageView. I was wondering if it is possible to use NSArray and set the UILabel's text property and imageNamed in the next view as the user switches to it.
My problem is that each view has a different UILabel element.
Thanks for your help, it is much appreciated!
Yes you could use a NSArray to store the data for each of your views. I would suggest first creating a simple object with the properties labelText and imageName;
Then you can create a NSArray of your custom object like this:
MyObject obj1 = [MyObject new];
obj1.labelText = #"My Text 1";
obj1.imageName = #"My Image 1";
//Other objects..
then
NSArray *myArray = [[NSArray alloc] initWithObjects: obj1, obj2, obj3, nil];
Then When you switch pages simply do this:
MyObject *myPageInfo = [myArray objectAtIndex:pageNumber];
myLabel.text = myPageInfo.labelText;
mylabel.imageName = myPageInfo.imageName;
Hope that helps.

Get truncated text from UILabel

Is there anyway I can get the truncated version of the text for a UILabel?
In short, I have a paragraph of text, and two UILabels - label A, which is 2 lines long, and label B, which is a variable height. Label A is above label B. The idea is that label A shows the first two lines of the paragraph of text, and upon a certain user action, label B because visible and displays the rest of the text.
I'm having trouble determining what should go in label B, as I don't know what's being shown in label A. I'd need to also remove the "..." from label A.
Note: I realize this is a bit convoluted but there are some good reasons for it, which I won't clutter up the question with.
I wonder if you could use the methods in the NSString UIKit Additions to figure out how much fits into label A.
A crude way might be to start with the first character of your text and test for the size it would take up (-sizeWithFont:forWidth:lineBreakMode: maybe?) and then keep adding characters one at a time until it doesn't fit into your label A any more.
I hope somebody else can come up with a better way to do this, but the above should work.
Update
Last night I looked a bit into Core Text for my own app and came across CTFramesetterSuggestFrameSizeWithConstraints. You could maybe use this to figure out how much of your string fits into the label, by looking at the fitRange in that function.
Update 2:
I think this should work, but I have just typed this in here, so it may not even compile:
UIFont *uiFont = [UIFont systemFontOfZise:13.0f]; // whichever font you're using
CTFontRef ctFont = CTFontCreateWithName((CFStringRef)uiFont.fontName, uiFont.pointSize, NULL);
NSDictionary *attr = [NSDictionary dictionaryWithObject:(id)ctFont forKey:(id)kCTFontAttributeName];
CFRelease(ctfont);
NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:yourLabelText attributes:attr];
CTFrameSetterRef frameSetter = CTFrameSetterCreateWithAttributedString((CFAttributedStringRef)attrString);
[attrString release];
CFRange fitRange;
CTFrameSetterSuggestFrameSizeWithConstrains(
frameSetter,
CFRangeMake(0, 0),
NULL,
CGSizeMake(labelWidth, labelHeight),
&fitRange);
CFRelease(frameSetter);
CFIndex numberOfCharactersThatFit = fitRange.length;
thanks to Thomas Müller
be sure to set line break mode the myLabel to this:
myLabel.lineBreakMode = NSLineBreakByWordWrapping;
by this method you can get chunked strings that actually fit in the constrained size.
Here is the baked code:
- (NSArray *)truncate:(NSString *)text
{
NSMutableArray *textChunks = [[NSMutableArray alloc] init];
NSString *chunk = [[NSString alloc] init];
CTFramesetterRef frameSetter;
UIFont *uiFont = [UIFont systemFontOfSize:17.0f];
CTFontRef ctFont = CTFontCreateWithName((__bridge CFStringRef)uiFont.fontName, uiFont.pointSize, NULL);
NSDictionary *attr = [NSDictionary dictionaryWithObject:(__bridge id)ctFont forKey:(id)kCTFontAttributeName];
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:text attributes:attr];
CFRange fitRange;
while (attrString.length>0) {
frameSetter = CTFramesetterCreateWithAttributedString ((__bridge CFAttributedStringRef) attrString);
CTFramesetterSuggestFrameSizeWithConstraints(frameSetter, CFRangeMake(0,0), NULL, CGSizeMake(myLabel.frame.size.width, myLabel.frame.size.height), &fitRange);
CFRelease(frameSetter);
chunk = [[attrString attributedSubstringFromRange:NSMakeRange(0, fitRange.length)] string];
[textChunks addObject:chunk];
[attrString setAttributedString: [attrString attributedSubstringFromRange:NSMakeRange(fitRange.length, attrString.string.length-fitRange.length)]];
}
return textChunks;
}
For Label A, calculate approximate character that should fit perfectly for two lines, for the particular font you are using.
For label B, set variable Height that the whole text must fit into it.

iphone - changing the background color of several views in a loop

I have to change the background color of several views that have the same base name like color1, color2, color3, etc., to the same color
I could simply do something like
color1.backgroundColor = theColor;
color2.backgroundColor = theColor;
color3.backgroundColor = theColor;
color4.backgroundColor = theColor;
...
but I would rather do that in a more elegant way using a loop, something like
NSString *baseName = #"color";
for (int i=1; i<numberOfViews; i++) {
NSString *tempName = [NSString stringWithFormat:#"%#%d", baseName, i];
// now that I have the correct name of the view as a string on tempName
// how do I reference the view which name is on tempName, so I can change its color?
}
thanks.
The approach you are trying to take would work in a language like JavaScript that allows you to take a string of text and send it to the interpreter at runtime. As a compiled language, Objective-C doesn't allow this.
Instead, you need to put your views in an array first, then you can iterate through them. The easiest way would be:
NSArray *views = [NSArray arrayWithObjects:color1, color2, color3, color4, nil];
for (UIView *v in views) {
v.backgroundColor = theColor;
}