2010-02-15 41 views
2

在下面的按钮中是一个地址显示。我的问题是地址是非常长的,我怎么能得到两个变量之间的按钮的标题breakline?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]; 

breakline必须介于strasseMitKomma和sitz之间?

谢谢你帮

回答

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了解更多详情。

+4

UILineBreakModeCharacterWrap已弃用(iOS 6.0)。改为使用NSLineBreakByCharWrapping。 – Arjan

0

它适用于iOS9。

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