Tôi đang cố gắng tạo một UILabel nhiều dòng với NSMutableAttributedString. này hoạt động tốt khi tôi gán một thuộc tính vào chuỗi hoàn chỉnh như thế này:Cách hiển thị UILabel đa dòng với NSMutableAttributedString
UILabel *contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,0,200,100)];
[contentLabel setLineBreakMode:NSLineBreakByWordWrapping];
[contentLabel setNumberOfLines:0];
[contentLabel setFont:[UIFont systemFontOfSize:13];
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"hello I am a long sentence that should break over multiple lines"];
[string addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:13] range:NSMakeRange(0, [string length])];
contentLabel.attributedText = string;
Nhưng tôi những gì tôi cần là để áp dụng một số thuộc tính cho subranges khác nhau của NSAttributedString (lời một số đậm).
UILabel *contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,0,200,100)];
[contentLabel setLineBreakMode:NSLineBreakByWordWrapping];
[contentLabel setNumberOfLines:0];
[contentLabel setFont:[UIFont systemFontOfSize:13];
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"hello I am a long sentence that should break over multiple lines"];
[string addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:13] range:NSMakeRange(3, 5)];
contentLabel.attributedText = string;
Điều tôi thấy là nếu tôi làm điều này, văn bản không được hiển thị trên nhiều dòng trong nhãn nữa. Nó được hiển thị dưới dạng một dòng, được căn giữa theo chiều dọc trong khung của nhãn.
Có điều gì tôi thiếu ở đây không?
Bạn đã tạo nhãn đủ cao cho dòng thứ hai chưa? –
Tôi không thể sao chép điều này trên 6.1. Tôi lưu ý rằng mã của bạn hơi sai (có một thiếu] trong dòng setFont: và bạn sử dụng "label" thay vì "contentLabel" ở một nơi). Bạn có chắc đây là mã thực tế không? –
@Kevin Ballard: Có, nhãn đủ cao. – adriaan