I'd like to set an action to a "part" of UILabel not the all of UIlabel, like the "term of service" of attached picture. I am using storyboard. Now, I'm thinking to cover the UIButton over the part of UILabel. However, it is troublesome to adjust the part of label with the button at any layout(iPhone 5, 6 and 6 plus) by autolayout. If you know better way, please tell me. Thank you for your kindness.
I'd recommend using a UITextView with an NSAttributedString. Get the range of the text you are looking for, and then add the button as a subview to the view.
NSRange termsOfServiceRange = [self.termsOfUseTextView.text rangeOfString:#"Terms of Service"];
self.termsOfUseTextView.selectedRange = termsOfServiceRange;
UITextRange *termsOfServiceTextRange = [self.termsOfUseTextView selectedTextRange];
CGRect termsOfServiceFrame = [self.termsOfUseTextView firstRectForRange:termsOfServiceTextRange];
CGRect convertedFrame = [self.view convertRect:termsOfServiceFrame fromView:self.termsOfUseTextView];
UIButton *termsOfServiceButton = [[UIButton alloc]initWithFrame:convertedFrame];
[termsOfServiceButton setBackgroundColor:[UIColor clearColor]];
[termsOfServiceButton addTarget:self action:#selector(termsOfServiceButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:termsOfServiceButton];
[self.view bringSubviewToFront:termsOfServiceButton];
I've solved by using TTTAttributedLabel!
https://github.com/TTTAttributedLabel/TTTAttributedLabel
Related
I have seen a bunch of similar questions, but noone got the answer I am looking for. When I use this code to make a UIButton and set it's titleLabel, the UIButton appear, but the titleLabel won't appear.
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
[button setBackgroundImage:[UIImage imageNamed:#"button.png"] forState:UIControlStateNormal];
button.titleLabel.text = #"Title";
button.titleLabel.font = [UIFont fontWithName:#"System-Bold" size:25.0f];
[self.view addSubview:button];
This code displays the button, but not the titleView, it's title. Why can that be? I am developing for iOS 6 and higher.
NOTE 1: I am not looking for an option like this:
[button setTitle:#"Title" forState:UIControlStateNormal];
Because I have to use the titleLabel to set it's font and fontSize.
NOTE 2: I can't create a UILabel and add it as a subView of the button. Since the button is later animating.
You always need to specify ControlState when updating button's title!
There are four possible values for UIButtons:
UIControlStateNormal
UIControlStateHighlighted
UIControlStateDisabled
UIControlStateSelected
so for example:
[button setTitle:#"Title" forState:UIControlStateNormal];
and then you can set custom settings for titleLabel:
[button.titleLabel setFont:[UIFont fontWithName:#"Zapfino" size:20.0]];
[button.titleLabel setTextColor:[UIColor blueColor]];
[button setTitle:#"Title" forState:UIControlStateNormal];
Is the correct way to set the title string.
titleLabel is used to set the font and color.
Try using...
[button setNeedsLayout];
[button layoutIfNeeded];
It will force button to update the layout.
If in the storyboard with a custom button image from assets like me
Check-in Attribute inspector
Check-in Attribute inspector set asset image as a background
Although this property is read-only, its own properties are read/write. Use these properties primarily to configure the text of the button. For example:
UIButton *button = [UIButton buttonWithType: UIButtonTypeSystem];
button.titleLabel.font = [UIFont systemFontOfSize: 12];
button.titleLabel.lineBreakMode = NSLineBreakByTruncatingTail;
Do not use the label object to set the text color or the shadow color. Instead, use the setTitleColor(:for:) and setTitleShadowColor(:for:) methods of this class to make those changes. To set the actual text of the label, use setTitle(_:for:) (button.titleLabel.text does not let you set the text).
The titleLabel property returns a value even if the button has not been displayed yet. The value of the property is nil for system buttons.
in swift :
button.setTitle("your text", for: .normal)
button.setTitleColor(.white, for: .normal)
TLDR - try this saveButton.titleLabel?.layer.zPosition = 10
This was a frustrating one for me. I created a UIButton programmatically, added constraints, and added a gradient background (this is what caused my problem...I think). Then I needed to update the font:
saveButton.titleLabel?.font = UIFont.systemFont(ofSize: 20, weight: .semibold)
saveButton.setTitle("Save", for: .normal)
Notice I'm setting the title in the correct way. But still, no title showing. The craziest part - when I clicked "Debug View Hierarchy" I could see the title right there! If I didn't change the font, the title showed as well. Confusing. Then I added this (literally flinging spaghetti and seeing what stuck):
saveButton.titleLabel?.layer.zPosition = 10
It worked. It seems like some combination of gradient background, setting the font, etc was putting the button's internal title label at the bottom of its layers.
http://g.virbcdn.com/_f/cdn_images/resize_1280x640/29/PageImage-489404-2437983-IMG_4531.PNG
Hi all, we are trying to figure out how to do the small orange numbered label on the left in the image above. As you can see, when the number is 6, it looks like a circle. When it is 33, the label looks wider. Does anyone know how to make it? Our developer believes it is made with UIButton. Is it possible to do it with label? Thanks in advance.
cornerRadius property of the CALayer class can be use to achieve this. Every view has a CALayer instance on which we can perform certain action. Means we can get rounded corners by using -
myLabel.layer.cornerRadius = //radius that we want;
Also for accessing layer we need to have following framework in our application-
<QuartzCore/QuartzCore.h>
First Add QuartzCore FrameWork, then
use in .m file
#import "QuartzCore/CALayer.h"
then use this code where do you want to show
UIButton *Mybutton = [UIButton buttonWithType:UIButtonTypeCustom];
Mybutton.backgroundColor=[UIColor orangeColor];
[Mybutton addTarget:self action:#selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
[Mybutton setTitle:#"1" forState:UIControlStateNormal];
Mybutton.frame = CGRectMake(135.0, 180.0, 40.0, 40.0);
Mybutton.clipsToBounds = YES;
Mybutton.layer.cornerRadius = 20;
Mybutton.layer.borderColor=[UIColor greenColor].CGColor;
Mybutton.layer.borderWidth=2.0f;
[self.view addSubview:Mybutton];
button will look like...
i have UIlabel with image.my text are won't come into top of the image i want to show my text in top of the image.
image1.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"salarytax.png"]];
this is my code to adding image to label. this image is a output of that code. how can i show the text in top of the image
try adding the UILabel on the image
[myimageview addSubView: myLabel];
[myImageView bringSubviewToFront: myLabel];
If you have just taken the UILabel and trying to set its background as an image then dont think it would create a problem.
If you are able to view the text on the label but you want the text to be vertically on top then you can set its contentVerticalAlignment property to top.
Hope this helps..
UILabel *imageLabel;
[imageLabel setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"logo.png"]]];
[imageLabel setText:#"Khalid Usman"];
[imageLabel setTextColor:[UIColor blackColor]];
Note: I have checked it for conformation then i post. It's working.
I have a problem .. I used this http://kwigbo.com/post/318396305/iphone-sdk-custom-uialertview-background-color to create my own custom UIAlertView.
I do not know why but this will not work:
UILabel *theTitle = [theAlert valueForKey:#"_titleLabel"];
[theTitle setTextColor:[UIColor redColor]];
UILabel *theBody = [theAlert valueForKey:#"_bodyTextLabel"];
[theBody setTextColor:[UIColor blueColor]];
the color of the title does not change .. the color of texbody it's ok.
How can I customize the buttons?
Hi Achieved same thing using custom UIAleartView.
Make a custom view as follows.
UIView *tempView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 5.0f, 262.0, 49)];
tempView.backgroundColor = [UIColor clearColor];
UILabel *lblTitle = [[UILabel alloc] initWithFrame:CGRectMake(12, 0, 262.0, 49)];
lblTitle.font = [UIFont fontWithName:#"Arial" size:22.0f];
lblTitle.textColor = [UIColor whiteColor];
lblTitle.textAlignment = UITextAlignmentCenter;
lblTitle.text = #"Subscribe";
lblTitle.backgroundColor = [UIColor clearColor];
[tempView addSubview:lblTitle];
alreadySubscriber = [[UIButton alloc] initWithFrame:CGRectMake(12, 260, 262.0, 50)];
alreadySubscriber.layer.cornerRadius = 25.0f;
[alreadySubscriber setTitle:#"Already a subscriber" forState:UIControlStateNormal];
[alreadySubscriber setBackgroundImage:[UIImage imageNamed:#"BTN0.png"] forState:UIControlStateNormal];
[alreadySubscriber setBackgroundImage:[UIImage imageNamed:#"BTN1.png"] forState:UIControlStateSelected];
[tempView addSubview:alreadySubscriber];
Insert this in UIAleartView
[self insertSubview:tempView atIndex:0];
[self setNeedsLayout];
Override layoutsubview as you have already done push all other controls down equal to view Height.
-> basically matter is to hide UIAleartView's title behind a label.
Not sure exactly what your issue might be, but we faced a similar situation when trying to workout a custom UIAlertView, so might be similar.
The custom solution in the link you provided appears to manipulate the alerts title and background by accessing the subview hierarchy and 'guessing' which subview might be which. (I may be wrong, didn't look through it in detail) The problem with this approach is that it'll work fine for one OS version, but in subsequent OS versions, Apple may restructure this subview hierarchy in some manner, and this 'guesswork' is no longer accurate. (i.e. the subview assumed to be the background image may not be).
This could be the case, seeing that posted link is an year old. If you're proceeding with this, you may have to review the subview hierarchy to see if they still match up.
I believe, that you can find proper solution without using standart tools, which are present in UIAlertView. But in this case, you application will be not approve for AppStore. That way, I strongly recommend you, avoid to using custom buttons in AlertView.
Maybe, you will find solution using UIActionSheet instead UIAlertView . It's more customizing.
If #"_titleLabel" is part of the UIAlertView hierarchy (which looks likely given the _ prefix), I can't recommend your approach.
Apple might some day change the key strings: If #"_titleLabel" ever changes, say to #"_titleLabelView", you're sunk and you might never know that you're sunk. This might even be grounds for rejection, I wouldn't know.
It's better to start out from scratch with your own custom view, subclassing UIView. Only then can you guarantee that this will be stable from OS to OS. On top of this, the time you lose trying to find a shortcut will be positively spent constructing some thing durable.
I'm trying to add subviews to a UIButton. This is working fine right now. But the button isn't clickable anymore as soon as I add the subviews.
I use the following code:
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(column*100+24, row*80+10, 64, 64);
[button addSubview:asyncImage];
[button addSubview:price];
[button addTarget:self
action:#selector(buttonClicked:)
forControlEvents:UIControlEventTouchUpInside];
The button works again if I remove the 2 addSubview: methods. If anyone knows how to fix this it would be great!
I found a quick solutions. I needed to set the asyncImageView to the following:
asyncImage.userInteractionEnabled = NO;
asyncImage.exclusiveTouch = NO;
After this, it worked!
try:
[UIButton buttonWithType:UIButtonTypeCustom];
instead of:
[UIButton buttonWithType:UIButtonControlType];
The important thing here is to make sure that userInteractionEnabled will be set to NO. Fortunately it works immediately for UIImageView and UILabel (maybe for other subclasses of a UIView but those are the most popular subviews added to button) because by default for this classes it is set to NO by default. Unfortunately it is set to YES in UIView so make sure to change it in that case. Messing around with any other flags shouldn't be necessary. The nature of problem is that many people do not know that default value of this flag is different in subclasses.
Have you tried to put:
[asyncImage setUserInteractionEnabled:YES];
in the same sitiation i make this action:
inherit from UIButton and add all labels and imageview's of button to self, finally put new button to view as last subview and add targets of self button to this last button(also set backgroundColor to clearColor for transparent). now it will be clickable and works fine.
In Swift, test that is you have your UIButton blocked
uibtn.userInteractionEnabled = false
uibtn.exclusiveTouch = false
I added a label to the subview button.
For a very long time I was looking for why my text in the button is not clickable. Solution in this thread:
myLable.isUserInteractionEnabled = false