UIButton头衔的Breakline

Mar*_*rco 2 iphone uibutton nsstring

在下面的按钮是显示的地址.我的问题是地址很长,我怎么能在按钮标题中的两个变量之间得到一个断裂线?

NSString *sitz = map.kordinate.herkunft;
NSString *strasse = map.kordinate.strasse;
NSString *strasseMitKomma = [strasse stringByAppendingString:@","];
NSString *fertigesSitzFeld = [strasseMitKomma stringByAppendingString:sitz];
UIButton *firmensitz = [UIButton buttonWithType:UIButtonTypeRoundedRect];
Run Code Online (Sandbox Code Playgroud)

断裂线必须介于strasseMitKomma和sitz之间?

谢谢你的帮忙

Gui*_*ume 9

您必须在UIButton中配置UILabel以支持多行.
你可以这样做:

 NSString *address;

    address = [NSString stringWithFormat:@"%@,\n%@", @"street name", @"city name"];   // Note the \n after the comma, that will give you a new line

    addressButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];


    [addressButton.titleLabel setLineBreakMode:UILineBreakModeWordWrap];    // Enabling multiple line in a UIButton by setting the line break mode
    [addressButton.titleLabel setTextAlignment:UITextAlignmentCenter];

    [addressButton setTitle:address forState:UIControlStateNormal];
Run Code Online (Sandbox Code Playgroud)

顺便说一下,你不需要那么多的NSString变量.您可以将新字符串的结果放在已经使用的NSString变量中.
看看我的答案的第一行中的方法stringWithFormat.阅读NSString文档以获取更多详细信息.

  • 不推荐使用UILineBreakModeCharacterWrap(iOS 6.0).请改用NSLineBreakByCharWrapping. (4认同)