How to achieve UIButton / UILabel 'padding' in iPhone app

前端 未结 10 2270
挽巷
挽巷 2021-01-31 13:40

I\'ve got various views in my iPhone application that require padding e.g a custom UIButton with text aligned left, and a UILabel with a background color.

This may be a

10条回答
  •  囚心锁ツ
    2021-01-31 13:48

    What about creating a custom class that extends UIButton and overriding this:

    - (CGRect)titleRectForContentRect:(CGRect)contentRect
    {
        return UIEdgeInsetsInsetRect(contentRect, UIEdgeInsetsMake(topPadding, rightPadding, bottomPadding, leftPadding));
    }
    

    In the case of UILabel just override:

    - (CGRect)textRectForBounds:(CGRect)bounds 
         limitedToNumberOfLines:(NSInteger)numberOfLines;
    

提交回复
热议问题