Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 668
Marco Avatar asked Jul 03 '26 11:07

Marco


1 Answers

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.

like image 185
Guillaume Avatar answered Jul 04 '26 23:07

Guillaume