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

时间:2014-04-17 12:15:43

标签: ios uitableview nsstring nsattributedstring

我想为表格视图指定标题标题,但我希望标题分为两行。第二行的字体应小于顶行。

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

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

但它会返回NSString而不是NSMutableAttributedString

有什么方法可以在保持字符串格式的同时将NSMutableAttributedString转换为NSString

这是我的代码:

-(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;
}

2 个答案:

答案 0 :(得分:4)

使用tableView:titleForHeaderInSection:方法无法做到这一点。 您需要使用tableView:viewForHeaderInSection:方法UITableViewDataSource,并使用UILabel设置

返回attributedText
-(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;
}

如果你有很多部分,你也可以使用较新的UITableViewHeaderFooterView课程和dequeue课程。该视图具有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;
}

修改

@PabloRomeu在评论tableView:viewForHeaderInSection:中指出,只有在tableView:heightForHeaderInSection:实施时返回非零值才有效。

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 80;
}

答案 1 :(得分:0)

实现此tableview委托方法

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

并返回一个设置为AttributedString的UILabel text