Marco
Marco

Reputation: 461

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??

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

Upvotes: 2

Views: 3678

Answers (2)

Uthen
Uthen

Reputation: 797

It's worked on iOS9.

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

Upvotes: 0

Guillaume
Guillaume

Reputation: 21736

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.

Upvotes: 9

Related Questions