【问题标题】:Different Font Styles in a String in an Array数组中字符串中的不同字体样式
【发布时间】:2014-12-01 23:43:13
【问题描述】:

假设我有这个:

self.dictionary = [NSMutableArray arrayWithObjects:
                           @"blah",
                           @"blah",
                           @"blah (blah)",
                           @"blah",
                           @"blah",
                           nil];

在第三个对象中,我希望第一个“blah”具有更大的字体大小和黑色字体颜色。但是,我希望带括号的第二个“blah”具有较小的尺寸和灰色。我该怎么做呢?有可能吗?

【问题讨论】:

    标签: objective-c nsstring nsarray


    【解决方案1】:

    您想使用NSMutableAttributedString

    您只需连接所有字符串并为每个子字符串确定您想要的属性,而不是调用- setAttributes:range:

    【讨论】:

      【解决方案2】:

      NSString 不允许你有特定的风格。如果您的字符串需要具有样式/属性,则必须使用 NSAttributedString。

      【讨论】:

        【解决方案3】:

        现在您只存储 NSString 对象。 NSString 没有附加任何字体。如果你想保留字体信息 - 你可以看看NSAttributedString class - https://developer.apple.com/Library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSAttributedString_Class/index.html

        所以你的代码看起来像:

        self.items = @[
        [[NSAttributedString alloc] initWithString: @"bla" attributes: @{
            NSFontAttributeName: [UIFont systemFontOfSize: 12.0],
            NSForegroundColorAttributeName: [UIColor blackColor]
        }],
        [[NSAttributedString alloc] initWithString: @"bla" attributes: @{
            NSFontAttributeName: [UIFont systemFontOfSize: 14.0],
            NSForegroundColorAttributeName: [UIColor grayColor]
        }]
        ];
        

        【讨论】:

          【解决方案4】:

          试试下面的代码:-

          NSArray *yourArr= @[@"blah",
                                 @"blah",
                                 @"blah (blah)",
                                 @"blah",
                                 @"blah"];
          
          
              for (NSString *word in yourArr)
              {
                  if ([word isEqualToString:@"blah (blah)"])
                  {
          
                      [yourAtt appendAttributedString:[[NSAttributedString  alloc]initWithString:[word componentsSeparatedByString:@" "][0] attributes:@{NSFontAttributeName:[NSFont boldSystemFontOfSize:18]}]];
                      [yourAtt appendAttributedString:[[NSAttributedString alloc]initWithString:@" "]];
                      [yourAtt appendAttributedString:[[NSAttributedString  alloc]initWithString:[word componentsSeparatedByString:@" "][1] attributes:@{NSFontAttributeName:[NSFont boldSystemFontOfSize:14],NSForegroundColorAttributeName:[NSColor grayColor]}]];
                  }
                  else
                  {
                      [yourAtt appendAttributedString:[[NSAttributedString alloc]initWithString:word]];
                  }
                  [yourAtt appendAttributedString:[[NSAttributedString alloc]initWithString:@",\n"]];
          
              }
              self.yourAttStr=yourAtt;
          

          【讨论】:

            猜你喜欢
            • 2017-11-14
            • 1970-01-01
            • 1970-01-01
            • 2021-07-19
            • 2013-03-03
            • 2016-03-11
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多