OGeek|极客世界-中国程序员成长平台

标题: ios - NSMutableAttributedString 转换为字符串,同时保持格式 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 07:28
标题: ios - NSMutableAttributedString 转换为字符串,同时保持格式

我想为我的表格 View 分配一个标题标题,但我希望标题分两行。第二行的字体应该比第一行小。

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

-(NSString *)tableViewUITableView *)tableView titleForHeaderInSectionNSInteger)section

但它返回一个 NSString 而不是 NSMutableAttributedString

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

这是我的代码:

-(NSString *)tableViewUITableView *)tableView titleForHeaderInSectionNSInteger)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;
}



Best Answer-推荐答案


使用 tableView:titleForHeaderInSection: 无法做到这一点方法。 您需要使用 tableView:viewForHeaderInSection:这是一个 UITableViewDelegate方法并返回 UILabelattributedText设置

-(UIView *)tableViewUITableView *)tableView viewForHeaderInSectionNSInteger)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那。该 View 具有 textLabel属性,就像上面一样,您可以在其上设置 attributedText 。它的代码多一点,但效率更高,因为您不必每次都返回新创建的 View 。

static NSString *const HeaderCellId = @"header_id";

-(void)viewDidLoad
{
    [super viewDidLoad];

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

-(UIView *)tableViewUITableView *)tableView viewForHeaderInSectionNSInteger)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)tableViewUITableView *)tableView heightForHeaderInSectionNSInteger)section {
    return 80;
}

关于ios - NSMutableAttributedString 转换为字符串,同时保持格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23132831/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4