UIButton with two lines of text in the title (numberOfLines=2)

后端 未结 6 1900
面向向阳花
面向向阳花 2021-01-30 15:42

I\'m trying to make a UIButton that has two lines of text in its titleLabel. This is the code I\'m using:

    UIButton *titleButton = [[UIButton all         


        
6条回答
  •  鱼传尺愫
    2021-01-30 16:07

    To avoid completely the need to edit code, and thus the need to subclass your view, in Xcode5 and greater you can follow Borut Tomazin suggestion:

    In Interface Builder (or storyboard) set Line Break to Word Wrap. Than you can insert multiple lines of title. Just hit Option + Return keys to make new line.

    and then, in the User Defined Runtime Attributes you can add

    Key path: titleLabel.textAlignment
    Type: Number
    Value: 1

    Note: this may be not completely "future proof" since we are translating the UITextAlignmentCenter constant into its numerical value (and that constant may change as new iOS versions are released), but it seems safe in the near future.

提交回复
热议问题