Breakline in UIButton title

谁说胖子不能爱 提交于 2020-01-04 06:30:17

问题


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


回答1:


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.




回答2:


It's worked on iOS9.

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


来源:https://stackoverflow.com/questions/2265940/breakline-in-uibutton-title

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!