ios - 将 2 个 NSAttributedStrings 放在一个标签中

标签 ios uitableview label nsattributedstring

是否可以将 2 个 NSAttributedString 放在一个标签中?

例如:

NSString *a =  [car valueForKey:@"name"];
NSString *b =  [car valueForKey:@"version"];

NSAttributedString *title;
    title = [[NSAttributedString alloc] initWithString:a attributes:@{ NSFontAttributeName : [UIFont fontWithName:@"Noteworthy-Bold" size:36], NSUnderlineStyleAttributeName : @1 , NSStrokeColorAttributeName : [UIColor blackColor]}]; //1

    NSAttributedString *title2;
    title2 = [[NSAttributedString alloc] initWithString:b attributes:@{ NSFontAttributeName : [UIFont fontWithName:@"Noteworthy-Bold" size:36], NSUnderlineStyleAttributeName : @0 , NSStrokeColorAttributeName : [UIColor blackColor]}]; //2

    cell.textLabel.attributedText = //what should I write here?

最佳答案

您可以使用 -appendAttributedString:(NSString *) 方法将一个属性字符串附加到另一个。由于您可以使两个属性字符串都可变,因此您还可以包含分隔符(分号、逗号)以区分同一个标签中的两个不同字符串。

下面是一些示例代码:

NSMutableAttributedString *string1 = [[NSMutableAttributedString alloc] initWithString:@"Hello"];
        NSMutableAttributedString *string2 = [[NSMutableAttributedString alloc] initWithString:@"Hello 2"];
NSMutableAttributedString *semiStr = [[NSMutableAttributedString alloc] initWithString:@" ; "];
[string1 appendAttributedString:semiStr]; //separate string 1 and 2 by semi-colon        
[string1 appendAttributedString:string2];

textLabel.text = string1; //which string2 is now appended to

显然,在您的属性字符串中,您会有属性(就像您在问题中所说的那样)。 您也可以始终信任 Apple's documentation on the NSMutableAttributedString class找到适合下次使用的方法。

关于ios - 将 2 个 NSAttributedStrings 放在一个标签中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17774039/

相关文章:

ios - Objective-c 如何从 UITableView 子类中进行 Segue

highcharts 隐藏 xAxis

ios - __NSCFString playbackFinished 来自 MPMoviePlayerController 的无法识别的选择器

ios - 在 iOS 中创建自定义框架

ios - 仅将圆角添加到 UITableView 的顶部?

ios - UITableView(Cell) 没有填满整个屏幕

ios - Swift 从第一响应者那里辞职 TextView

ios - 添加do时,try catch Google Analytic tracker got error

label - 如何将点击监听器附加到 Vaadin 标签?

css - 创建 CSS 以放大复选框