UIRefreshControl属性标题多行

4

UIRefreshControl标题中使用多行文本是否可行?每当我在NSAttributedString中添加\n时,只会显示第一行。我正在尝试设置一个标题,下一行是更多的文本。那么,在UIRefreshControl中使用两行文本的解决方法是什么?

以下是当前代码,只显示“Title Here”:

self.refreshControl = [[UIRefreshControl alloc] init];

NSString *title = @"Title Here";
NSString *subText = @"Subtext Here";
NSMutableAttributedString *attString=[[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@\n%@",title,subText]];

[attString addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Helvetica" size:20.0f] range:NSMakeRange(0, [title length])];
[attString addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Helvetica" size:14.0f] range:NSMakeRange([title length],[subText length])];

[attString addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0, [title length])];
[attString addAttribute:NSForegroundColorAttributeName value:[UIColor lightGrayColor] range:NSMakeRange([title length], [subText length])];

self.refreshControl.attributedTitle = attString;
3个回答

4

这里有一个棘手的方法:在 UIRefreshControl 中查找 UILabel,并将 numberOfLines = 0。

UILabel *titleLabel = [[[[self.refreshControl subviews] firstObject] subviews] lastObject];
if (titleLabel) {
    titleLabel.numberOfLines = 0;

    NSString title = @"Pull to Refresh.\nUpdated Time: 09:30"; // \n for new line.
    self.refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:title];
}

1
这段代码将会运行。
NSString *title = @"Title Here";
NSString *subText = @"Subtext Here";

NSMutableAttributedString *titleAttString =  [[NSMutableAttributedString alloc] initWithString:title];
NSMutableAttributedString *subTitleAttString =  [[NSMutableAttributedString alloc] initWithString:subText];

[titleAttString addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Helvetica" size:20.0f] range:NSMakeRange(0, [title length])];
[subTitleAttString addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Helvetica" size:14.0f] range:NSMakeRange(0,[subTitle length])];

[titleAttString addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0, [title length])];
[subTitleAttString addAttribute:NSForegroundColorAttributeName value:[UIColor lightGrayColor] range:NSMakeRange(0, [subTitle length])];

[titleAttString appendAttributedString:subTitleAttString];

self.refreshControl.attributedTitle = titleAttString;

1
无法正常工作,文本并排显示。有人能提供正确的答案吗? - ennzhed

0
诀窍是将Refresh Controller的UILable属性更改为零。 黑客技巧是我们必须通过子视图和它们的子项找到标签,如下所示。
这是一个Swift版本。
    if let refreshLabel = refreshControl?.subviews.first?.subviews.last as? UILabel {
            refreshLabel.numberOfLines = 0
    }

这里是完整的代码示例

private var marketRefreshController = UIRefreshControl()
private var lastUpdatedDate = Date()

override func viewDidLoad() {
    super.viewDidLoad()
    tableView.refreshControl = marketRefreshController
    if let refreshLabel = refreshControl?.subviews.first?.subviews.last as? UILabel {
        refreshLabel.numberOfLines = 0
    }
    marketRefreshController.addTarget(self, action: #selector(refreshMarketData), for: .valueChanged)
    marketRefreshController.tintColor = UIColor.blue
    let textAttributes = [NSAttributedString.Key.font: UIFont.appSubTitle,
                          NSAttributedString.Key.foregroundColor: UIColor.blue]
    let timeSinceLastUpdate = lastUpdatedDate.timeAgoSinceNow // Date Sting Converter Helper Method
    let displayString = "Fetching Market Data ... \n Last Updated: \(timeSinceLastUpdate)"
    marketRefreshController.attributedTitle = NSAttributedString(string: displayString,
                                                                 attributes: textAttributes)
    tableView.addSubview(marketRefreshController)
}

期望的输出应该像这样 - 在tableview控制器上使用多行文本的刷新控件

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接