简体   繁体   English

如何在多行上显示UIButton标签?

[英]How to display a UIButton label on multiple lines?

I know there are some questions regarding this on SO, and I've tried them but it doesn't work, which is why I'm posting now. 我知道在SO上对此有一些疑问,我已经尝试过了,但是没有用,这就是为什么我现在要发布。

Basically I have an NSDate contained within the titleLabel of a UIButton. 基本上,我在UIButton的titleLabel中包含一个NSDate。 The label contain contains the Day of the Week, appended to the Date of the Week, and I'm wanting to display the Day of the Week on the first line, and Date of the Week on the second line like so: 标签包含包含“星期几”,并附加到“星期几”,我想在第一行显示“星期几”,在第二行显示“星期几”,如下所示:

在此处输入图片说明

However, it keeps displaying on the same line: Thursday 11 27 但是,它始终显示在同一行上:Thursday 11 27

Here's my code in viewDidLoad: 这是我在viewDidLoad中的代码:

NSMutableString *appendedDate= [NSMutableString stringWithCapacity: 20];

NSDateFormatter *nowDateFormat = [ [NSDateFormatter alloc] init];
[nowDateFormat setDateFormat:@"MM dd"];

NSDate *date = [[NSDate alloc] init];

NSString *theDateNow = [nowDateFormat stringFromDate:date];

NSDateFormatter *dateFormatter = [ [NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"EEEE"];

[appendedDate appendString:[dateFormatter stringFromDate:[NSDate date]]];
[appendedDate appendString:@" "];
[appendedDate appendString:theDateNow];

[self.todaysDate setTitle:appendedDate forState:UIControlStateNormal];

self.todaysDate.titleLabel.numberOfLines = 0;
self.todaysDate.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;

I've tried to shrink the width of the UIButton to see if it would wrap the next word or character, but it still remains on the same line. 我试图缩小UIButton的宽度,以查看是否可以包装下一个单词或字符,但仍保持在同一行。

How can I achieve this? 我该如何实现?

You almost got it right. 你几乎是对的。 All you needed is this [appendedDate appendString:@"\\n"]; 您只需要此[appendedDate appendString:@“ \\ n”]; in the place of [appendedDate appendString:@" "]; 代替[appendedDate appendString:@“”];

try this Code. 试试这个代码。

    NSMutableString *appendedDate= [NSMutableString stringWithCapacity: 20];

NSDateFormatter *nowDateFormat = [ [NSDateFormatter alloc] init];
[nowDateFormat setDateFormat:@"MMM dd"];

NSDate *date = [[NSDate alloc] init];

NSString *theDateNow = [nowDateFormat stringFromDate:date];

NSDateFormatter *dateFormatter = [ [NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"EEEE"];

[appendedDate appendString:[dateFormatter stringFromDate:[NSDate date]]];
[appendedDate appendString:@"\n"];
[appendedDate appendString:theDateNow];


[self.todaysDate setTitle:appendedDate forState:UIControlStateNormal];
self.todaysDate.titleLabel.numberOfLines = 0;


CGSize buttonSize = [ self.todaysDate.titleLabel.text sizeWithAttributes:@{NSFontAttributeName:self.todaysDate.titleLabel.font}];



self.todaysDate.frame = CGRectMake(
                               self.todaysDate.frame.origin.x,  self.todaysDate.frame.origin.y,
                               self.todaysDate.frame.size.width, buttonSize.height);

NOTE: Make sure your button has enough width for Day name. 注意:确保您的按钮具有足够的宽度以用作“日期”名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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