NSMutableAttributedString转换为字符串,同时保持格式

Mad*_*adu 2 uitableview nsstring nsattributedstring ios

我想为表格视图分配标题标题,但我希望标题分成两行。第二行应具有比顶行小的字体。

我正在尝试使用NSMutableAttributedString,但是在方法中:

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
Run Code Online (Sandbox Code Playgroud)

但它返回NSString而不是NSMutableAttributedString

有什么办法,我可以转换NSMutableAttributedStringNSString同时保持字符串格式化?

这是我的代码:

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    NSString *title = @"";
    title = @"Top Line \n";
    if(_favouriteLocations.count == 0){
        title = [title stringByAppendingString:@"Smaller font Bottom Line"];
    }
    NSMutableAttributedString *attString =[[NSMutableAttributedString alloc] initWithString:title];
    NSRange range = [title rangeOfString:@"Smaller"];

    [attString setAttributes:@{NSFontAttributeName:@"8"} range:NSMakeRange(range.location, title.length - range.location)];

     NSLog(@"%@", attString);
     title = [attString string];

     return title;
}
Run Code Online (Sandbox Code Playgroud)

Ric*_*ich 5

使用该tableView:titleForHeaderInSection:方法无法执行此操作。你需要使用tableView:viewForHeaderInSection:它是一种UITableViewDelegate方法,并返回一个UILabelattributedText

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

    NSString *title = @"";
    title = @"Top Line \n";
    if(_favouriteLocations.count == 0){
        title = [title stringByAppendingString:@"Smaller font Bottom Line"];
    }
    NSMutableAttributedString *attString =[[NSMutableAttributedString alloc] initWithString:title];
    NSRange range = [title rangeOfString:@"Smaller"];

    [attString setAttributes:@{NSFontAttributeName:@"8"} range:NSMakeRange(range.location, title.length - range.location)];

     UILabel *titleLabel = [UILabel new];
     titleLabel.attributedText = attString;

     return titleLabel;
}
Run Code Online (Sandbox Code Playgroud)

如果您有很多部分,也可以使用较新的UITableViewHeaderFooterView类并将其出队。该视图具有一个textLabel属性,就像上面一样,您可以对其进行设置attributedText。它的代码更多,但效率更高,因为您每次不必返回一个新创建的视图。

static NSString *const HeaderCellId = @"header_id";

-(void)viewDidLoad
{
    [super viewDidLoad];

    // Setup table view
    [tableView registerClass:[UITableViewHeaderFooterView class] forHeaderFooterViewReuseIdentifier:HeaderCellId];
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

    NSString *title = @"";
    title = @"Top Line \n";
    if(_favouriteLocations.count == 0){
        title = [title stringByAppendingString:@"Smaller font Bottom Line"];
    }
    NSMutableAttributedString *attString =[[NSMutableAttributedString alloc] initWithString:title];
    NSRange range = [title rangeOfString:@"Smaller"];

    [attString setAttributes:@{NSFontAttributeName:@"8"} range:NSMakeRange(range.location, title.length - range.location)];

    UITableViewHeaderFooterView *view = [tableView dequeueReusableHeaderFooterViewWithIdentifier:HeaderCellId];

    view.textLabel.attributedText = attString;

    return view;
}
Run Code Online (Sandbox Code Playgroud)

编辑

正如@PabloRomeu在注释中指出的那样,tableView:viewForHeaderInSection:仅当tableView:heightForHeaderInSection:实现返回非零值时,该注释才有效。

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 80;
}
Run Code Online (Sandbox Code Playgroud)