UIButton title alignment and multiline support - iphone

How do I set the title of a UIButton to be left-aligned, and how can I show multiple lines of text in a UIButton?

Here's the code to do the UIButton alignment in code too: -
[myButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];

To set the alignment of a UIButton, look in the Attributes Inspector in Interface Builder for the button. There's a section called Alignment. Set it to left. Or right, or whatever you want.
To do it in code, use the contentHorizontalAlignment property of UIControl. You can set it to any of these properties:
UIControlContentHorizontalAlignmentCenter
UIControlContentHorizontalAlignmentLeft
UIControlContentHorizontalAlignmentRight
UIControlContentHorizontalAlignmentFill
[myButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
None of these options look particularly good (as you can see above), and you might have might have more luck using the contentEdgeInsets property of UIButton to reposition the content.
To set a multiline title on a UIButton, check this forum post which describes changing the button label.
Or you can use this code to quickly make a 2 line button:
myButton.titleLabel.textAlignment = UITextAlignmentCenter;
myButton.titleLabel.lineBreakMode = UILineBreakModeCharacterWrap;
[myButton setTitle:#"Siegfried\nRoy" forState:UIControlStateNormal];
Raju, don't forget to mark questions as accepted.

Actually, you can set the lineBreakMode to UILineBreakModeWordWrap or UILineBreakModeCharacterWrap. This breaks the text into multiple lines if necessary.

Try this
myButton.titleLabel.textAlignment = UITextAlignmentLeft;
myButton.titleLabel.lineBreakMode = UILineBreakModeCharacterWrap;
myButton.titleLabel.numberOfLines = 0;

To add a Paragraph effect you can change the size of title.

Related

iPhone: How to remove glow (light) from UIBarButtonItem when pressed?

I'm trying to remove the glow from a UIBarButton item so that my text appears to be a label instead of a button. I've seen various posts talking about how to do this through interface builder or by setting a boolean variable "showsTouchWhenHighlighted", but neither of these options are available to me it appears. I've tried setting the showsTouchWhenHighlighted in the .m viewDidLoad where I change the font and font-size but the UIBarButtonItem doesn't appear to have that property. I also only have the options in the following image to change in InterfaceBuilder.
There is a way to do this (a bit of a hack but it works). Just drag a UIButton into your toolbar (instead of a UIBarButtonItem). Then a UIBarButtonItem will be automatically be created for you as a superview for your UIButton. Then you just set it like this:
UIBarButtonItem
Style: Plain
Title: (empty)
UIButton
Type: Custom
Title: (your actual label title here)
Text Color: White
Shows Touch On Highlight: (Unchecked)
Here is a screenshot to use as reference:
Note: Just remember that from now on any updates on the text must be made on the UIButton
try this:
`
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(140 , 0, 50, 250)];
[label setBackgroundColor:[UIColor clearColor]];
label.text = #"TEXT";
UIView *view = (UIView *) label;
[self.barItem setCustomView:view];
`
note: self.barItem is a UIBarButtonItem added from the object library and placed between two flexible spaces.
another way is to remove the [self.barItem setCustom:view] line and change the parameters of the label (width) so that it fills the entire toolbar and set the alignment to middle and the font by yourself in code,

How to set UITextField height?

I am using a UITextField. I want to increase its height but I have not found any property to do this. How can I achieve this?
You can not change the height of the rounded rect border style.
To set the height, just choose any border style other than rounded border in Xcode:
I finally found the fix for this!
As we have found, IB doesn't allow us to change the height of the rounded corner border style. So change it to any of the other styles and set the desired height. In the code change the border style back.
textField.borderStyle = UITextBorderStyleRoundedRect;
CGRect frameRect = textField.frame;
frameRect.size.height = 100; // <-- Specify the height you want here.
textField.frame = frameRect;
If you are using Auto Layout then you can do it on the Story board.
Add a height constraint to the text field, then change the height constraint constant to any desired value. Steps are shown below:
Step 1: Create a height constraint for the text field
Step 2: Select Height Constraint
Step 3: Change Height Constraint's constant value
1.) Change the border Style in the InterfaceBuilder.
2.) After that you're able to change the size.
3.) Create an IBOutlet to your TextField and enter the following code to your viewDidLoad() to change the BorderStyle back.
textField.borderStyle = UITextBorderStyleRoundedRect;
Swift 3:
textField.borderStyle = UITextBorderStyle.roundedRect
Choose the border style as not rounded
Set your height
in your viewWillAppear set the corners as round
yourUITextField.borderStyle = UITextBorderStyleRoundedRect;
Enjoy your round and tall UITextField
Follow these two simple steps and get increase height of your UItextField.
Step 1: right click on XIB file and open it as in "Source Code".
Step 2: Find the same UITextfield source and set the frame as you want.
You can use these steps to change frame of any apple controls.
An update for iOS 6 : using auto-layout, even though you still can't set the UITextField's height from the Size Inspector in the Interface Builder (as of Xcode 4.5 DP4 at least), it is now possible to set a Height constraint on it, which you can edit from the Interface Builder.
Also, if you're setting the frame's height by code, auto-layout may reset it depending on the other constraints your view may have.
I know this an old question but I just wanted to add if you would like to easily change the height of a UITextField from inside IB then simply change that UITextfield's border type to anything other than the default rounded corner type. Then you can stretch or change height attributes easily from inside the editor.
swift3
#IBDesignable
class BigTextField: UITextField {
override func didMoveToWindow() {
super.didMoveToWindow()
if window != nil {
borderStyle = .roundedRect
}
}
}
Interface Builder
Replace UITextField with BigTextField.
Change the Border Style
to none.
My pathetic contribution to this dumb problem. In IB set the style to none so you can set the height, then in IB set the class to be a subclass of UITextField that forces the style to be rounded rect.
#interface JLTForcedRoundedRectTextField : UITextField
#end
#implementation JLTForcedRoundedRectTextField
- (void)awakeFromNib
{
self.borderStyle = UITextBorderStyleRoundedRect;
}
#end
It kept me from having to hack the XIB file or writing style code into my view controller.
A UITextField's height is not adjustable in Attributes Inspector only
when it has the default rounded corners border style, but adding a
height constraint (plus any other constraints which are required to
satisfy the autolayout system - often by simply using Add Missing
Constraints) to it and adjusting the constraint will adjust the
textfield's height. If you don't want constraints, the constraints can
be removed (Clear Constraints) and the textfield will remain at the
adjusted height.
Works like a charm.
In Swift 3 use:
yourTextField.frame.size.height = 30
try this
UITextField *field = [[UITextField alloc] initWithFrame:CGRectMake(20, 80, 280, 120)];
UITextField *txt = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
[txt setText:#"Ananth"];
[self.view addSubview:txt];
Last two arguments are width and height, You can set as you wish...
You can use frame property of textfield to change frame
Like-Textfield.frame=CGRECTMake(x axis,y axis,width,height)
This is quite simple.
yourtextfield.frame = CGRectMake (yourXAxis, yourYAxis, yourWidth, yourHeight);
Declare your textfield as a gloabal property & change its frame where ever you want to do it in your code.
Happy Coding!
If you're creating a lot of UITextFields it can be quicker to subclass UITextViews and override the setFrame method with
-(void)setFrame:(CGRect)frame{
[self setBorderStyle:UITextBorderStyleRoundedRect];
[super setFrame:frame];
[self setBorderStyle:UITextBorderStyleNone];
}
This way you can just call
[customTextField setFrame:<rect>];
I was having the same issue. tried some of the solutions here but rather than doing all this mumbo-jumbo. I found just setting height constraint is enough.

iOS: UIButton titleLabel -- does it do anything at all?

I want to make a UIButton with type UIButtonTypeCustom (for the shape). I want to assign the title using button.titleLabel because I need to specify the font. The following code looks like it should work, but doesn't -- no label shows up, period.
UIImage *editButtonImage = [UIImage imageNamed: #"editButton.png"];
float width = editButtonImage.size.width;
float height = editButtonImage.size.height;
UIButton *editButton = [UIButton buttonWithType: UIButtonTypeCustom];
editButton.frame = CGRectMake(0, 0, width, height);
[editButton setBackgroundImage: editButtonImage forState: UIControlStateNormal];
editButton.adjustsImageWhenHighlighted = YES;
editButton.titleLabel.text = #"Edit";
editButton.titleLabel.textColor = [UIColor whiteColor];
editButton.titleLabel.textAlignment = UITextAlignmentCenter;
editButton.titleLabel.font = [UIFont fontWithName: #"Helvetica" size: 14];
[self.view addSubview: editButton];
Everyone always says to use setTitle:forState: but that gives you a font I don't like. The titleLabel method is NOT deprecated -- it should work.
I have run into this several times before and always just worked around it, but I'd really like to figure it out. Any ideas?
Setting the titleLabel's text property like that has no effect. Instead, call -setTitle:forState: on the button:
[editButton setTitle:#"Edit" forState:UIControlStateNormal]
The reason for this is because the button can have different titles for different states (e.g., UIControlStateDisabled, UIControlStateHighlighted). Setting a property for the UIControlStateNormal control state will apply to all the states if you don't specify the others explicitly.
Per the documentation for UIButton:
This class provides methods for setting the title, image, and other appearance properties of a button. By using these accessors, you can specify a different appearance for each button state.
You can customize label's color and shadow color based on the state. See -setTitleColor:forState and -setTitleShadowColor:forState, respectively. The rest of the properties on titleLabel, such as textAlignment and font, should work as you have them set now and should apply to all the control states.
Specifically, see the documentation for UIButton's titleLabel property: https://developer.apple.com/documentation/uikit/uibutton/1623992-titlelabel
titleLabel itself is read-only, but that doesn't mean you can't change the values of its own properties, such as font, line break mode, etc.
Here's how I worked it out using Swift 4.2.
counterButton.setTitle("Start Counter",
for:UIControl.State.normal)

How to hide a UIButton title after onclick event

When I click a button Its title should be hide. I do't want to set the title to empty string #"". So, How can i do this?
Just a better solution that I'm using now:
The accepted answer doesn't let me re-use the name if I need, so I'm using in this way:
-(void)SomeButtonPressed {
someButton.titleLabel.textColor = [UIColor clearColor];
}
I think it's better just keeping the button's label invisible.
Why don't you want to set the title to an empty string? Simply store the value in a local field and set the button's title to #"" and everything will be fine.
In your .h:
NSString *someLocalField;
in your .m:
-(void)SomeButtonPressed {
someLocalField = someButton.text;
someButton.text = #"";
}
This way, if you ever need to restore the text of the button, you can do so:
someButton.text = someLocalField
If you want to do this for a bunch of buttons, you could always use an NSDictionary and associate the string values with the buttons.
If you want it to disappear when a finger is on the button,
[button setTitle:#" " forState:UIControlStateHighlighted];
If you want it to toggle between being displayed and not,
[button setTitle:#" " forState:UIControlStateSelected];
[button setTitle:#" " forState:UIControlStateSelected|UIControlStateHighlighted];
and then set button.selected = !button.selected in the button action.
I'm using a single space instead of the empty string because sometimes the empty string has special handling which makes it equivalent to nil. If the empty string works, you can use that instead.
I haven't tried this, but if you need the title text to stay the same, but still hide from a user you might be able to set the font color to [UIColor clearColor];
using button.titleLabel.hidden = YES will not work (at least on on iOS 7).
I ended up using:
// remove the button since hiding it doesn't work
[button.titleLabel removeFromSuperview];
// put back when you're done
[button addSubview:button.titleLabel];
// Hide text
button.titleLabel.layer.opacity = 0.0f;
// Show text
button.titleLabel.layer.opacity = 1.0f;
Just hiding the title sounds a bit odd and not very Apple interface like. You can also just set the button to be hidden and then the entire thing goes away. If you do want the title to be the only thing that goes away (keep in mind that the button will still work in this state, just not have a title) then you could always assign the background color someButton.currentTitleColor = someButton.backgroundColor; should make the text vanish (you may need to set the shadow color as well).
I guess here you can find a solution, simply make the label that contains the text hidden.
button.titleLabel.hidden = YES;
You can put this in an IBAction linked in InterfaceBuilder to the Touch Inside Up event associate to your button
Have you tried button.titleLabel.hidden = YES ?

Best way to add a large chunk of text to a UIScrollView?

What is the best way to display a large chunk of text (taken from a .txt file in the app) in a UIScrollView so a user can scroll about it? Length of the text is variable.
On Interface Builder open the Attributes Inspector (if not already open - command-1) and uncheck "Editable".
Also notice there's a Scroll View section below. Make sure "Scrolling" is checked.
Hope this helps somebody (the post is a year old so I guess by now the one who posted it doesn't need this info).
I came here looking for an answer and found that all answers are bad - or flat out wrong.
The proper way to do this is using UITextView by itself. Since it is a descendant of UIScrollView, it has scrolling built-in and lots of features for adjusting formatting such as the insets etc.
If you intend to only show text, you need to explicitly disable editing. You do this by setting the "editable" property to false.
And if you want to disable the text selection mechanism, set the "selectable" property to false.
In newer versions of iOS, UITextView has added support for NSTextContainer which gives you even greater control over formatting.
One way I had working for me is to create UILabel, set text and then set content size of scrollview by it size.
Here is an example
Quote:
// alocate and initialize scroll
UIScrollView *myScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 460.0f)];
// alocate and initialize label
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 460.0f)];
// add long text to label
myLabel.text = #"Lorem ipsum... long text here";
// set line break mode to word wrap
myLabel.lineBreakMode = UILineBreakModeWordWrap;
// set number of lines to zero
myLabel.numberOfLines = 0;
// resize label
[myLabel sizeToFit];
// set scroll view size
myScroll.contentSize = CGSizeMake(myScroll.contentSize.width, myLabel.frame.size.height);
// add myLabel
[myScroll addSubview:myLabel];
// add scroll view to main view
[self.view addSubview:myScroll];
Usage of the UITextView into the UIScrollView. I could not recommend this because UITextView is the subclass of UIScrollView. Apple is also recommending the same.
Use UILabel in this case as a sub-view,
Put the UITextView into the UIScrollView.