Tôi đang cố tạo một UIButtoncó hai dòng văn bản trong titleLabel của nó. Đây là mã tôi đang sử dụng:
UIButton *titleButton = [[UIButton alloc] initWithFrame:CGRectMake(15, 10, frame.size.width-100, 100)];
titleButton.titleLabel.font = [UIFont boldSystemFontOfSize:24.0];
[titleButton setTitle:@"This text is very long and should get truncated at the end of the second line" forState:UIControlStateNormal];
titleButton.titleLabel.lineBreakMode = UILineBreakModeTailTruncation;
titleButton.titleLabel.numberOfLines = 2;
[self addSubview:titleButton];
Khi tôi thử điều này, văn bản chỉ xuất hiện trên một dòng. Có vẻ như cách duy nhất để đạt được nhiều hơn một dòng văn bản UIButton.titleLabellà thiết lập numberOfLines=0và sử dụng UILineBreakModeWordWrap. Nhưng điều này không đảm bảo văn bản có chính xác hai dòng.
UILabelTuy nhiên, sử dụng đồng bằng vẫn hoạt động:
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 10, frame.size.width-100, 100)];
titleLabel.font = [UIFont boldSystemFontOfSize:24.0];
titleLabel.text = @"This text is very long and should get truncated at the end of the second line";
titleLabel.numberOfLines = 2;
titleLabel.lineBreakMode = UILineBreakModeTailTruncation;
[self addSubview:titleLabel];
Có ai biết làm thế nào để làm cho UIButtoncông việc với hai dòng? Có phải giải pháp duy nhất để tạo một vùng riêng biệt UILabelđể giữ văn bản và thêm nó làm chế độ xem phụ của nút?
