Cocoa Hyperlink label issue? - ios

I have a hyperlink label in my application.Clicking on it opens the application i want.
The problem is after i click on it the font size of the label shrinks automatically.
Is there a way to avoid this??
Please suggest a solution for this issue. Thanks in advance

I would suggest you to use UIButton with UIButtonTypeCustom and clear background instead of UILabel. It will look like UILabel and in the action method you can perform any task.
UIButton *btn = [[UIButton alloc] initWithFrame:btn_frame];
[btn setBackgroundColor:[UIColor clearColor]];
[btn addTarget:self action:#selector(DoSomething) forControlEvents:UIControlEventTouchUpInside];
[btn setTitle: #"Button_Title" forState: UIControlStateNormal];
[btn setTitleColor: #your_choice forState:UIControlStateNormal];
[btn setTitleColor: #your_choice forState:UIControlStateHighlighted];
btn.titleLabel.font = #your_choice
btn.titleLabel.textAlignment = #your_choice;
btn.contentVerticalAlignment = #your_choice;
if still you want UILabel here is link might help you..
Attributed Label

Related

UIButton set Text on top of image

How can i add a Text in a button which already contains an image. I've tried out various methods but the text is not visible. How can i make the text visible on top of the image ?
Can anyone help me !
try setting the backgroundImage property of button
[self.buttonWithTextAndImage setBackgroundImage:[UIImage imageNamed:#"image.png"] forState:UIControlStateNormal];
[self.buttonWithTextAndImage setTitle:#"Button Title" forState:UIControlStateNormal];
You need to set the background image of the button, instead of the image, using setBackgroundImage:forState:
If your button's image set in setImage state, then the text is not going to display on Button.
If you have to display name on button then we have to set setBackgroundImage and then you can set the title for button.
[btn setBackgroundImage:YOURIMAGE forState:UIControlStateNormal];
[btn setTitle:YOURTITLE forState:UIControlStateNormal];
Set background image and title on a button as-
[self.m_myBetButton setBackgroundImage:[UIImage imageNamed:#"buttonImage.png"] forState:UIControlStateNormal];
[self.m_myBetButton setTitle:#"BUTTON TITLE" forState:UIControlStateNormal];
[yourButton setBackgroundImage:[UIImage imageNamed:#"test.png"] forState:UIControlStateNormal];
[yourButton setTitle:#"Click" forState:UIControlStateNormal];
This must work.
something, that worked fine for me: I set the Button in InterfaceBuilder and use the tiny Icons near "Position" to define the position of the text. Mouseover shows the code which is applied by InterfaceBuilder.
Now if the text doesn't show, due to color, i use a reference to the button and set an attributedTitle:
NSDictionary *atts = [[NSDictionary alloc]initWithObjectsAndKeys:[NSColor whiteColor], NSForegroundColorAttributeName, nil];
NSAttributedString *example = [[NSAttributedString alloc]initWithString:#"myText" attributes:atts];
[myExampleButton setAttributedTitle:example];
I have found a better way.
UILabel *next=[ [UILabel alloc] initWithFrame:CGRectMake(0,0,375,65)];
next.text=#"SIGN IN";
next.textAlignment = NSTextAlignmentCenter;
next.font=[UIFont fontWithName:#"Helvetica" size:20.0 ];
next.textColor=[UIColor whiteColor];
[self.button addSubview:next];

Switched from UILabel to UIButton. Text disappears

I just switched from a UILabel to UIButton, intending to use the button's titleLabel property to display the information formerly displayed in the label replaced by the button.
Problem is, the text doesn't show up. I've checked to see if the button itself (normally with a transparent background, changed to red to check) is appearing, and it is, but the text isn't there. Here's a sample of the relevant code, which is identical to the original (working) code from the label, changing only lines like:
UILabel *firstLabel;
(...)
firstLabel.textColor = [UIColor blackColor];
to:
UIButton *firstLabel;
(...)
firstLabel.titleLabel.textColor = [UIColor blackColor];
Here's a full chunk for clarity:
firstLabel.frame = CGRectMake(thisRiser.bounds.origin.x, thisRiser.frame.size.height -focusBarEndHeight - 55, barWidth, 60);
firstLabel.backgroundColor = [UIColor clearColor];
firstLabel.titleLabel.textColor = [UIColor blackColor];
firstLabel.titleLabel.text = [NSString stringWithFormat:#"%#\n%.2f%%\nTime--%#",focusItemName,focusItemPercent,actualDurationFocusItem];
[firstLabel.titleLabel setFont:[UIFont boldSystemFontOfSize:12]];
firstLabel.titleLabel.numberOfLines = 0;
firstLabel.titleLabel.textAlignment = NSTextAlignmentCenter;
[firstLabel setHidden:NO];
What am I missing? Any ideas?
Thanks!
UPDATE --
This may be related to a known bug!
Many thanks to the responders below. I implemented the first recommended fix, which resolved my initial problem, so my text now appears in the label. However, the UIControlStateHighlighted and UIControlStateSelected don't work.
For example, this code produces no discernible effect to the text when clicked:
firstLabel.backgroundColor = [UIColor clearColor];
[firstLabel setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[firstLabel setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
[firstLabel setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
After searching around SO for a while, I'm coming to the conclusion that there is a bug (at least in iOS 7) that prevents proper functioning of these methods. Unfortunately, I'm not smart enough to provide more detailed information, but plenty of recent posts point to a major problem in this area of UIControl.
I'll be ecstatic to be proven wrong, because I'd like to use this functionality.
UIButton responds to different messages, you can use the button state to change its title.
You can set the title like this;
UIButton *button = [UIButton buttonWithType: UIButtonTypeCustom];
[button setTitle:#"Title goes here" forState:UIControlStateNormal];
See Control State: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIControl_Class/Reference/Reference.html#//apple_ref/c/tdef/UIControlState
To set a title in an UIButton, you have to init the button:
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(10, 10, 60, 20)];
and then use:
[button setTitle:#"text" forState:UIControlStateNormal];
where you must specific the control state (highlighted, selected, etc.)
try for example,
UIButton *firstLabel = [UIButton buttonWithType:UIButtonTypeCustom];
firstLabel.frame = CGRectMake(0, 60, 100, 50); //set the frame
[firstLabel setTitle:#"Hello" forState:UIControlStateNormal];
[firstLabel setBackgroundColor:[UIColor redColor]];//for test
[firstLabel setTitleColor:[UIColor greenColor] forState:UIControlStateNormal]; //it will always displays green for normal
[firstLabel setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];//if u touch it text changed to white instantly
[firstLabel setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];//if touch it and hold it shows white color

UIButton Highlighted State not showing when clicking over a Selected UIButton

I want my UIButton to show up the highlighted state when I click over a button that is already selected.
Basically in the highlighted state I apply a *.png image as my UIButton backgroundImage to give a pressed down effect.
But if the button is already in the Selected State When I click over it again I just can't see the highlighted state but it goes straight to the normal state!
Watch the Issue--> Video of the Issue!
Help please
//0 init UIButton
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(x, y, aSide, aSide)];
//1 Give it a backgroundColor
[button setBackgroundColor:aColor];
[..]
//2 Set titleLabel and its style
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
[button setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
UIImage *shadowImage = [UIImage imageNamed:kBtnShadow];
shadowImage = [shadowImage stretchableImageWithLeftCapWidth:floorf(shadowImage.size.width/2) topCapHeight:floorf(shadowImage.size.height/2)];
[button setBackgroundImage:shadowImage forState: UIControlStateHighlighted];
[button setTitle:aLabel forState: UIControlStateNormal];
//3 Assign tag and Action
[button setTag:tag];
[button addTarget:target action:a forControlEvents:UIControlEventTouchUpInside];
The various states: UIControlStateNormal, UIControlStateSelected, and (UIControlStateSelected | UIControlStateHighlighted) are all actually distinct. If you want your shadowImage to apply both in the (only) highlighted state and in the highlighted+selected state, you must also set:
[button setBackgroundImage:shadowImage forState:(UIControlStateHighlighted | UIControlStateSelected)]
In swift this would be:
button.setBackgroundImage(shadowImage, forState: UIControlState.Selected.union(UIControlState.Highlighted))
In Swift v3 (Nov. 2016):
button.setBackgroundImage(shadowImage, for: UIControlState.selected.union(UIControlState.highlighted))
Swift 4.2
Applicable programmatically only.
aButton.setImage(UIImage(named: "your_image"), for: [.selected, .highlighted])

UIButton color change after external event

I need a quick way to change the color of an UIButton live (in response to a delegate call) . So the button is drawn with a white background (standard) then when a delegate call comes it needs to change to a blue/red background.
I tried :
btn.backgroundColor = [UIColor redColor];
[btn setNeedsDisplay];
This doesn't work...
Also it would be a good ideea to change the color of the button text... How is this done????
Try using it like this :
[button setBackgroundColor:someColor forState:UIControlStateNormal];
Did it work for you .. ? I am assuming you have made your button a custom button type.
If it worked, you may want to consider, accepting the answer
First make your UIButton type Custom..
UIButton* myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton setbackgroundColor:[UIColor blueColor] forState: UIControlStateNormal];
[myButton setTitleColor:[UIColor blueColor] forState: UIControlStateNormal];

Custom UIButton in UIscrollview

I am using xcode 4.6 to develop an app. Here i want to add UIButton programmatically to UIscrollview. This is the code i follow.
UIButton *bt =[[UIButton alloc]initWithFrame:frame];
bt=[UIButton buttonWithType:UIButtonTypeCustom];
[bt setTitle:#"Custom Button" forState:UIControlStateNormal];
[bt addTarget:self action:#selector(userTappedOnLink:) forControlEvents:UIControlEventTouchUpInside];
bt.backgroundColor = [UIColor grayColor];
bt.titleLabel.textColor=[UIColor blueColor];
[self.mainscrollview addSubview:bt];
[self.mainscrollview bringSubviewToFront:bt];
Now the problem is that Button gets disappeared (technically its textcolor becomes white) on click. I checked keeping UIscrollview color to red that th button was still in the view but i cant get the reason why its text color changed and how do i undo dis.
Basically I wan to create a clickable link using UIbutton.
I know uitextview approach (datadetectortype) but its of no use as i want to show different text in the label for the link and the actual link.
Note: The textcolor doesnt change back to blue and remains white only.
Thanks in advance.
Try the below code
UIButton *bt =[UIButton buttonWithType:UIButtonTypeCustom];
bt.frame = CGRectMake(50.0, 50.0, 100.0, 50.0);
[bt setTitle:#"Custom Button" forState:UIControlStateNormal];
[bt addTarget:self action:#selector(userTappedOnLink:) forControlEvents:UIControlEventTouchUpInside];
bt.backgroundColor = [UIColor grayColor];
bt.titleLabel.textColor=[UIColor blueColor];
[self.scrollTest addSubview:bt];
-(void)userTappedOnLink:(UIButton*)sender
{
NSLog(#"Test ..");
[self performSelector:#selector(changeBtnTextColor:) withObject:sender afterDelay:1.0];
}
-(void)changeBtnTextColor:(UIButton*)btn
{
btn.titleLabel.textColor=[UIColor blueColor];
}
hope it will work for you.
use below code you will get your solution
UIButton *bt =[[UIButton alloc]initWithFrame:frame];
bt=[UIButton buttonWithType:UIButtonTypeCustom];
[bt setTitle:#"Custom Button" forState:UIControlStateNormal];
[bt addTarget:self action:#selector(userTappedOnLink:) forControlEvents:UIControlEventTouchUpInside];
[bt setBackgroundColor:[UIColor grayColor]];
[bt setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[self.mainscrollview addSubview:bt];
[self.mainscrollview bringSubviewToFront:bt];
Use the below code:
[bt setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
Problem is you are set Title to titleLabel and you are changing textColor of buttonTitle color.
All the best !!!
check Button's Fram .. is it valid or not ??
and remove bt=[UIButton buttonWithType:UIButtonTypeCustom]; line from your code.

Resources