0

I'm using Xamarin.iOS and I want to have a UILabel and UIImage aligned with same margins to the container view. See image below

Template

Right now, the image and text has the same height but due to the padding of the label, the top and bottom have bigger margins. Btw, I already used SizeToFit(). Here's my code:

nfloat gap = 10f;
UIView containerView = new UIView ();
UILabel textView = new UILabel ();
textView.BackgroundColor = UIColor.Red;
textView.Text = "HELLO!";
textView.Font = UIFont.FromName ("Arial-BoldMT", 40f);
textView.ContentMode = UIViewContentMode.Bottom;
textView.SizeToFit ();
textView.Frame = new CGRect (gap, gap, textView.Frame.Width, textView.Frame.Height);
containerView.AddSubview (textView);

UIImageView imgView = new UIImageView (UIImage.FromBundle ("Sample-icon"));
imgView.Frame = new CGRect (textView.Frame.X + textView.Frame.Width + gap,
textView.Frame.Y, textView.Frame.Height, textView.Frame.Height);
imgView.ContentMode = UIViewContentMode.ScaleToFill;
containerView.AddSubview (imgView);

containerView.Frame = new CGRect(10, 50, gap + textView.Frame.Width + gap + imgView.Frame.Width + gap, gap + textView.Frame.Height + gap);
containerView.BackgroundColor = UIColor.Yellow;
View.AddSubview (containerView);

Here how it looks like now:

Current view

My question is how can I remove all paddings (including top and bottom) and if still possible I can remove the small paddings of left and right

I can understand Objective-C codes so if there are references using it, kindly share

Thanks!

UPDATE! Here's my CustomLabel with same result.

enter image description here

marhs08
  • 57
  • 1
  • 1
  • 6

1 Answers1

0

Try this link

Set 0 for all in insets. So you won't get extra padding

CustomLabel.h

 #import <UIKit/UIKit.h>

 @interface CustomLabel : UILabel

 @end

CustomLabel.m

#import "CustomLabel.h"
#import <QuartzCore/QuartzCore.h>

@implementation CustomLabel

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

// for inset
-(void) drawTextInRect:(CGRect)rect
{
    UIEdgeInsets insets = {0, 0, 0, 0};

    [super drawTextInRect: UIEdgeInsetsInsetRect(rect, insets)];    
}

YourViewController.h

#import <UIKit/UIKit.h>
#import "CustomLabel.h"

@interface YourViewController : UIViewController {
    CustomLabel *myLabel;
}

@property (strong, nonatomic) IBOutlet CustomLabel *textView;

@end
Community
  • 1
  • 1
Sujay
  • 2,510
  • 2
  • 27
  • 47
  • Hi @Sujay, I tried that. I already tried subclassing a UILabel and it doesn't work. – marhs08 Jun 27 '15 at 06:56
  • @marsh08, have you used custom class name for your text field? – Sujay Jun 27 '15 at 06:59
  • Do you mena somehting like this: textView = new CustomButtonLabel();.. ? Yes – marhs08 Jun 27 '15 at 07:35
  • you have used following code to create label `UILabel textView = new UILabel();` instead of using `UILabel` use your custom class name that you have created for subclassing the `UILabel` try `CustomLabel textView = new CustomLabel();` – Sujay Jun 27 '15 at 07:52
  • sorry, the one above is not the one I'm really using but just the same implementation. – marhs08 Jun 27 '15 at 08:04
  • I updated my question to display the CustomLabel and as you can see, no effect. Thanks – marhs08 Jun 27 '15 at 10:34
  • didn't update the upper code. but it is updated in my code – marhs08 Jun 27 '15 at 11:08