How do I put a background image on a text area (IN THE IPAD) - xcode

How do I put a template'd background image on a text area?

Not sure what you mean with "template'd"... but....
In interface builder, add the imageview behind the textview, uncheck the "opaque" box for the textview.

You can set the contents property of the text views layer.
#import <QuartzCore/QuartzCore.h>
...
- (void) viewDidLoad {
[super viewDidLoad];
textView.layer.contents = (id)[UIImage imageNamed:#"myImage.png"].CGImage
}
should work

If you want to add the image programmaticaly I managed to do it this way:
First in my textviewViewController.h I added an outlet for the UITextView.
#import <UIKit/UIKit.h>
#interface textviewViewController : UIViewController {
UITextView *textView;
}
#property (nonatomic, retain) IBOutlet UITextView *textView;
#end
Next, in my .xib I added my UITextView and connected my outlet.
Lastly in the viewDidLoad of textviewViewController.m I add the image as a subview to my textview.
- (void)viewDidLoad {
[super viewDidLoad];
// Change the pathForResource to reflect the name and type of your image file
NSString *path = [[NSBundle mainBundle] pathForResource:#"d" ofType:#"jpg"];
UIImage *img = [UIImage imageWithContentsOfFile:path];
UIImageView *imgView = [[UIImageView alloc] initWithImage:img];
[self.textView insertSubview:imgView atIndex:0];
[imgView release];
}
I tried the insertSubview:atIndex: with both 0 and 1 with the same result, but I would stick with 0 idicating that it's behind the textview.

Related

How to change UIImageView with a button in a another view

I've search the web a day for it but I can't find it:
How Can I change a UIImageView with a button in a another view on Xcode.
I'm new to Xcode.
Add this to your header file in the ImageView ViewController, and connect it:
#property (nonatomic, retain) IBOutlet UIImageView *imageView;
Add to the other ViewController's .m file (don't forget to import your first file):
- (IBAction)btnPressed {
TheOtherViewController *vc = [[TheOtherViewController alloc] init];
vc.imageView.image = [UIImage imageNamed:#"MyImage"];
}

Load UIView defined in XIB into UIView

following a post presented here, I tried to load a view defined in a XIB file into a view, currently being displayed. Basically, what I want to do is to replace the middle view (see screenshot below) with a different view when the user clicks on the button.
This is the simple view I want to load into the middle view:
And this is the source code of the ViewController:
ViewController.h
#import
#interface ViewController : UIViewController
#property (strong, nonatomic) IBOutlet UIButton *loadxibButton;
#property (strong, nonatomic) IBOutlet UIView *middleView;
- (IBAction)buttonClicked:(id)sender;
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (IBAction)buttonClicked:(id)sender {
NSLog(#"click...");
NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:#"NewMiddleContent" owner:self options:nil];
UIView *nibView = [nibObjects objectAtIndex:0];
self.middleView= nibView;
}
#end
When I click on the button, nothing happens, i.e. the new view will not be displayed. Can anybody please help?
Thanks a lot
Christian
Phillip, thanks a lot!
Instead of
- (IBAction)buttonClicked:(id)sender {
NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:#"NewMiddleContent" owner:self options:nil];
UIView *nibView = [nibObjects objectAtIndex:0];
self.middleView= nibView;
}
I now use this code:
- (IBAction)buttonClicked:(id)sender {
NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:#"NewMiddleContent" owner:self options:nil];
UIView *nibView = [nibObjects objectAtIndex:0];
nibView.frame = CGRectMake(261, 0, 532, 748);
[[[self view] viewWithTag:2] removeFromSuperview];
[[self view] addSubview:nibView];
}
Is this want you meant, i.e. is this like "best practice"?
Bye
Christian
Your view controller has a view property which, presumably, has three subviews (left, middle, and right). Instead of just setting a middleView property in your controller, your strategy should be to update the primary view's subviews array (removing the one that's to be replaced and inserting the new one).

Core Image help: Setting CIImage from UIImage via UIImagePicker (then filtering it)

Searched around on SO for an answer. Found some interesting stuff but I am still stuck.
In a view controller I have a UIImageView which I set an image taken from UIImagePicker when the view loads.
(I also set a CIImage at the same time)
I have two sliders (one for brightness, one for contrast). Upon moving the slider, a filter is applied to the CIImage, then the CIImage is rendered into the the UIImageView's UIImage.
Fist off, the UIImage taken from UIImagePicker does show up correctly when first selected.
I also have my slider ranges set correctly and have verified the proper float values are being passed to the delegate functions (via NSLog).
However, when I try playing with the sliders, my UIImageView turns white! Perhaps you folks can help. I'll post some code snippets:
First, my #interface:
#interface PrepViewController : UIViewController <UIImagePickerControllerDelegate , UINavigationControllerDelegate>
#property (weak, nonatomic) IBOutlet UIImageView *editingImage;
#property (weak, nonatomic) CIImage *editingCIImage;
#property (weak, nonatomic) CIContext *editingCIContext;
#property (weak, nonatomic) CIFilter *editingCIFilter;
#property (nonatomic) BOOL didAskForImage;
#property (weak, nonatomic) IBOutlet UISlider *brightnessSlider;
#property (weak, nonatomic) IBOutlet UISlider *contrastSlider;
- (void) doImageFilter:(NSString *)filter withValue:(float)value;
#end
My viewDidLoad, pretty simple just sets didAskForImage to NO
- (void)viewDidLoad
{
[super viewDidLoad];
self.didAskForImage = NO;
}
viewDidAppear is where I check if I've already asked for the image, then ask for it
- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
if(!self.didAskForImage){
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = NO;
UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
sourceType = UIImagePickerControllerSourceTypeCamera;
}
picker.sourceType = sourceType;
[self presentModalViewController:picker animated:YES];
}
}
Here is the imagePickerController didFinishPickingImage delegate method
this is where I actually set the UIImageView's image property and initialize the CIImage, CIContext, and CIFilter objects.
I also set the didAskForImage boolean and the slider targets.
-(void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingImage : (UIImage *)image
editingInfo:(NSDictionary *)editingInfo
{
self.didAskForImage = YES;
self.editingImage.image = image;
self.editingCIImage = [[CIImage alloc] initWithImage:image];
self.editingCIContext = [CIContext contextWithOptions:nil];
self.editingCIContext = [CIContext contextWithOptions:nil];
self.editingCIFilter = [CIFilter filterWithName:#"CIColorControls" keysAndValues:kCIInputImageKey, self.editingCIImage, nil];
[self.contrastSlider addTarget:self action:#selector(contrastMove:) forControlEvents:UIControlEventValueChanged];
[self.brightnessSlider addTarget:self action:#selector(brightnessMove:) forControlEvents:UIControlEventValueChanged];
[picker dismissModalViewControllerAnimated:YES];
}
and the cancel delegate
-(void)imagePickerControllerDidCancel:(UIImagePickerController *) picker
{
[picker dismissModalViewControllerAnimated:YES];
picker = nil;
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:0] animated:YES];
}
Here are the sliders' delegate methods. Again, the NSLogs display the expected values.
- (void) contrastMove:(id)sender{
NSLog(#"%f", [(UISlider *)sender value]);
[self doImageFilter:#"inputContrast" withValue:[(UISlider *)sender value]];
}
- (void) brightnessMove:(id)sender{
NSLog(#"%f", [(UISlider *)sender value]);
[self doImageFilter:#"inputBrightness" withValue:[(UISlider *)sender value]];
}
And finally, here is the doImageFilter method I created, which actually does the filter and reassigns the UIImageView (this is where it turns white)
- (void) doImageFilter:(NSString *)filter withValue:(float)value{
[self.editingCIFilter setValue:[NSNumber numberWithFloat:value] forKey:filter];
CIImage *output = [self.editingCIFilter outputImage];
CGImageRef cgi = [self.editingCIContext createCGImage:output fromRect:[output extent]];
self.editingCIImage = output;
self.editingImage.image = [UIImage imageWithCGImage:cgi];
}
So I have a hunch that the CIImage isn't even being initialized properly in the first place; but I don't know why. Been at this for a couple hours now.
Thanks for all your help!
Cheers
Hahaha, oh wow.
So I figured it out, my CIContext and CIFilter need to be set to strong instead of weak.
Awesome.

XCode Prevent UIWebView flashing white screen after Launch Image

I am fairly new to XCode - I'm designing an app with a single view that just displays a UIWebView that loads my jQuery mobile web site.
I have the Default.png and Default#2x.png files for the Launch images in place. When I run the app on my test device or in the emulator, it shows my Launch Image, and then flashes a white screen while the UIWebView loads the first page.
I know that I can change the background color and opacity so it will flash a different color than white, but I would like to prevent the flash altogether. I would like the app to launch, show the Launch image, and not show the UIWebView until the first page has completely loaded. Help?
# Andreas Volz (and anyone else wondering how I solved the problem of the white "flash")
First, add the name-of-loading-image.png and name-of-loading-image#2x.png files to your Xcode project. The regular image should be 320 x 460, and the #2x image should be 640 x 920.
Then, in my ViewController.h file I added these properties:
#property (nonatomic, retain) UIWebView *webView;
#property(nonatomic, strong) UIImageView *loadingImageView;
#end
And then in my ViewController.m file I added this:
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController;
#synthesize webView;
#synthesize loadingImageView;
- (void)viewDidLoad
{
[super viewDidLoad];
//**************** Set website URL for UIWebView
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.example.com"] cachePolicy:NSURLCacheStorageAllowed timeoutInterval:20.0]];
//**************** Add Static loading image to prevent white "flash" ****************/
UIImage *loadingImage = [UIImage imageNamed:#"name-of-loading-image.png"];
loadingImageView = [[UIImageView alloc] initWithImage:loadingImage];
loadingImageView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"name-of-loading-image.png"],
nil];
[self.view addSubview:loadingImageView];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
// Remove loading image from view
[loadingImageView removeFromSuperview];
}
To know when your webView has finished loading, you must implement the UIWebViewDelegate protocol.
In your viewController's .h file:
#interface viewController : UIViewController <UIWebViewDelegate>
{
UIWebView *webView;
}
#end
Then in your viewController's .m file, add this method:
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSLog(#"content loading finished");
// Display your webView
[self.view addSubview:webView];
}
You can use the -viewDidLoad method in your viewController's .m to continue displaying your launch image. You can also begin the loading of content for your webView:
- (void)viewDidLoad {
[super viewDidLoad];
// continue to display launch image
UIImage *launchImage = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"launchImage.png" ofType:nil ]];
UIImageView *launchImageView = [[[UIImageView alloc] initWithImage:launchImage] autorelease];
[self.view addSubview:launchImageView];
// now setup the base view for the webView controller
UIView *contentView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
contentView.backgroundColor = [UIColor blueColor];
// for rotation
contentView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
self.view = contentView;
//create a frame to size and place the web view
CGRect webFrame = [[UIScreen mainScreen] applicationFrame];
UIWebView *aWebView = [[UIWebView alloc] initWithFrame:webFrame];
self.webView = aWebView;
//aWebView.scalesPageToFit = YES;
aWebView.autoresizesSubviews = YES;
aWebView.autoresizingMask=(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);
//set the web view delegates for the web view to be itself
[aWebView setDelegate:self];
//determine the path the to the index.html file in the Resources directory
NSURL *url = [[NSBundle mainBundle] URLForResource:#"index" withExtension:#"html"];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[aWebView loadRequest:requestObj];
}

Continuously checking two NSSecureTextField

how can i make this to keep checking all the time the 2 NSSecureTextFields to know if they are equal/different to display the images without the need of a button?
the .h file:
IBOutlet NSSecureTextField *textField;
IBOutlet NSSecureTextField *textField2;
IBOutlet NSImageView *imagem;
}
- (IBAction)verificarPass:(id)sender;
the .m file:
- (IBAction)verificarPass:(id)sender;
{
NSString *senha1 = [textField stringValue];
NSString *senha2 = [textField2 stringValue];
NSImage *certo;
NSImage *errado;
certo = [NSImage imageNamed:#"Status_Accepted.png"];
errado = [NSImage imageNamed:#"Error.png"];
if ([senha1 isEqualToString:senha2]) {
[imagem setImage:certo];
}else{
[imagem setImage:errado];
}
}
Add a target method to the textField controls, triggered by the UIControlEventEditingChanged control event:
[textField addTarget:self action:#selector(verificarPass:) forControlEvents:UIControlEventEditingChanged];

Resources