简体   繁体   中英

How to add two lines of strings into a same UILabel

I want to insert 2 lines for a UILabel like this

$2500.50

per ride

I need to add these two lines into a same UILabel programmatically. How can I do that. Please anybody help me.

Thanks

This can be achieved like so:

UILabel *myLabel = [[UILabel alloc] init];

// allows the UILabel to wrap text across multiple lines
myLabel.lineBreakMode = UILineBreakModeWordWrap;

// allows the UILabel to display an unlimited number of lines
myLabel.numberOfLines = 0; 

You can add line breaks to the text using new-line characters (“\\n”), as mentioned in the comments above.

You only need to set some properties like this

UILabel *myLabel = [[UILabel alloc] init];
[myLabel setFrame:CGRectMake(20, 20, 300, 200)];
myLabel.lineBreakMode = NSLineBreakByWordWrapping;
myLabel.numberOfLines = 2;

[myLabel setText:[NSString stringWithFormat:@"%@\n%@", @"$2500.50", @"per ride"]];

[self.view addSubview:myLabel];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM