UIButton标题中的Break Line

时间:2010-02-15 12:41:40

标签: iphone nsstring uibutton

在以下按钮中显示的是一个地址。我的问题是地址很长,如何在按钮标题中的两个变量之间得到一个断行线?

NSString *sitz = map.kordinate.herkunft;
NSString *strasse = map.kordinate.strasse;
NSString *strasseMitKomma = [strasse stringByAppendingString:@","];
NSString *fertigesSitzFeld = [strasseMitKomma stringByAppendingString:sitz];
UIButton *firmensitz = [UIButton buttonWithType:UIButtonTypeRoundedRect];

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

之间

谢谢你的帮助

2 个答案:

答案 0 :(得分: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];

顺便说一下,你不需要那么多的NSString变量。您可以将新字符串的结果放在您已经使用的NSString变量中 看看我的答案的第一行中的方法stringWithFormat。请阅读NSString documentation了解更多详情。

答案 1 :(得分:0)

它适用于iOS9。

[_butNoReceipt.titleLabel setLineBreakMode:NSLineBreakByCharWrapping];
[_butNoReceipt.titleLabel setTextAlignment:NSTextAlignmentCenter];
[_butNoReceipt setTitle:btnTest forState:UIControlStateNormal];