• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

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

[复制链接]
菜鸟教程小白 发表于 2022-12-13 07:28:31 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我想为我的表格 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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap