Sizing ButtonCells in NSMatrix - objective-c

I am trying to implement a Radiogroup with cocoa and used the example provided by apple Using Radio Buttons
The following screenshot shows the problem I have. Even if the NSMatrix containing the cells have a NSRect large enough the cells themselves are not wide enough to display the titles.
How can I correct this?
NSButtonCell *prototype = [[[NSButtonCell alloc] init]autorelease];
[prototype setButtonType:NSRadioButton];
[prototype setBordered:YES];//only for testing
_view = [[[NSMatrix alloc] initWithFrame:rect
mode:NSRadioModeMatrix
prototype:(NSCell *)prototype
numberOfRows:3
numberOfColumns:1]autorelease];
NSArray *cellArray = [_view cells];
for (std::size_t index = 0; index < 3; ++index)
{
[[cellArray objectAtIndex:index] setTitle:#"a title"];
}

Use setCellSize of matrix.
NSSize size = [_view cellSize];
size.width = 400;
[_view setCellSize:size];

Related

Constraint spacing UILabel in For Loop

I have a small problem and hope someone can help me solve it ... I searched the net and made some attempts, but I could not solve my problem.
I created a FOR loop that plays a specified number of labels obtained through an array, as you can see from the code that I posted below ...
Now my problem is how to insert constraint programmatically. I need that between a label and the other (horizontal) there is a specific space.
Example:
if I label 4 I would like these would take around the horizontal space is in the iPhone display SE both iPhone 7 Plus ..
So my question is how do I set a specific space between each label and pass in the FOR loop?
CGFloat padding = 0;
NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:#"INFO UNIVERSITARIE",#"INFO GENERALI", #"TROVA SEDI", nil];
for (NSInteger i = 0; i < array.count; i ++) {
NSString *valueText = [array objectAtIndex:i];
UILabel *label = [[UILabel alloc] init];
label.frame = CGRectMake(padding *i, 0, 50, 30);
label.text = [NSString stringWithFormat:#"%#", valueText];
label.textColor = [UIColor whiteColor];
label.font = [UIFont fontWithName:#"HelveticaNeue" size:12];
[label sizeToFit];
[_baseView addSubview:label];
padding = label.frame.size.width;
label.translatesAutoresizingMaskIntoConstraints = NO;
}
Researching this, I remembered that the UIStackView is our new friend, designed specifically for this problem.
NSArray *array = #[ #"INFO UNIVERSITARIE",#"INFO GENERALI", #"TROVA SEDI" ];
NSMutableArray *labels = [NSMutableArray array];
for (NSString *string in array) {
UILabel *label = [[UILabel alloc] init];
label.text = string;
[labels addObject:label];
}
UIStackView *stackView = [[UIStackView alloc] initWithArrangedSubviews:labels];
stackView.axis = UILayoutConstraintAxisHorizontal;
stackView.spacing = 12.0;
stackView.frame = self.view.bounds;
[self.view addSubview:stackView];
Just learned about this myself, but I tried this code and it works nicely.

Adding text to a UIScrollView

I'm creating a tape measure in a UIScrollView, it has horizontal lines from an image and I want to add the numbers programatically.
(Note the blue was to help getting the layout right)
I tried this:
for (NSInteger i = 0; i < self.maxUnits.integerValue; i++) {
static CGFloat labelWidth = 40;
CGFloat x = (halfScreen - (labelWidth/2)) + (i * self.widthOfOneFraction.floatValue * self.numberOfFractions.floatValue);
CGRect frame = CGRectMake(x-1, 41, labelWidth, 70);
UILabel *label = [[UILabel alloc] initWithFrame:frame];
label.text = [NSString stringWithFormat:#"%d", i];
label.textAlignment = NSTextAlignmentCenter;
label.textColor = [UIColor tlbBlueColor];
label.font = [UIFont fontWithName:#"HelveticaNeue-Light" size:20.0];
[self addSubview:label];
}
Which works, but uses about 40Mb of RAM. I thought that adding the text in a CATextLayer might be more efficient, but I can't get it working:
for (NSInteger i = 0; i < self.maxUnits.integerValue; i++) {
CATextLayer *label = [[CATextLayer alloc] init];
[label setFont:#"HelveticaNeue-Light"];
[label setFontSize:20];
[label setFrame:frame];
[label setString:[NSString stringWithFormat:#"%d", i]];
[label setAlignmentMode:kCAAlignmentCenter];
[label setForegroundColor:[[UIColor whiteColor] CGColor]];
[self.layer addSublayer:label];
}
The numbers don't appear on the screen.
So my question is can I either make the first way more efficient, and if not is the second way more efficient when it works and if so how do I get it working?
I changed the way the loading worked so as to only load items on display and to load a few more as you scroll.
This seems to have totally fixed the high memory issue and even if you scroll to both ends it doesn't use the same high amount of memory. I guess there was a leak in how my original code worked.

Finding Color of a UIView Object and change the Color

Hello Guys actually i have a lot of UIView custom class objects as a subview of a UIViewController class view which is inside a UIScrollView. And i want to check colors of UIView custom class objects. code i use is given below:-
- (void) RectColorCheck:(id)sender
{
NSArray *subViews = [[NSArray alloc] init];
subViews = [self.scrollView subviews];
NSLog(#"array----%#",subViews);
for (viewCG in subViews)
{
if ([viewCG.backgroundColor isEqual: [UIColor darkGrayColor]])
{
[viewCG setBackgroundColor:[UIColor orangeColor]];
}
}
}
but it not works the subview array of viewCG is null.
And the below code which adds the custom class(UIView) objects in viewCG subviews:-
for (int i=0; i<[mapDataArr count]; i++)
{
X = [[[mapDataArr objectAtIndex:i] valueForKey:#"X"] intValue];
Y = [[[mapDataArr objectAtIndex:i] valueForKey:#"Y"] intValue];
W = [[[mapDataArr objectAtIndex:i] valueForKey:#"W"] intValue];
H = [[[mapDataArr objectAtIndex:i] valueForKey:#"H"] intValue];
circleVwObj = [[CircleView alloc] init];
circleVwObj.frame = CGRectMake(X,Y, W, H);
circleVwObj.tag = i;
circleVwObj.lbl.frame = CGRectMake(2,2, circleVwObj.frame.size.width, circleVwObj.frame.size.height/2);
circleVwObj.lbl.text = [[mapDataArr objectAtIndex:i] valueForKey:#"standId"];
NSLog(#"lbl text---%#", circleVwObj.lbl.text);
circleVwObj.lbl.font = [UIFont boldSystemFontOfSize:11];
circleVwObj.lbl.backgroundColor = [UIColor clearColor];
circleVwObj.lbl.textColor = [UIColor whiteColor];
circleVwObj.lbl.textAlignment = UITextAlignmentCenter;
circleVwObj.lbl.minimumFontSize = 11;
circleVwObj.lbl.adjustsFontSizeToFitWidth = YES;
circleVwObj.lbl.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
[self.viewCG addSubview:circleVwObj];
}
If the subviews array is nil, it is very likely that self.viewCG is nil as well and hasn't been initialized.
Also, you should use isEqual: to compare colors. The == operator just compares pointer identity (which might actually give you the expected result in this particular case, but it's likely to break).

Activity Indicator while loading images inside UIScrollView

I have UIScrollView that contains images from the server.
I should put Activity Indicator while the image is currently loading.
UIScrollView contains dynamic number of images from the server.
May I know how can I add activity indicators on each page while the image is loading and remove once image is loaded.
Here's my code to retrieve images:
NSDictionary *items = [NSDictionary dictionaryWithObject:dictInfo forKey:#"images"];
imageList = [items objectForKey:#"images"];
NSArray *img = [imageList objectForKey:#"list"];
NSInteger imgCount = [img count];
buttonArray = [[NSMutableArray alloc] init];
for (int i=0; i<imgCount; i++) {
NSDictionary *imgDict = [img objectAtIndex:i];
// REQUEST FOR IMAGES
NSString *imgPath = [imgDict objectForKey:#"image_slot"];
NSString *imgURL = imgPath;
__block ASIHTTPRequest *requestImage = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:imgURL]];
[requestImage setCompletionBlock:^{
imgView = [UIImage imageWithData:[requestImage responseData]];
if (imgURL.length) {
[pendingRequests removeObjectForKey:imgURL];
}
scrollView.userInteractionEnabled = YES;
scrollView.exclusiveTouch = YES;
scrollView.canCancelContentTouches = YES;
scrollView.delaysContentTouches = YES;
scrollView.bounces = NO;
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
SWTUIButton *imgBtn = [[SWTUIButton alloc] initWithFrame:frame];
imgBtn.url = [requestImage.userInfo objectForKey:#"rURL"];
[imgBtn setImage:imgView forState:UIControlStateNormal];
imgBtn.backgroundColor = [UIColor clearColor];
[imgBtn addTarget:self action:#selector(buttonpushed:) forControlEvents:UIControlEventTouchUpInside];
[scrollView addSubview:imgBtn];
[buttonArray addObject:imgBtn];
[imgBtn release];
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * img.count, self.scrollView.frame.size.height);
}];
I highly suggest you use NINetworkImageView from https://github.com/jverkoey/nimbus project.
It's very light and useful.
It has a delegate method to let you know when an image is loaded.
What you basically need to do is:
1. create an NINetworkImageView for each page, just like you do with UIImageView, and call set
NINetworkImageView* networkImageView = [[[NINetworkImageView alloc] initWithImage:initialImage]
autorelease];
networkImageView.delegate = self;
networkImageView.contentMode = UIViewContentModeCenter;
[networkImageView setPathToNetworkImage:
#"http://farm3.static.flickr.com/2484/3929945380_deef6f4962_z.jpg"
forDisplaySize: CGSizeMake(kImageDimensions, kImageDimensions)];
https://github.com/jverkoey/nimbus/tree/master/examples/photos/NetworkPhotoAlbums
the add the indicator to the networkImageView as a subview.
implement the delegate as follows:
-(void)networkImageView:(NINetworkImageView *)imageView didLoadImage:(UIImage *)image {
[imageView removeAllSubviews];
}
the end result would be a much smaller code for doing the same thing.

Scroll to xib (pagecontrol)

i had a small question, i'm using this code to scroll from color to color:
NSArray *colors = [NSArray arrayWithObjects:[UIColor redColor], [UIColor greenColor], [UIColor blueColor], nil];
you can scroll from color to color, but is there a way to scroll to another xib file?
- (void)viewDidLoad {
[super viewDidLoad];
NSArray *colors = [NSArray arrayWithObjects:[UIColor redColor], [UIColor greenColor], [UIColor blueColor], nil];
for (int i = 0; i < colors.count; i++) {
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
UIView *subview = [[UIView alloc] initWithFrame:frame];
subview.backgroundColor = [colors objectAtIndex:i];
[self.scrollView addSubview:subview];
[subview release];
}
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * colors.count, self.scrollView.frame.size.height);
}
This tutorial will help you out: http://www.iosdevnotes.com/2011/03/uiscrollview-paging/
(i think that you've use this tutorial)
but on the last line of the tutorial:
Update: In the comments, some people have asked about placing buttons
inside the scroll view, and also about setting up the scroll view
using Interface Builder. I’ve added some code that includes buttons
here, and a version using Interface Builder here.
So here is the link to move to xib files: https://github.com/cwalcott/UIScrollView-Paging/tree/buttons-ib
Good luck,
Nathan
Instead of preparing UIView's and adding them to a scrollview, you could indeed use [[NSBundle mainBundle] loadNibNamed... to load several nib (which are compiled xib) and add them to a scrollview, and then scroll them up and down, or back and forth.