I would like create a button with two independent labels.
I want to set in one button, two separate texts in different colors.
e.g.
[myButton setTitle: #"Title" forState: UIControlStateNormal];
[myButton setTitle2: #"Title2" forState: UIControlStateNormal];
Is it even possible? Maybe I should need to create a new control? Can anyone help me? I will be very grateful for a tutorial step by step.
If you wanted to do it all in Interface Builder you could place two UILabels where you want them in the view and then lay a UIButton over the top of them. Then with the Attributes Inspector change the UIButton's type to "Custom" and delete any existing label or placeholder text it has. This will make the button clear so both labels show through and you can set the UIButton's IBAction to do whatever you want to the two separate UILabels.
Also, you can do this :
UIButton *testButton = [UIButton buttonWithType:UIButtonTypeCustom];
testButton.frame = CGRectMake(0, 0, 200, 40);
[self.view addSubview:testButton];
UILabel *firstLineTestButton = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 20)];
firstLineTestButton.text = #"First line";
firstLineTestButton.font = [UIFont systemFontOfSize:12];
[testButton addSubview:firstLineTestButton];
UILabel *secondLineTestButton = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, 200, 20)];
secondLineTestButton.text = #"Second line";
secondLineTestButton.font = [UIFont boldSystemFontOfSize:12];
[testButton addSubview:secondLineTestButton];
but the #vikingosegundo's solution is more appropriate for the new SDK
For iOS 6 and higher you can use setAttributedTitle:forState: to set an attributes string. A NSAttributedString can have many different attributes like text color.
Swift Version:
The following code also centres two titles both horizontally and vertically inside the button and has some font style adjustments.
let lineOne = UILabel.init(frame: CGRect(x: 0, y: self.button.frame.size.height / 4, width: self.button.frame.size.width, height: self.button.frame.size.height / 4))
lineOne.text = "Title 1"
lineOne.textColor = .black
lineOne.font = UIFont.boldSystemFont(ofSize: 13.0)
lineOne.textAlignment = .center
let lineTwo = UILabel.init(frame: CGRect(x: 0, y: lineOne.frame.size.height * 2, width: self.button.frame.size.width, height: self.button.frame.size.height / 4))
lineTwo.text = "Title 2"
lineTwo.textColor = .black
lineTwo.font = UIFont.boldSystemFont(ofSize: 13)
lineTwo.textAlignment = .center
self.button.addSubview(lineOne)
self.button.insertSubview(lineTwo, belowSubview: lineOne)
UIButton is a subclass of UIView. So you can add two different labels to your button using - (void)addSubview:(UIView *)view. Positioning could then be done either with Autolayout (if you are using that), or frames + springs/struts.
Related
I want to add a custom label (with a time stamp) in each cell as well as an image (for warning message) in JSQMessageViewController. I am already using bottomlabel as well as toplabel. But I am unable to get the result I want.
The image is a reference of what I would like it to look like
UIImage *bubbleImage = [UIImage imageNamed:#"Commentbox_right.png"];
UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(textFrame.origin.x-2, textFrame.origin.y-2, textFrame.size.width+4 , textFrame.size.height+7)];
imgView.image= [bubbleImage stretchableImageWithLeftCapWidth:bubbleImage.size.width/2-5 topCapHeight:bubbleImage.size.height/2];
[cell addSubview:imgView];
[cell bringSubviewToFront:txtViewMessage];
UILabel *lblTimeStamp = [[UILabel alloc]initWithFrame:CGRectMake(textFrame.origin.x+2, imgView.frame.size.height+imgView.frame.origin.y, 90, 10)];
[lblTimeStamp setText:message.dateTime];//set time here
[lblTimeStamp setFont:FONT_300_LIGHT(7)];
[lblTimeStamp setTextColor:GET_COLOR_WITH_RGB(129,129,129, 1)];
[lblTimeStamp setTextAlignment:NSTextAlignmentLeft];
[lblTimeStamp setBackgroundColor:[UIColor clearColor]];
[cell addSubview:lblTimeStamp];
Sorry i didn't get much time to look in to it .. but again you can add that image in same message bubble xib and add constraints according to your need. Try this xib
How I add custom Label in JSQMessageviewcontroller is... I declare Label text before ViewDidLoad.
// Add Text Label
let myLabel: UILabel = {
let lb = UILabel()
lb.translatesAutoresizingMaskIntoConstraints = false
lb.textAlignment = .center
lb.numberOfLines = 1
lb.textColor = UIColor.white
lb.font=UIFont.systemFont(ofSize: 22)
lb.backgroundColor = UIColor(red: 0.0/255.0, green:70.0/255.0, blue:110.0/255.0, alpha:1)
lb.text = NSLocalizedString("No Notification", comment: "")
return lb
}()
And add this code in viewDidiLoad or call anywhere you like.
self.view.addSubview(myLabel)
setUpMyLabel()
This is how I add custom label in my app. Hope this will help you. :)
I have a navigation controller with a custom back button on the left. I do this programmatically so this is not an auto-layout issue. My problem is that the navigation controller title is not centered, it is going off the right side of the screen. I remember seeing a fix for this awhile back dealing with setting some type of fixed space as the right bar button item, but I cannot seem to find anything similar to this now.
Could someone tell me how to set the navigation controller title to centered and if the title is too big for its space, set nav bar title to fix its font size to fit the width of title space. This all needs to be done programmatically, thanks!
In navigation controller, by default, the view controller A that pushes current view controller B. The title of A will show up in backBarButton in view controller B.
The navigation controller title not centered is also because the previous view controller title is too long.
To counter that, use this in viewWillAppear of view controller A:
self.navigationItem.backBarButtonItem= UIBarButtonItem(title: "",
style: UIBarButtonItemStyle.Plain, target: nil, action: nil)
self.navigationItem.backBarButtonItem!.title = ""`
Thanks so much for the answers, they were useful in helping my find a solution. My solution was to use the titleView property of the navigation controller. It sets the title without having to go through the trouble of making a custom UIView and the setAdjustsFontSizeToFitWidth property solved my problem of the font size being too big. I've added a little extra formatting to make it look nice, but I won't post it since it doesn't have to do with my question. Here is the code I am using:
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, self.view.frame.size.width-60, self.navigationController.navigationBar.frame.size.height)];
titleLabel.text = #"This is my big, long title";
titleLabel.textColor = [UIColor blackColor];
titleLabel.textAlignment = UITextAlignmentCenter;
[titleLabel setAdjustsFontSizeToFitWidth:YES];
self.navigationItem.titleView = titleLabel;
You have to add custom view in the navigation
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(10, 0, 150, 44)];
UILabel *titleLabel=[[UILabel alloc] initWithFrame:CGRectMake(10, 165,370, 25)];
titleLabel.text=#"Home";
titleLabel.textColor=[UIColor whiteColor];
[view addSubview:titleLabel];
[self.navigationController.navigationBar addSubview:view];
Try out this, it would help you and it is tested.
Objective C version
UIButton * button = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
[button addTarget:self action:#selector(yourSelector:) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"<" forState:UIControlStateNormal];
button.titleEdgeInsets = UIEdgeInsetsMake(-3, -15, 3, 15);
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithCustomView:button];
self.navigationItem.leftBarButtonItem = leftItem;
Swift version
button = UIButton(frame: CGRectMake(0, 0,50, 50))
button?.addTarget(self, action: "backButtonTapped:", forControlEvents: UIControlEvents.TouchUpInside)
button?.setTitle("<", forState: .Normal)
button?.titleLabel?.font = UIFont(name: kFontHelveticaNeueRegular, size: 30)
button?.titleEdgeInsets = UIEdgeInsetsMake(-3, -15, 3, 15)
button?.setTitleColor(UIColor.whiteColor(), forState: .Normal)
var leftItem = UIBarButtonItem(customView: button!)
controller.navigationItem.leftBarButtonItem = leftItem
Add following method in your Utilities class
+ (void)setTitle:(NSString *)title forViewController:(UIViewController *)viewController {
UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
[label setTextAlignment:NSTextAlignmentCenter];
[label setTextColor:[UIColor whiteColor]];
[label setFont:[UIFont boldSystemFontOfSize:18]];
[label setText:title];
[label sizeToFit];
viewController.navigationItem.titleView = label;
}
Use across view controllers as follows
[Utilities setTitle:#"Title for View Controller" forViewController:self];
Add both leftbarbutton and Rightbarbutton . Usually back button exist . If you add an empty right bar button , title will get centered.
I want to create a header toolbar like shown in the image below. It is from the web view of the Twitter app.
I created a UIToolbar and put it at the top. Then I inserted the Buttons left and right and changed the Identifier to get the correct symbols.
My problem is the text in the middle. I create a UIBarButtonItem and placed it between the buttons. Here is my Problem.
How to I achieve that the UIBarButtonItem title does not overlap the left and the right button when the title is to long?
In my case:
How do I achieve that the title gets ... at the end if it gets to long?
How can I set the sub title?
How can I achieve that the button is not clickable, i.e., has color black?
Edit Using the answer from #Viral Savaj: Here is what it looks like:
To test if the title is too long, you should use character counting. Let myHeaderBarString represent your string for the title. You will probably have to adjust this number to find the right length before it gets truncated.
if count(myHeaderBarString) > 40 {
myHeaderBarString = myHeaderBarString.substringToIndex(40)
myHeaderBarString.append("...")
}
For a subtitle, you should insert a new view with a UILabel inside it.
You can set custom title view to the navigation bar like this way.
//To hide default back button
self.navigationItem.hidesBackButton=YES;
//Title View for Navigation
UIView *navTitle1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
[navTitle1 setBackgroundColor:[UIColor lightGrayColor]];
//Left View button and separator
UIButton *crossBarButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
[crossBarButton setTitle:#"X" forState:UIControlStateNormal];
//Center view with two buttons and seprator for them
UILabel *topTitle = [[UILabel alloc] initWithFrame: CGRectMake(50,0, self.view.bounds.size.width-100, 22)];
topTitle.text = #"text";
topTitle.font = [UIFont systemFontOfSize:25];
topTitle.lineBreakMode = NSLineBreakByCharWrapping;
UILabel *subTitle = [[UILabel alloc] initWithFrame: CGRectMake(50,20, self.view.bounds.size.width-100, 18)];
subTitle.text = #"subtext";
subTitle.font = [UIFont systemFontOfSize:18];
subTitle.lineBreakMode = NSLineBreakByCharWrapping;
//Right button with seprator before that
UIButton *uploadBarButton = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width-50, 0, 44, 44)];
[uploadBarButton setTitle:#"U" forState:UIControlStateNormal];
[navTitle1 addSubview:crossBarButton];
[navTitle1 addSubview:topTitle];
[navTitle1 addSubview:subTitle];
[navTitle1 addSubview:uploadBarButton];
self.navigationItem.titleView = navTitle1;
You can refer THIS for more detail.
Use BarButtonItem.customView, it works.
let button = UIButton(frame: CGRectMake(0,0,200,40))
button.titleLabel?.adjustsFontSizeToFitWidth = true
button.setTitle("gogogogogogogogoogogogogogogogogogoogo", forState: .Normal)
button.setTitleColor(UIColor.blackColor(), forState: .Normal)
button.addTarget(self, action: "showMessage:", forControlEvents: .TouchUpInside)
let rightBarButtonItem = UIBarButtonItem()
rightBarButtonItem.customView = button
self.navigationItem.rightBarButtonItem = rightBarButtonItem
I want to make a TableView Cell with a Label and Textfield in it, but the Textfield doesn't appear.
Here is the init of the textfield, it's in a cell-class:
self.tfTitle = [[UITextField alloc]initWithFrame:CGRectMake(20, 70, 100, 20)];
self.tfTitle.borderStyle = UITextBorderStyleRoundedRect;
self.tfTitle.placeholder = #"Titel/Ort";
self.tfTitle.textAlignment = NSTextAlignmentRight;
self.tfTitle.adjustsFontSizeToFitWidth = YES;
[self addSubview:self.tfTitle];
try [self.contentView addSubview:self.tfTitle];
Also change your frame CGRectMake(20, 70, 100, 20) to CGRectMake(20, 10, 100, 20)
70 is too far from the top so that you cannot see it.
Since iOS7, UITableViewCell hierarchy has changed, consider adding the UITextField to self.contentView
[self.contentView addSubview:self.tfTitle];
It might not be the problem, but still, if you make it appear on self, you won't be able to select the textfield.
ALso make sure the frame you give it is in the cell (put some background color to the contentView of the self and to your textfield)
I would like to position the text in a textfield more specifically.
My current code:
UITextField * textFieldIndustry = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 465, 68)];
textFieldIndustry.font = [UIFont systemFontOfSize:17.0];
textFieldIndustry.placeholder = #"Tap to..";
textFieldIndustry.textAlignment = NSTextAlignmentCenter;
This code:
textFieldIndustry.textAlignment = NSTextAlignmentCenter;
Centers the text but not exactly where I want it. How do I position using for example X & Y?
You cannot set position of text implicitly in UITextField without subclassing it.
You can use UITextView instead, and should use the same approach with it:
UITextView *textView;
UIEdgeInsets insets = UIEdgeInsetsMake(top, left, bottom, right);
[textView setContentInset:insets];
You can use this approach also with subclassing the UITextField.
Its very simple you just need to do textfield padding on which direction you want by using "UIEdgeInsets"
Ex: UIEdgeInsets insets = UIEdgeInsetsMake(0, 10, 0, 0);
[textView setContentInset:insets];
It will show you the textfield after 10 pxls from .....like this you can give.
happy coding...
UITextField *textFieldNote = [[UITextField alloc] initWithFrame:CGRectMake(5, 5, 310, 110)];
textFieldNote.placeholder = #"Ghi chĂș";
textFieldNote.textAlignment = NSTextAlignmentLeft;
textFieldNote.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;
For the x, you can create a padding by adding a view to the left side of the textfield.
let padding = UIView(frame: CGRect(x: 0, y: 0, width: 15, height: phoneNumberTextfield.frame.height))
textFieldIndustry.leftView = paddingView
textFieldIndustry.leftViewMode = .always
This ensures that the cursor is not at the edg
You can also utilize the leftView portion of UITextView to do what you are asking. This is great for adding a prefix to a textField without placing a label in front of it. I'm not sure exactly how you wish to use it but it may be a good solution. Basically you just create a UILabel (or any view) with the width and height you like then add that as the leftView of the textField.