Vertical text in a Horizontal UIButton

前端 未结 5 1855
借酒劲吻你
借酒劲吻你 2021-01-21 10:01

I\'m using a vertical UIButton in a portrait app (Just a normal button that is of width 60 and Height 160)

I want to put the label Vetically down the button instead of a

5条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-21 10:52

    I know this is an old thread, but another way to do this is to subclass the button and specify a square size for the button label.

    @interface RotatingButton : UIButton
    
    @end
    
    
    @implementation RotatingButton
    
    - (CGRect) titleRectForContentRect:(CGRect)bounds {
        CGRect frame = [super titleRectForContentRect:bounds];
    
        frame.origin.y -= (frame.size.width - frame.size.height) / 2;
        frame.size.height = frame.size.width;
    
        return frame;
    }
    
    @end
    

    The label of any RotatingButton created in code or on Storyboard can be rotated without being truncated.

    [button.titleLabel setTransform:CGAffineTransformMakeRotation(M_PI / 2)];
    

提交回复
热议问题