請考慮長字符串,例如「位於美麗的巴爾的摩市中心」。UILabel:僅對文本的一部分應用換行符模式
我的標籤太小,這個文本,目前顯示它像這樣:
我喜歡的位置串爲中心截斷不截斷「位於」子,像這樣:
坐落在美麗的... ltimore市
的UILabel class reference表示這應該是可能的:
如果你想換行模式適用於文本的僅一部分,創建一個新的屬性串與所需的樣式信息,並將其與標籤相關聯。如果您未使用樣式文本,則此屬性適用於text屬性中的整個文本字符串。
在一個示例項目,只有一個的UILabel,我試圖遵循下面的代碼這些指令:
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *firstPart = @"Located in";
NSString *secondPart = @"beautiful downtown Baltimore City";
NSString *joined = [NSString stringWithFormat:@"%@ %@", firstPart, secondPart];
NSMutableAttributedString *joinedAttributed = [[NSMutableAttributedString alloc] initWithString:joined];
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
style.lineBreakMode = NSLineBreakByTruncatingMiddle;
NSRange detailRange = [joined rangeOfString:secondPart];
[joinedAttributed addAttribute:NSParagraphStyleAttributeName value:style range:detailRange];
self.label.attributedText = joinedAttributed;
}
我的標籤仍然出現相同的,並在最後的截斷。
這裏是最後的結果看起來像在調試器:
(lldb) po joinedAttributed
Located in {
}beautiful downtown Baltimore City{
NSParagraphStyle = "Alignment 4, LineSpacing 0, ParagraphSpacing 0, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 0/0, LineHeightMultiple 0, LineBreakMode 5, Tabs (\n 28L,\n 56L,\n 84L,\n 112L,\n 140L,\n 168L,\n 196L,\n 224L,\n 252L,\n 280L,\n 308L,\n 336L\n), DefaultTabInterval 0, Blocks (null), Lists (null), BaseWritingDirection -1, HyphenationFactor 0, TighteningFactor 0, HeaderLevel 0";
}
有沒有人得到這個工作?我的實現中缺少什麼?
您是否嘗試過將UILabel的lineBreakMode設置爲NSLineBreakByTruncatingMiddle?似乎你應該得到你想要的東西,甚至不必與屬性文本混雜在一起。 –
是;這將中心截斷整個字符串。期望的結果是中心截斷子字符串('secondPart')。 –
那麼,你有沒有嘗試設置lineBreakMode的標籤,風格? –