开发者

Breakline in UIButton title

in the following button is an address showed. My problem is the addresses are very long and how can i get between the two variables in the title of the button an breakline开发者_JAVA百科??

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

the breakline must be between strasseMitKomma and sitz??

thank you for helping


You must configure the UILabel inside the UIButton to support multiple line.
You can do it like this:

 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];

By the way, you don't need so many NSString variables. You could have put the result of the new string in the NSString variable you were already using.
Have a look at the method stringWithFormat in the first line of my answer. Read the NSString documentation for more detail.


It's worked on iOS9.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜