我想用NSMutableAttributedString創建多行UILabel。 當我給你這樣的完整的字符串的屬性也能正常工作:如何使用NSMutableAttributedString呈現多行UILabel
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;
但我什麼,我需要的是應用一系列的NSAttributedString的不同子範圍的屬性(大膽某些詞)。
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;
我在發現的是,如果我這樣做,文本不會再通過標籤中的多行顯示。它被渲染爲一條線,垂直居中在標籤的框架中。
有什麼我在這裏失蹤?
您是否將標籤設計爲足夠高的第二行? – 2013-02-27 17:51:09
我無法在6.1上重現這一點。我注意到你的代碼在setFont:行中有些不正確(缺少],你在一個地方使用「label」而不是「contentLabel」)。你確定這是實際的代碼嗎? – 2013-02-27 18:02:05
@凱文巴拉德:是的,標籤足夠高。 – adriaan 2013-02-27 21:08:58