如何在UIRefreshControl中使用NSAttributedString

3

我在这篇帖子中找到了NSAttributedString的示例。我的问题是 - 是否有任何提示,如何将其用于UIRefreshControl class

来自上面的帖子:

NSMutableAttributedString * string = [[NSMutableAttributedString alloc] initWithString:@"firstsecondthird"];
[string addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0,5)];
[string addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(5,6)];
[string addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(11,5)];
< p > UIRefreshControl调用的属性是自动的吗?< /p > < p > 编辑:< /p > < p > 我知道如何设置它,我想知道它是否除了作为格式化标签之外还有其他作用 - UIRefresherControl能否显示两个字符串?在被拉之前和之后分别显示一个字符串?当我发现无法输入“普通”字符串时,这就是我最初想到的。 < /p >
2个回答

5
这是一个完整的示例,展示如何在UIRefreshControl中创建和修改色调颜色、字体颜色和字体样式:
_refreshControl = [[UIRefreshControl alloc] init];
_refreshControl.tintColor = [UIColor blackColor];
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"Pull to Refresh"];
NSRange fullRange = NSMakeRange(0, [string length]);
[string addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:fullRange];
[string addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Georgia" size:17] range:fullRange];
[_refreshControl setAttributedTitle:string];
[_refreshControl addTarget:self action:@selector(refresh:) forControlEvents:UIControlEventValueChanged];
[self setRefreshControl:_refreshControl];

4

看到 UIRefreshControl 有一个 attributedTitle 属性,它接受一个 NSAttributedString。而且因为 NSMutableAttributedStringNSAttributedString 的子类,所以你可以这样做:

[myRefreshControl setAttributedTitle:string];

UIRefresherControl能够显示两个字符串吗?

不行。但你可以尝试使用带有多行的属性字符串,或者使用不同的段落样式(例如,一个部分可以左对齐,另一个部分可以右对齐)。

一个在被拉动之前,一个在被拉动之后?

不行。根据您自己应用程序的逻辑,更改刷新控件的属性标题由您自己决定。


抱歉我的问题没有表达清楚,我会进行编辑。 - Michal
多行属性字符串使用“\n”无法正常工作。只有第一行被显示。 - Marián Černý

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