在Xamarin IOS中显示带文本和图像的按钮

7kjnsjlb  于 2022-12-07  发布在  iOS
关注(0)|答案(1)|浏览(148)

I want to design a custom button in Xamarin IOS to display both Text and Image. Please find the below image.

You can find the Text and down arrow symbol in the above image. the down arrow symbol should always display after the text. The button text is dynamic.
Can any one suggest me to solve this.

uoifb46i

uoifb46i1#

您想要使用UIButtonTitleEdgeInsetsImageEdgeInsets属性来控制文字和影像的内缩开始位置:

button.TitleEdgeInsets = new UIEdgeInsets(0, -button.ImageView.Frame.Size.Width, 0, button.ImageView.Frame.Size.Width);
button.ImageEdgeInsets = new UIEdgeInsets(0, button.TitleLabel.Frame.Size.Width, 0, -button.TitleLabel.Frame.Size.Width);

完整示例:

button = new UIButton(UIButtonType.System);
    button.Frame = new CGRect(150, 20, 200, 50);
    button.Layer.CornerRadius = 5;
    button.BackgroundColor = UIColor.Black;
    button.SetTitleColor(UIColor.White, UIControlState.Normal);
    button.SetImage(UIImage.FromFile("ratingstar.png"), UIControlState.Normal);
    button.SetTitle("StackOverflow", UIControlState.Normal);
    button.TitleEdgeInsets = new UIEdgeInsets(0, -button.ImageView.Frame.Size.Width, 0, button.ImageView.Frame.Size.Width);
    button.ImageEdgeInsets = new UIEdgeInsets(0, button.TitleLabel.Frame.Size.Width, 0, -button.TitleLabel.Frame.Size.Width);

相关问题