gpt4 book ai didi

ios - 如何将多段文本设置为在 iOS 中看起来像一个句子?

转载 作者:行者123 更新时间:2023-11-28 19:08:40 26 4
gpt4 key购买 nike

我有一行文字需要显示在 UITableViewCell 中。文本由来自数据库的多个部分组成。每个部分都有不同的颜色。例如:

Lorem ipsum dolar sit amet

每个项目都来自数据库并且是不同的颜色。

我正在尝试设置五个 UITextField(每个一个。)

到目前为止一切顺利。

如何让它看起来像单个字符串以确保字间距相同。

最佳答案

使用NSMutableAttributedString,你可以这样做:

NSArray *words = @[@"Lorem ", @"ipsum ", @"do", @"lar si", @"t amet"];
NSArray *colors = @[[UIColor blueColor], [UIColor greenColor], [UIColor yellowColor], [UIColor redColor], [UIColor blackColor]];

// Concatenate the list of words
NSMutableString *string = [NSMutableString string];
for (NSString *word in words)
[string appendString: word];

// Add the coloring attributes
NSMutableAttributedString *attrString = [[[NSMutableAttributedString alloc] initWithString: string] autorelease];

int location = 0;

for (int i = 0; i < words.count; i++) {

NSString *s = [words objectAtIndex: i];

[attrString addAttribute: NSForegroundColorAttributeName
value: [colors objectAtIndex: i]
range: NSMakeRange(location, s.length)];

location += s.length;
}


UILabel *label = [[UILabel alloc] initWithFrame: CGRectMake(20, 20, 280, 21)];
[label setAttributedText: attrString];
[self.view addSubview: label];
[label release];

关于ios - 如何将多段文本设置为在 iOS 中看起来像一个句子?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17891890/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com