【问题标题】:UIRefreshControl attributedTitle multiple linesUIRefreshControl 属性标题多行
【发布时间】:2013-04-22 11:33:08
【问题描述】:

是否可以在UIRefreshControl 标题中使用多行?每当我将 \n 添加到 NSAttributedString 时,只会显示第一行。我正在尝试设置标题,并在下一行设置更多文本。那么有没有解决方法在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;

【问题讨论】:

    标签: ios objective-c uirefreshcontrol


    【解决方案1】:

    这里有个棘手的方法:在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];
    }
    

    【讨论】:

      【解决方案2】:

      这段代码可以工作

      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;
      

      【讨论】:

      • 不起作用,文本彼此相邻显示。有人可以提供正确的答案吗?
      【解决方案3】:

      诀窍是将刷新控制器的UILable的属性更改为零。 hack 是我们必须通过子视图和子视图找到标签,如下所示。

      这是一个 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)
      }
      

      预期的输出应该是 -

      【讨论】:

        猜你喜欢
        • 2013-10-11
        • 1970-01-01
        • 2013-10-07
        • 2013-02-20
        • 2014-03-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多