0

我遇到了保留屬性NSMutableString的問題。我有一個UITableView,每個UITableViewCell都有一個屬性文本。設置屬性文本沒有問題,但在選擇時,UITableViewCell的屬性會丟失。這是我在的cellForRowAtIndexPath,設置屬性代碼:NSMutableAttributedString UILabel在選擇時沒有保留其屬性

NSMutableAttributedString *changesStyleString_h = [[NSMutableAttributedString alloc] initWithString:@"Attributes change!" attributes:@{NSFontAttributeName: [UIFont boldSystemFontOfSize:20], NSForegroundColorAttributeName:[UIColor yellowColor]}]; 

[changesStyleString_h addAttributes:@{ NSUnderlineStyleAttributeName:@(1)} range:NSMakeRange(11, 6)]; 
cell.mainLabel.attributedText = changesStyleString 

可能我想指出,mainLabel也是一個UILabel,不需要定製那裏。任何幫助正確的方向將不勝感激!

回答

1

我發現我需要在ENTIRE字符串上設置屬性,否則它會做些時髦的事情。

NSString* string = @"1 - some string" 
NSMutableAttributedString* string = [[NSMutableAttributedString alloc] initWithString:string]; 
[string setAttributes:@{NSForegroundColorAttributeName: accent, NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue-Bold" size:13.f]} range:NSMakeRange(0, 1)]; 

這會在突出顯示單元格時引起奇怪的行爲。

但是,我這樣做的時候:

[string setAttributes:@{NSForegroundColorAttributeName: [UIColor blackColor]} range:NSMakeRange(1, [labelTwo length] - 1)]; 

一切似乎都按預期運行。

希望有幫助!

相關問題