我正在使用Xamarin.iOS,我想让UILabel和UIImage以相同的边距与容器视图对齐。见下图
目前,图像和文字的高度相同,但由于标签的填充,顶部和底部的边距更大。顺便说一句,我已经使用过SizeToFit()。 这是我的代码:
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);
现在看起来如何:
我的问题是如何删除所有填充(包括顶部和底部),如果仍然可以删除左右的小填充
我可以理解Objective-C代码,所以如果有使用它的引用,请分享
谢谢!
更新<!/强> 这是我的CustomLabel,结果相同。
答案 0 :(得分:0)
试试这个link
为所有插件设置0。所以你不会得到额外的填充
<强> 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