UIButton Padding - ios

Is there an equivalent padding for UIButton, just like in CSS?.
I seem to have a problem with small buttons so I want to expand the button size for it to be easily clickable.
I made a work around code to resize a UIButton and have the contents remain as they are supposed to be:
- (void)adjustButtonView:(UIButton *)button toSize:(CGSize)size
{
CGRect previousFrame = button.frame;
CGRect newFrame = button.frame;
newFrame.size = size;
CGFloat adjustX = (size.width - previousFrame.size.width)/2;
CGFloat adjustY = (size.height - previousFrame.size.height)/2;
newFrame.origin.x = previousFrame.origin.x - adjustX;
newFrame.origin.y = previousFrame.origin.y - adjustY;
button.frame = newFrame;
UIEdgeInsets edgeInsets = UIEdgeInsetsMake(adjustY, adjustX, adjustY, adjustX);
button.contentEdgeInsets = edgeInsets;
}
I'm just wondering if there is an easier way to expand the UIButtons.

I don't think there's an easier way than what you did. But we can make your code better by moving the method to a UIButton category.
UIButton+Resize.h
#interface UIButton (Resize)
- (void)adjustToSize:(CGSize)size;
#end
UIButton+Resize.m
#implementation UIButton (Resize)
- (void)adjustToSize:(CGSize)size
{
CGRect previousFrame = self.frame;
CGRect newFrame = self.frame;
newFrame.size = size;
CGFloat adjustX = (size.width - previousFrame.size.width)/2;
CGFloat adjustY = (size.height - previousFrame.size.height)/2;
newFrame.origin.x = previousFrame.origin.x - adjustX;
newFrame.origin.y = previousFrame.origin.y - adjustY;
self.frame = newFrame;
UIEdgeInsets edgeInsets = UIEdgeInsetsMake(adjustY, adjustX, adjustY, adjustX);
self.contentEdgeInsets = edgeInsets;
}
#end

Well actually, there is one stupidly simplistic way to expand the width of UIButtons if you don't want to mess with subclassing.... try this:
[b setTitle:[NSString stringWithFormat:#" %# ", #"Title"] forState:UIControlStateNormal];

No, there's nothing like CSS or style sheets for iOS, if you want to resize a button you'll have to do it manually setting the frame.
In order to automatize this task, you can create a button and subclass it, or use categories.

Related

Autolayout UITextView inside UIScrollView size not correct

So I know there are already a lot of questions about this subject, but I haven't found one that has solved my problem (or perhaps I don't understand the answer).
Alright, so I have set up a scrollview that has an UIView in it, containing an image view at the top (a gradient view which you can ignore), and a textview under it. The textview has to expand with whatever is put inside of it without scrolling (hence why it's in the scrollView).
I have in code:
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
[self setupGradientView];
[self resize];
}
- (void) resize {
CGFloat maxWidth = [[UIScreen mainScreen] bounds].size.width;
CGRect newFrame;
// reset width for scrollview
newFrame = imageView.frame;
newFrame.size = CGSizeMake(maxWidth, imageView.frame.size.height);
imageView.frame = newFrame;
gradientView.frame = newFrame;
newFrame = textView.frame;
newFrame.size = CGSizeMake(maxWidth, textView.frame.size.height);
//textView.frame = newFrame;
newFrame = dummyView.frame;
newFrame.size = CGSizeMake(maxWidth, dummyView.frame.size.height);
dummyView.frame = newFrame;
// resize height
CGFloat fixedWidth = textView.frame.size.width;
CGSize newSize = [textView sizeThatFits:CGSizeMake(fixedWidth, CGFLOAT_MAX)];
newFrame = textView.frame;
newFrame.size = CGSizeMake(fixedWidth, newSize.height);
//textView.frame = newFrame;
self.textViewHeightConstraint.constant = newSize.height;
CGFloat dummyViewHeight = textView.frame.origin.y + textView.frame.size.height;
dummyView.frame = CGRectMake(dummyView.frame.origin.x, dummyView.frame.origin.y, dummyView.frame.size.width, dummyViewHeight);
[scrollView setContentSize:dummyView.frame.size];
}
Unfortunately, the content size seems to be off by what I think may be 16pts, resulting in a view that looks like this with the text cut off (don't mind the unicode mess in the text):
Here's a picture of what the view looks like in IB: https://i.imgur.com/hxzzUg2.png
Here's what the constraint hierarchy looks like: https://i.imgur.com/rUepwa2.png
Any help would be greatly appreciated!
EDIT: If I comment out the entire code in the resize function, I get the same result. It would appear that auto-layout is resizing all of this after the function is called...but I thought auto-layout was completed when viewDidLayoutSubviews was called?
Edit 2: I have tried and found that adjusting the frame of the textView even n viewDidAppear has no effect. (I can edit things such as the background color)
What I ended up having to do was setting equal width and equal height to the view (not the scrollview), and then manually changing the height constraint in viewDidLayoutSubviews, along with changing the frame of the textView and the content size of the scrollView.

How to increase the tap area of UIButton

I am using the following code to resize the buttons in my appplication
+(void) processButton:(UIButton*) button buttonType:(NSString*)buttonType {
ENDebug(#"Process button %#" ,[button restorationIdentifier]);
[button setTranslatesAutoresizingMaskIntoConstraints:true];
CGRect rect = [button frame];
float startx = rect.origin.x;
float midx = startx +rect.size.width/2;
float starty = rect.origin.y;
float midy = starty +rect.size.height/2;
if ([buttonType isEqualToString:BUTTON_IPAD_SMALL]||[buttonType isEqualToString:BUTTON_IPHONE_SMALL]) {
rect.size.width = BUTTON_WIDTH_SMALL;
rect.size.height = BUTTON_HEIGHT_SMALL;
} else {
rect.size.width = BUTTON_WIDTH_LARGE;
rect.size.height = BUTTON_HEIGHT_LARGE;
}
rect.origin.x = midx - rect.size.width/2;
rect.origin.y = midy-rect.size.height/2;
button.titleLabel.font = GillSansBold(BUTTON_TITLE_SIZE);
[button setFrame:rect];
}
Using this I am able to resize the code.. but am not able to increase the tap size of the buttons.. Looks like only the view has expanded without the button becoming clickable in entirety.
Check the edge insets of the button. An inset is a margin around the drawing rectangle. Or you cant just try:
button.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
after setting the button's frame.

how to resize UIScrollView to fit content of children UITextViews

I have a UIScrollView with a number of children UITextViews arranged vertically. I want the UIScrollView to resize to fit content. So my TextViews I do
- (void)textViewDidChange:(UITextView *)textView
{
CGFloat fixedWidth = textView.frame.size.width;
CGSize newSize = [textView sizeThatFits:CGSizeMake(fixedWidth, MAXFLOAT)];
CGRect newFrame = textView.frame;
newFrame.size = CGSizeMake(fmaxf(newSize.width, fixedWidth), newSize.height);
textView.frame = newFrame;
[textView setNeedsLayout];
}
And for the scrollview I do
-(void)resizeScrollViewToFitContent
{
CGRect contentRect = CGRectZero;
for (UIView *view in self.scrollView.subviews) {
contentRect = CGRectUnion(contentRect, view.frame);
}
self.scrollView.contentSize = contentRect.size;
}
I call [self resizeScrollViewToFitContent] inside viewDidAppear. Any ideas why this setup is not working?
I am not sure how is the arrangement of your textViews inside the scrollView. I assume that they are arranged vertically right next to each other. I have created a sample project with a scrollView and 3 textViews and it works well with the following code:-
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
CGFloat height = self.textView1.frame.size.height +self.textView2.frame.size.height +self.textView3.frame.size.height ;
CGSize size = CGSizeMake(self.scrollView.frame.size.width, height);
self.scrollView.contentSize = size;
}
From my own project, the content Height of the scrollview is the combination of the height for all 3 textviews. It might be different in your project.
You shouldn't be doing the union of the contentFrame and view.frame. Try the following:
-(void)resizeScrollViewToFitContent
{
UIScrollView* scrollView;
CGSize contentSize = CGSizeZero;
for (UIView* subview in scrollView.subviews)
{
CGFloat subviewRight = CGRectGetMaxX(subview.frame);
if (subviewRight > contentSize.width)
{
contentSize.width = subviewRight;
}
CGFloat subviewBottom = CGRectGetMaxY(subview.frame);
if (subviewBottom > contentSize.height)
{
contentSize.height = subviewBottom;
}
}
scrollView.contentSize = contentSize;
}
One small thing worth noting: a UIScrollView's scroll indicators are subviews of any UIScrollView. This has the potential to throw off the above code's accuracy, but I've found that it always works except in some very particular cases.
If you know how many subviews are in your scrollview per row then you'll want to do a little math to find the correct height.
For example, you might have a uiview with a uiimageview and uilabel, so you're subview count will be 3 times what it should be.
try
// csv = contentscrollview
NSLog(#"%d", [[csv subviews] count] / 3);

contentOffset Changes when Setting UITextView Frame in textViewDidChange

I am trying to set the height of a UITextView dynamically in textViewDidChange. I get the frame to adjust to the proper height, but when the height is greater than a threshold that I have set, then the textView should just scroll with a new line. However, the contentOffset gets messed up and it doesn't scroll down to fully include the new line.
- (void)viewDidLoad
{
[super viewDidLoad];
_textView.frame = CGRectMake(60, 100, 200, _textView.contentSize.height);
_textView.scrollEnabled = NO;
}
- (void)textViewDidChange:(UITextView *)textView
{
NSLog(#"Offset: %f", _textView.contentOffset.y);
CGFloat height = _textView.contentSize.height;
_textView.scrollEnabled = NO;
if (_textView.contentSize.height >= 120) {
height = 120;
_textView.scrollEnabled = YES;
_textView.contentOffset = CGPointMake(0, _textView.contentSize.height - _textView.frame.size.height);
}
CGRect frame = _textView.frame;
frame.size.height = height;
_textView.frame = frame;
}
I suppose an easier way of explaining what is happening is this. On a completely blank view controller add a UITextView in Storyboard. Then add only these three lines into textViewDidChange:
CGRect frame = _textView.frame;
frame.size.height = _textView.contentSize.height;
_textView.frame = frame;
Every other time the textView's height increases, the text is not vertically centered. If you scroll it around though and then let go it will return to the correct position. The frame is always the correct size. It is the contentOffset that is not quite right.
I wonder if you could post some code as to what you're actually doing?
I've just tried this on 5.0 and this works.
-(void) textViewDidChange:(UITextView *)textView {
int maxHeight = 30;
CGFloat height = [textView.text sizeWithFont:textView.font constrainedToSize:CGSizeMake(textView.frame.size.width, 99999) lineBreakMode:NSLineBreakByWordWrapping].height;
CGRect rect = textView.frame;
rect.size.height = MIN(height, maxHeight);
[textView setFrame:rect];
}

UIButton with title under the imageview

I want to creat an UIButton programmatically with the title under the imageView.
Size of the button : 170 * 120
Size of the imge : 50 * 50
Size of the title : depend of the text.
I know I'have to use but I don't know how :
[_button setTitleEdgeInsets:UIEdgeInsetsMake(0.f, 0.f, 0.f, 0.f)];
[_button setImageEdgeInsets:UIEdgeInsetsMake(0.f, 0.f, 0.f, 0.f)];
I think I should calculate the size of the title and then Use the EdgeInsets.
Thank you.
Hope this can help you.
#interface UIButton (UIButtonExt)
- (void)centerImageAndTitle:(float)space;
- (void)centerImageAndTitle;
#end
#implementation UIButton (UIButtonExt)
- (void)centerImageAndTitle:(float)spacing
{
// get the size of the elements here for readability
CGSize imageSize = self.imageView.frame.size;
CGSize titleSize = self.titleLabel.frame.size;
// get the height they will take up as a unit
CGFloat totalHeight = (imageSize.height + titleSize.height + spacing);
// raise the image and push it right to center it
self.imageEdgeInsets = UIEdgeInsetsMake(- (totalHeight - imageSize.height), 0.0, 0.0, - titleSize.width);
// lower the text and push it left to center it
self.titleEdgeInsets = UIEdgeInsetsMake(0.0, - imageSize.width, - (totalHeight - titleSize.height),0.0);
}
- (void)centerImageAndTitle
{
const int DEFAULT_SPACING = 6.0f;
[self centerImageAndTitle:DEFAULT_SPACING];
}
#end
The ultimate and stable solution is to use frame, not EdgeInset solution like this:
#interface UIButton (UIButtonExt)
(void)centerImageAndTitleEx;
#end
#implementation UIButton (UIButtonExt)
(void)centerImageAndTitleEx
{
CGRect frame = self.imageView.frame;
frame = CGRectMake(truncf((self.bounds.size.width - frame.size.width) / 2), 10.0f, frame.size.width, frame.size.height);
self.imageView.frame = frame;
frame = self.titleLabel.frame;
frame = CGRectMake(truncf((self.bounds.size.width - frame.size.width) / 2), self.bounds.size.height - frame.size.height - 5.0, frame.size.width, frame.size.height);
self.titleLabel.frame = frame;
}
#end

Resources