How to stop the horizontal scrolling programmatically? - cocoa-touch

I have a UITextView *textView in cocos2d's CCLayer. The text is scrolling in both the horizontal and vertical directions. But, I need it to scroll and bounce only in vertically.
How to stop the horizontal scrolling programmatically ?
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(100,200, windowSize.height/2,windowSize.width/2)];
textView.backgroundColor = [UIColor clearColor];
textView.text = #"I am First enemy I am First enemy I am First enemy I am First enemy I am First enemy I am First enemy I am First enemy I am First enemy";
[textView setEditable:NO];
textView.font = [UIFont fontWithName:#"Helvetica" size:24.0f];
CGPoint location = CGPointMake(200, 160);
textView.showsHorizontalScrollIndicator = NO;
//textView.bounces = NO;
//textView.alwaysBounceVertical = YES;
textView.center = location;
textView.transform = CGAffineTransformMakeRotation(CC_DEGREES_TO_RADIANS( 90.0f ));
What should I do stop scrolling horizontally ?
Thank You.

I don't know whether it was the good solution. But it worked for me. I am posting here so that any one may correct it.
textView = [[UITextView alloc] initWithFrame:CGRectMake(100,200,200 ,200)];
textView.backgroundColor = [UIColor clearColor];
NSLog(#"description: %#", enemyDescription);
textView.text = enemyDescription;
[textView setEditable:NO];
textView.font = [UIFont fontWithName:#"Helvetica" size:14.0f];
CGPoint location = CGPointMake(200, 160);
textView.showsHorizontalScrollIndicator = NO;
textView.alwaysBounceVertical = YES;
textView.transform = CGAffineTransformMakeRotation(CC_DEGREES_TO_RADIANS( 90.0f ));
[[[CCDirector sharedDirector]openGLView]addSubview:textView];
It worked for me. It scrolls only up and down and also bounces up and down.
Thank You.

Related

Search bar rounded corner

How to change the border of search bar to rounded corner. Below is my code and I need the orange coloured border to be rounded not as sharp rectangle. Please help
// Search bar
self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 60, 270, 41)];
self.searchBar.delegate = self;
self.searchBar.placeholder = MEGLocalizedString(#"search_hint_home_screen", nil);
UIColor *searchBarBackgroundColor = [UIColor clearColor];
self.searchBar.backgroundImage = [UIImage imageFromColor:searchBarBackgroundColor];
[self.searchBar setImage:[[UIImage imageNamed:#"search_icon_general.png"] imageTintedWithColor:primaryEventColor] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
UIView *textField = [self.searchBar subviewWithKindOfClass:[UITextField class]];
textField.backgroundColor = [UIColor orangeColor];
// textField.layer.borderWidth = 1;
textField.layer.cornerRadius = 14;
[(UITextField *)textField setBorderStyle:UITextBorderStyleNone];
// Change the search bar placeholder text color
[textField setValue:[UIColor whiteColor] forKeyPath:#"_placeholderLabel.textColor"];
}
self.searchBar.layer.cornerRadius = 14;
self.searchBar.layer.borderColor = [UIColor orangeColor].CGColor;
This solve your problem
textField.layer.masksToBounds = true

Create a temporary HUD popup notification in Cocoa App

Im trying to have a window pop up to notify the user of an action, kind of like how the Flutter app shows a play/pause notification when a gesture is received.
This comes up for about two seconds and vanishes. The app itself is run from the menu bar so this pop up doesn't take control from whatever application is currently in focus but rather just shows on top of it and cannot be interacted with. I basically want to recreate exactly this. Say in the body of
- (void)popup:(id)sender {}
This is essentially very similar to a toast on android.
Edit: I looked closer at the flutter app and all it does is display two images (.png) one for the huge pause and another to indicate what application its in (in this case iTunes). Question now is how do I display an image like this.
You should take a look at this:
https://github.com/Foxnolds/MBProgressHUD-OSX
or Growl:
http://growl.info/documentation/developer/
To replicate this I added a 'HUD Window" from the interface builder which is just an NSPanel. I then added a image view to the panel and set the panel's colour to clear. I was then able to position the panel where I wanted and overlay the appropriate image.
I'm using this code:
static void showHud(NSString* text, int width, int height, CGFloat duration, int fontSize) {
NSWindow* window = [NSWindow new];
[window setFrame:NSMakeRect(0,0,width,height) display:NO];
[window center];
window.titleVisibility = NSWindowTitleHidden;
window.styleMask = NSWindowStyleMaskBorderless;
window.alphaValue = 0.9;
window.movableByWindowBackground = YES;
[window setLevel: NSStatusWindowLevel];
[window setBackgroundColor: [NSColor clearColor]];
[window setOpaque:NO];
//[window setHasShadow:NO];
[window makeKeyAndOrderFront:NSApp];
NSVisualEffectView *view = [[NSVisualEffectView new] initWithFrame:window.contentView.bounds];;
view.translatesAutoresizingMaskIntoConstraints = NO;
[view setBlendingMode:NSVisualEffectBlendingModeBehindWindow];
[view setMaterial:NSVisualEffectMaterialDark];
[view setState:NSVisualEffectStateActive];
//[view setAppearance:[NSAppearance appearanceNamed:NSAppearanceNameVibrantDark]];
//[view setWantsLayer:NO];
[view setWantsLayer:YES];
view.layer.cornerRadius = 16.;
//view.layer.shouldRasterize = YES;
//view.layer.rasterizationScale = 0.45;
view.layer.shadowOpacity = 0.1; //0.01;
view.layer.edgeAntialiasingMask = kCALayerTopEdge | kCALayerBottomEdge | kCALayerRightEdge | kCALayerLeftEdge;
[window.contentView addSubview:view];
//window.contentView = view;
NSTextField *label = [[NSTextField alloc] initWithFrame:view.bounds];
[label setBezeled:NO];
[label setDrawsBackground:NO];
[label setEditable:NO];
[label setSelectable:NO];
label.stringValue = text;
label.alignment = NSTextAlignmentCenter;
label.textColor = [NSColor whiteColor];
label.backgroundColor = [NSColor clearColor];
label.font = [NSFont fontWithName:#"Arial-BoldMT" size:fontSize];
//NSRect titleRect = [label titleRectForBounds:view.bounds];
NSSize cellSize = [label.cell cellSizeForBounds:view.bounds];
NSSize strSize = cellSize; // [label.attributedStringValue size];
NSRect frame = view.frame;
frame.origin.y = frame.size.height / 2 - strSize.height / 2;
frame.size.height = strSize.height;
label.frame = frame;
[view addSubview:label];
[NSAnimationContext runAnimationGroup:^(NSAnimationContext *context) {
context.duration = duration;
window.animator.alphaValue = 0.;
} completionHandler:^{
[window close];
}];
}

iOS8 UIButton title disappear

I have a button in my app that is perfectly visible on iOS7. But when I switch to iOS8 the title label for this button does not appear. And I can see only border around the button.
Here is the code that makes the button. Could someone give me a clue where the problem is?
UIButton *problem = [[UIButton alloc] initWithFrame:frame];
[problem addTarget:self action:#selector(problemButton:) forControlEvents:UIControlEventTouchUpInside];
problem.backgroundColor = [UIColor whiteColor];
[problem setTitle:NSLocalizedString(#"ReportAProblem", nil) forState:UIControlStateNormal];
problem.titleLabel.textColor = CUSTOM_DARK_GRAY_COLOR;
[[problem layer] setBorderWidth:1.0f];
[[problem layer] setBorderColor:CUSTOM_DARK_GRAY_COLOR.CGColor];
problem.layer.cornerRadius = 5;
problem.clipsToBounds = YES;
I found an answer for this question but immediately there is another:
I encounter the very same problem with some of my UILabels, for example:
self.label = [[UILabel alloc] initWithFrame:labelFrame];
self.label.numberOfLines = 1;
self.label.adjustsFontSizeToFitWidth = YES;
self.label.textAlignment = NSTextAlignmentCenter;
self.label.textColor = self.textColor;
CGFloat fontSize = self.bounds.size.height / 5;
self.label.font = [UIFont systemFontOfSize:fontSize];
You should use - (void)setTitleColor:(UIColor *)color forState:(UIControlState)state instead of titleLabel.textColor.
Second problem: Why are you trying to set a ratio of your view height as a font size ? Maybe doing it with your label makes more sense. CGFloat fontSize = self.label.bounds.size.height / 5; Anyway what you usually want to do is have a fixed font size and adapt the label frame around it.

UIScrollView with UIImageView partially visible on screen

I have a UIScrollView with a UIImageView in order to enable zoom features.
The problem now is that the scrollView should be shown fullscreen (except the navigationBar), but it's shown with a weird "offset".
Due that images explains more than 1000 words, here it is:
The red rectangles are the weird offset that's being shown, while the image should take the whole view, but it's not. As you see,it's being cut instead.
I tried changing the frame, bounds etc but same result.
Here's my code:
- (void)viewDidLoad
{ [super viewDidLoad];
self.view.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];
imageScrollView.bouncesZoom = YES;
imageScrollView.delegate = self;
imageScrollView.clipsToBounds = NO; // If set to yes, the image would be like the screen above
imageView = [[UIImageView alloc] init];
imageView.clipsToBounds = YES;
imageScrollView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin);
//activity indicator
UIActivityIndicatorView *activityIndicator = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge] autorelease];
activityIndicator.hidesWhenStopped = YES;
activityIndicator.hidden = NO;
activityIndicator.center = CGPointMake(self.imageView.frame.size.width /2, self.imageView.frame.size.height/2);
[activityIndicator startAnimating];
[imageView setImageWithURL:[NSURL URLWithString:tipImageString]
placeholderImage:nil options:SDWebImageProgressiveDownload
success:^(UIImage *image) { [activityIndicator stopAnimating];[activityIndicator removeFromSuperview]; }
failure:^(NSError *error) { [activityIndicator stopAnimating];[activityIndicator removeFromSuperview]; }];
[imageView addSubview:activityIndicator];
imageView.userInteractionEnabled = YES;
imageView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin);
imageView.center = [[imageScrollView window] center];
imageView.contentMode = UIViewContentModeScaleAspectFit;
[imageScrollView setContentMode:UIViewContentModeScaleAspectFit];
[imageScrollView addSubview:imageView];
imageScrollView.contentSize = self.imageView.image.size;
imageScrollView.decelerationRate = UIScrollViewDecelerationRateFast;
CGSize boundsSize = self.imageScrollView.bounds.size;
imageView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
CGRect frameToCenter = imageView.frame;
// center horizontally
if (frameToCenter.size.width < boundsSize.width)
frameToCenter.origin.x = (boundsSize.width - frameToCenter.size.width) / 2;
else
frameToCenter.origin.x = 0;
// center vertically
if (frameToCenter.size.height < boundsSize.height)
frameToCenter.origin.y = (boundsSize.height - frameToCenter.size.height) / 2;
else
frameToCenter.origin.y = 0;
imageView.frame = frameToCenter;
// calculate minimum scale to perfectly fit image width, and begin at that scale
float minimumScale = 0.50;//[imageScrollView frame].size.width / [imageView frame].size.width;
imageScrollView.maximumZoomScale = 5.0;
imageScrollView.minimumZoomScale = minimumScale;
imageScrollView.zoomScale = minimumScale;
[imageScrollView setContentMode:UIViewContentModeScaleAspectFit];
[imageView sizeToFit];
[imageScrollView setContentSize:CGSizeMake(imageView.frame.size.width, imageView.frame.size.height)];
}
I tried everything, but with no success!
I think that's something with frames but i can't figure out what.
Any help appreciated
#Pheel Glad that you find it due to CGAffineTransformMakeRotation of the image. This may change the coordinates.
Try to change the Image Resolution. Also, did you try visually placing the scroll view and then placing the image view in Interface Builder. After doing that, you can initialize it in a method such as ViewDidLoad
Why don't you just do:
... set-up imageView...
imageView.frame = imageScrollView.bounds;
imageView.contentMode = UIViewContentModeScaleAspectFit;
[imageScrollView addSubview:imageView];
You have a lot of code that is redundant or have cumulative effect in your method (multiple consecutive changes of frame/center, contentMode, contentSize...), which makes it hard to know what is causing what (probably the scroll view is clipping the image view, whose center is first based on the window coordinate instead of its superview coordinates, but finally not, because you redefine its frame...). You should probably start from scratch with some simpler code, depending on what you want exactly, and the configuration you start with (what is the scroll view frame for instance?).

How Can I Set the Height of a Single UITabelViewCell to a Constant Value?

I am searching on Google about this but I always find very complex solutions about it. I have a cell with an non editable scrollable UITextView and I just want to increase the height of that cell to a constant value. How can I do it? Actually I am using the next code to add the UITextView to the cell.
cell.contentView.bounds = CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height);
UITextView *textView = [[UITextView alloc] initWithFrame:cell.contentView.bounds];
textView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
textView.editable = NO;
textView.scrollEnabled = YES;
textView.backgroundColor = [UIColor clearColor];
textView.text = self.description;
[cell.contentView addSubview:textView];
[textView release];
Thanks for reading.
Implement this method in your delegate: http://developer.apple.com/library/ios/documentation/uikit/reference/UITableViewDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intfm/UITableViewDelegate/tableView:heightForRowAtIndexPath: