NSAttributedString是否有[NSString stringWithFormat:]的类似物 [英] Are there any analogues of [NSString stringWithFormat:] for NSAttributedString

查看:118
本文介绍了NSAttributedString是否有[NSString stringWithFormat:]的类似物的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常我在界面构建器中构建app界面。有时设计需要使用属性字符串(字体,颜色等)。如果string是静态的,那么很容易配置。

但是如果string是动态的(带参数的格式),那么就没有办法在接口构建器中配置属性。它需要编写大量代码。

我正在为 NSAttributedString <寻找 [NSString stringWithFormat:] 的一些类似物/ code>。所以我将能够在界面构建器中设置字符串格式和必要的属性,然后在代码中提供必要的参数。



例如:

让我们考虑一下我需要这样格式的显示字符串:%d + %d = %d (所有数字均为粗体)。

我想在界面构建器中配置此格式。在代码中我想提供参数:1,1,2。应用程序应显示 1 + 1 = 2

解决方案

我一直在寻找这个问题的现有解决方案,但没有成功。

所以我能够实现它在我自己。

这就是为什么我自我回答与社区分享知识的问题。



解决方案



是一个示例项目。它提供了Basic和Pro格式示例。


Usually I build app interface in interface builder. Sometimes design requires to use attributed strings (fonts, colors and etc.). It's easy to configure if string is static.
But if string is dynamic (format with arguments) then there are no ways to configure attributes in interface builder. It requires to write a lot of code.
I am looking for some analogues of [NSString stringWithFormat:] for NSAttributedString. So I will be able to set string format and necessary attributes in interface builder, and then provide necessary arguments in code.

For example:
Let's consider that I need display string with such format: "%d + %d = %d" (all numbers are bold).
I want to configure this format in interface builder. In code I want to provide arguments: 1, 1, 2. App should show "1 + 1 = 2".

解决方案

I was looking for good existing solution for this question, but no success.
So I was able to implement it on my own.
That's why I am self-answering the question to share the knowledge with community.

Solution

NSAttributedString+VPAttributedFormat category provides methods for building attributed string based on attributed format and arguments that should satisfy this format.
The most suitable case of using this category is text controls with variable attributed text configured in interface builder.
You need set correct string format to attributed text and configure necessary attributes.
Then you need pass necessary arguments in code by using methods of this category.

  • Format syntax is the same as in [NSString stringWithFormat:] method;
  • Can be used in Objective C and Swift code;
  • Requires iOS 6.0 and later;
  • Integrated with CocoaPods;
  • Covered with unit tests.

Usage

1. Import framework header or module

// Objective C
// By header
#import <VPAttributedFormat/VPAttributedFormat.h>

// By module
@import VPAttributedFormat;


// Swift
import VPAttributedFormat

2. Set correct format and attributes for text control in interface builder

3. Create IBOutlet and link it with text control

// Objective C
@property (nonatomic, weak) IBOutlet UILabel *textLabel;


// Swift
@IBOutlet weak var textLabel: UILabel!

4. Populate format with necessary arguments

// Objective C
NSString *hot = @"Hot";
NSString *cold = @"Cold";

self.textLabel.attributedText = [NSAttributedString vp_attributedStringWithAttributedFormat:self.textLabel.attributedText,
                                 hot,
                                 cold];


// Swift
let hot = "Hot"
let cold = "Cold"
var arguments: [CVarArgType] = [hot, cold]
textLabel.attributedText = withVaList(arguments) { pointer in
    NSAttributedString.vp_attributedStringWithAttributedFormat(textLabel.attributedText, arguments: pointer)
}

5. See result

Examples

VPAttributedFormatExample is an example project. It provides Basic and Pro format examples.

这篇关于NSAttributedString是否有[NSString stringWithFormat:]的类似物的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆