Xcode link with label - iphone

I want to create hyperlink like in html text. Is it possible to hyperlink a label in Xcode?

I suggest using the Round Rect Button then setting the background image to be an image that is the same color as your view's background.

Just add a tapGestureRecognizer to that label
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(labelTapped:)];
tapGesture.numberOfTapsRequired = 1;
self.theLabel.userInteractionEnabled = YES;//Since by default, UILabel is not interaction enabled
[self.theLabel addGestureRecognizer:tapGesture];
//Some where in the code
-(void)labelTapped:(UIGestureRecognizer *)sender
{
//Do some stuff
}

Related

Set UIImageView background color upon user intervention

I am developing an application in which I have a horizontal scroller.
The scroller area comprise of different colors [which are nothing but UIImageView of colors].
Above the scroller area is a canvas [which is also a UIImageView].
Now, what I want is that when I select any color in the scroller area, it should set on the canvas.
It is pretty much like a color picker but I am already providing solid color options to be selected by the user.
How should I go about it ? Any sample Code to get me started or my mind kicking would be awesome ?
Thanx a ton
To implement it easily.
Use UIView not UIImageView
Set UIView backgroundColor and place it on the scroll
Set UITapGestureRecognizer on views,Refer here
when a purticular view is touched you have its instance of view which
is touched
get the background color of the view and set it in the scroller
Can you take UIButtons instead of UIImageViews inside your scrollView to show colours to be picked.
This will make the implementation easier. User will select any colour button and you will receive a callback in buttons action selector method. Then based on button tag or any property you can set the colour to your canvas.
In current implementation it will be tricky as you need to know the exact content offset where the tap is done to make out which UIImageView was pressed.
coding steps:
In your scroll view add UIButtons instead of UIImageView like:
UIButton* button = [[UIButton alloc] initWithFrame:someRect];
//put some tag to button
button.tag = someInt;
//add selector to each button to get callback
[view addTarget:self action:#selector(btnSelected:) forControlEvents:UIControlEventTouchUpInside];
[scrollView addSubview:button];
In the btnSelected method put this code:
-(IBAction)btnSelected:(id)sender;
{
UIButton* button = (UIButton*) sender;
if (button.tag == someInt) {
//do something like
canvas.backgroundColor = button.backgroundColor;
}
}
Instead of using UIImageViews or UIViews as suggested, I would put custom UIButtons for picking colors.
UIButton * colorButton;
//You will probably do these inside a for loop
colorButton = [UIButton buttonWithType:UIButtonTypeCustom];
[colorButton setFrame:CGRectMake(X, Y, buttonSize, buttonSize)];
[colorButton setTitle:#"" forState:UIControlStateNormal];
[colorButton setBackgroundColor:SOME_COLOR];
//Border visualization would be good if you are putting buttons side-by-side
[colorButton.layer setBorderWidth:1.0];
[colorButton.layer setBorderColor:[[UIColor darkGrayColor] CGColor]];
//You can use this tag to identify different buttons later
[colorButton setTag:INDEX+SOME_OFFSET]; //use loop index with some offset
[colorButton addTarget:self action:#selector(colorButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
-(void) colorButtonPressed:(UIButton *) sender
{
UIColor *selectedColor = sender.backgroundColor; //Or identify with sender.tag
[yourCanvas doSmtOnCanvas:selectedColor];
}
Initialize UIImageView then add gesture recognizer to all imageViews by calling this method.
- (void) setupImageView : (UIImageView *) imageView{
[imageView setUserInteractionEnabled:YES];
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(changeUIImageViewBackgroundColor:)];
tapGestureRecognizer.numberOfTapsRequired = 1;
[imageView addGestureRecognizer:tapGestureRecognizer];
}
Now change color of canvasView within the selector by following way:
- (void) changeUIImageViewBackgroundColor : (UITapGestureRecognizer *) recognizer{
UIImageView *imageView = (UIImageView *)[recognizer view];
[canvasView setBackgroundColor:[imageView backgroundColor]];
}
You can put a tap gesture in your scrollview and whenever a tap is made in the scroll check whether the tap is on the imageview. (You will get whether the tap point in on imageview or not) and according you can change the image.
Second alternative is to take a custom button and handle on its click.
Hope it helps

iOS Gesture Handling: where to add a gesture recognizer when using a custom UIView

I have a view controller where I display a grid/array of images, where every image view is a custom nib (custom nib because images have a name & like/dislike icon too). So I displayed the grid of images doing something like this in my view controller viewDidLoad.
int row=0, col=0;
for (int i=0; i<arrayImg.count; i++) {
NSArray *topObj = [[NSBundle mainBundle] loadNibNamed:#"CustomImageView" owner:nil options:nil];
CustomImageView *imgView = [topObj objectAtIndex:0];
imgView.frame = CGRectMake(180*col+10, 180*row+10, 170, 170);
// custom image values inserted here
[self.view addSubView:imgView];
// update the row,col variables here
}
Now I need to add a tap gesture recognizer to every image displayed on the screen. It seems logical to me to add the gesture recognizer inside the custom nib/class, CustomImageView in this case. CustomImageView extends UIView, so it seems a gesture recognizer cannot be declared here (auto-complete does not appear, syntax highlighting does not work either). What am I missing over here?
You can surely add a gesture recognizer to your CustomImageView (provided it is a UIView). Try something like this:
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapped:)];
[tapRecognizer setNumberOfTapsRequired:1];
[tapRecognizer setDelegate:self];
[imgView addGestureRecognizer:tapRecognizer];
Note that the only method that you should see auto-completed is addGestureRecognizer.
In general, prefer the official documentation (or the compiler, if you like) over auto-completion in order to decide whether a feature is present or not. Auto-completion is not always right, in my experience.

How to change the alpha of a UILabel by tapping?

I have an iOS (4.0) app that I would like to change the alpha of a specific UILabel of just by taping anywhere on the screen. I don't have my interface done programmatically, I just used the interface builder to place the labels for things on the screen.
How can I use a tap gesture to change the alpha of a specific UILabel in my program?
Thanks in advance!
In viewDidLoad:
UITapGestureRecognizer *oneTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapAction:)];
[oneTap setNumberOfTapsRequired:1];
[oneTap setNumberOfTouchesRequired:1];
[self.view oneTap];
Then define the method:
-(void)tapAction:(UIGestureRecognizer *)gesture
{
[self.yourLabel setAlpha:0.5f]; // set the alpha to whatever you want, or animate the fade, whatever
}
-(IBAction)myPressedButton:(id)sender {
float x;
x = theLabel.alpha;
foat a=0.1;
self.theLabel.alpha = x-a;
}

how can I get image tag from different image views on tapping on it

I want to used image view from nos. of image views in scroll view on single tapping any particular image view.
If you are insistent on using images instead of buttons you can use Gesture Recognizer. Create an imageView and enable its userInteraction
UIImageView *testImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"someImage"]];
testImageView.frame = CGRectMake(30.0,30.0,60.0,40.0);
testImageView.tag = 30;
testImageView.userInteractionEnabled = TRUE;
[tempPlotView addSubview: testImageView];
[testImageView release];
Now allocate a gesture Recognizer object and add it to your imageView...
UITapGestureRecognizer *testGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(singletap:)];
[testGesture setNumberOfTapsRequired:1];
[testImageView addGestureRecognizer: testGesture];
[testGesture release];
Now in the selector "singleTap" you can do whatever your action is..
-(void)singleTap:(UIImageView*)sender{
if(sender.tag == 30){
//do your stuff here...
}
}
Hope this help...cheers....
Create a button and set image as background.while creating button you can set tag like
button.tag=yourtag;//your tag integer value
[button addTarget:self action:#selector(buttonTouched:) forControlEvents:UIControlEventTouchDown];
[button setBackgroundImage:[UIImage imageNamed:#"img.png"] forState:UIControlStateNormal];
and implement this function in your clss
- (void)buttonTouched:(UIButton *)sender
{
NSLog(#"touched %i",[sender tag]);
}
while tapping the particular button this function will get called.
Rather than use image views, you could use UIButtons with image views as their content.
That way, you'll get a callback when a given image is tapped with a reference to the button. From there you should be able to get the tag of that which has been tapped!
I hope this helps,
You can do according to what Nick has suggested.
Or else, you can create a subclass of the imageview. Set frame and tag to it.
In touchesEnded method of this custom class, you can find which imageview it is based on its tag.

Making UILabel touchable

I have a UILabel and I would like to make it react to a touch. I tried putting a button on top of the label, thanks to this I could interact with the button. However, the button cannot be fully transparent, right? I could set an alpha of the button to 0,02, but it is still visible on by background. How to solve this? Maybe I could set the properties in some other way to make them fully invisible? Or is there some other solution?
First, why not just use a button and set the button title to the label's contents?
If you can't/don't want to do that, you can also set userInteractionEnabled = YES on the label and then add a gesture recognizer to the label.
In Swift:
label.userInteractionEnabled = true
let gestureRecognizer = UITapGestureRecognizer(target: self, action: Selector("labelPressed"))
label.addGestureRecognizer(gestureRecognizer)
Get your click in Action :
func labelPressed(){
print("Label pressed")
//Your awesome code.
}
I usually do this:
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(pushAction)];
[myLabel addGestureRecognizer:tap];
I don't know if it works with labels, but then i just make a transparent UIView with the same rect and put it on top.
Okay, i checked, it only works in UIView, but then, do this:
UIView *tapView = [[UIView alloc] initWithFrame:myButton.frame];
And put "tapView" in the addGestureRecognizer-method.