Tôi biết đây là một câu hỏi cũ nhưng tôi đã phải đối mặt với cùng một vấn đề và câu trả lời ở đây đã giúp tôi. Tôi cần mở rộng tiêu đề và các trường văn bản tin nhắn để chứa thêm văn bản trên iPad.
Điều tôi đã làm là triển khai phương thức ủy quyền UIAlertView willPresentAlertView: và thêm hai đối tượng UILabel để làm tiêu đề và thông báo. Tôi đã có thể cấu hình kích thước khung hình và nguồn gốc của hai nhãn này. Sau khi có được kích thước mong muốn cho các trường văn bản, tôi thay đổi kích thước cảnh báo để chứa văn bản mới. Sau đó, tôi lặp qua các bản xem trước của chế độ xem cảnh báo để tìm nút và điều chỉnh khung của nó.
- (void)willPresentAlertView:(UIAlertView *)alertView {
// add a new label and configure it to replace the title
UILabel *tempTitle = [[UILabel alloc] initWithFrame:CGRectMake(10,20,350, 20)];
tempTitle.backgroundColor = [UIColor clearColor];
tempTitle.textColor = [UIColor whiteColor];
tempTitle.textAlignment = UITextAlignmentCenter;
tempTitle.numberOfLines = 1;
tempTitle.font = [UIFont boldSystemFontOfSize:18];
tempTitle.text = alertView.title;
alertView.title = @"";
[alertView addSubview:tempTitle];
[tempTitle release];
// add another label to use as message
UILabel *tempLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 50, 350, 200)];
tempLabel.backgroundColor = [UIColor clearColor];
tempLabel.textColor = [UIColor whiteColor];
tempLabel.textAlignment = UITextAlignmentCenter;
tempLabel.numberOfLines = 20;
tempLabel.text = alertView.message;
[alertView addSubview:tempLabel];
// method used to resize the height of a label depending on the text height
[self setUILabel:tempLabel withMaxFrame:CGRectMake(10,50, 350, 300) withText:alertView.message];
alertView.message = @"";
// set the frame of the alert view and center it
alertView.frame = CGRectMake(CGRectGetMinX(alertView.frame) - (370 - CGRectGetWidth(alertView.frame))/2 ,
alertView.frame.origin.y,
370,
tempLabel.frame.size.height + 120);
// iterate through the subviews in order to find the button and resize it
for(UIView *view in alertView.subviews)
{
if([[view class] isSubclassOfClass:[UIControl class]])
{
view.frame = CGRectMake (view.frame.origin.x+2,
tempLabel.frame.origin.y + tempLabel.frame.size.height + 10,
370 - view.frame.origin.x *2-4,
view.frame.size.height);
}
}
[tempLabel release];
}
Và phương pháp được sử dụng để thay đổi kích thước nhãn:
- (void)setUILabel:(UILabel *)myLabel withMaxFrame:(CGRect)maxFrame withText:(NSString *)theText{
CGSize stringSize = [theText sizeWithFont:myLabel.font constrainedToSize:maxFrame.size lineBreakMode:myLabel.lineBreakMode];
myLabel.frame = CGRectMake(myLabel.frame.origin.x,
myLabel.frame.origin.y,
myLabel.frame.size.width,
stringSize.height
);
}
Tôi không biết nếu điều này là cách tốt nhất để làm điều này nhưng nó làm việc cho tôi.
Bạn đã bao giờ tìm thấy một cách để làm điều này? Chỉ cần tò mò, rất nhiều câu trả lời ở đây, ba năm trôi qua ... vv – Morkrom