iOS设置UIButton文字对齐方式和边距

  • 按钮垂直对齐方式
@property(nonatomic) UIControlContentVerticalAlignment contentVerticalAlignment;

typedef NS_ENUM(NSInteger, UIControlContentVerticalAlignment) {
    UIControlContentVerticalAlignmentCenter        = 0,
    UIControlContentVerticalAlignmentTop           = 1,
    UIControlContentVerticalAlignmentBottom        = 2,
    UIControlContentVerticalAlignmentFill          = 3,
};

  • 按钮水平对齐方式
@property(nonatomic) UIControlContentHorizontalAlignment contentHorizontalAlignment;

typedef NS_ENUM(NSInteger, UIControlContentHorizontalAlignment) {
    UIControlContentHorizontalAlignmentCenter = 0,
    UIControlContentHorizontalAlignmentLeft   = 1,
    UIControlContentHorizontalAlignmentRight  = 2,
    UIControlContentHorizontalAlignmentFill   = 3,
    UIControlContentHorizontalAlignmentLeading  API_AVAILABLE(ios(11.0), tvos(11.0)) = 4,
    UIControlContentHorizontalAlignmentTrailing API_AVAILABLE(ios(11.0), tvos(11.0)) = 5,
};

 设置按钮文字左对齐

btn.contentHorizontalAlignment = UIControlContentHorizonAlignmentLeft;

设置按钮文字与边框距离

btn.contentEdgeInsets = UIEdgeInsetsMake(0,10, 0, 0);

 

    原文作者:培根芝士
    原文地址: https://blog.csdn.net/watson2017/article/details/103504672
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞