如何使用 NSAttributedString?
NSString
或 NSMutableStrings
中不可能有多种颜色。所以我听说过一些关于 NSAttributedString
是随 iPad SDK 3.2(或 3.2 左右)引入的,自 < strong>iPhone SDK 4.0 测试版。
我想要一根具有三种颜色的绳子。
我不使用 3 个单独的 NSString 的原因是因为三个 NSAttributedString
子字符串中每个子字符串的长度经常变化,所以我宁愿不使用任何计算来重新定位 3 个单独的 NSString
对象。
如果可以使用 NSAttributedString
我该如何进行以下操作 - (如果不可能使用 NSAttributed string 你会怎么做):
编辑: 请记住,@"first"
、@"second"
和 @"third"
随时会被其他字符串替换。因此使用硬编码的 NSRange 值是行不通的。
Multiple colours in an NSString
or NSMutableStrings
are not possible. So I've heard a little about the NSAttributedString
which was introduced with the iPad SDK 3.2 (or around 3.2) and is available on the iPhone as of iPhone SDK 4.0 beta.
I would like to have a string that has three colours.
The reason I don't use 3 separate NSStrings, is because the length of each of the three NSAttributedString
substrings changes often and so I would prefer, not to use any calculations to re-position 3 separate NSString
objects.
If it's possible using NSAttributedString
how do I make the following - (if not possible with NSAttributed string how would you do it):
Edit:
Remember, @"first"
, @"second"
and @"third"
will be replaced by other strings at any time. So using hardcoded NSRange values won't work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
在构建属性字符串时,我更喜欢使用可变子类,只是为了让事情更干净。
话虽这么说,以下是创建三色属性字符串的方法:
在浏览器中输入 。 警告实现者
显然,您不会在这样的范围内进行硬编码。也许你可以这样做:
When building attributed strings, I prefer to use the mutable subclass, just to keep things cleaner.
That being said, here's how you create a tri-color attributed string:
typed in a browser. caveat implementor
Obviously you're not going to hard-code in the ranges like this. Perhaps instead you could do something like:
问题已经得到解答...但我想展示如何添加阴影并使用 NSAttributedString 更改字体,这样当人们搜索这个主题时,他们就不必继续寻找。
这是 Swift 版本...
警告!
这适用于 4s。
对于 5 秒,您必须将所有 Float 值更改为 Double 值(因为编译器尚未正常工作)
用于字体选择的 Swift 枚举:
用于枚举访问的 Swift 数组(需要,因为枚举不能使用“-”):
Swift 属性文本函数:
用于查找字符串数组中的范围的第一个和最后一个扩展:
新颜色:
我的宏替换:
带有属性文本的按钮制作器:
带有属性文本、阴影和圆角的 UIView/UILabel 制作器:
通用阴影添加:
视图样式的视图扩展:
实际视图确实加载功能:
The question is already answered... but I wanted to show how to add shadow and change the font with NSAttributedString as well, so that when people search for this topic they won't have to keep looking.
Here is a Swift version...
Warning!
This works for 4s.
For 5s you have to change all of the the Float values to Double values (because the compiler isn't working correctly yet)
Swift enum for font choice:
Swift array for enum access (needed because enum can't use '-'):
Swift attributed text function:
first and last extension used for finding ranges in a string array:
new colors:
my macro replacement:
my button maker w/attributed text:
my UIView/UILabel maker w/attributed text, shadow, and round corners:
generic shadow add:
view extension for view style:
the actual view did load function:
我认为,使用正则表达式来查找应用属性的范围是一种非常方便的方法。我就是这样做的:
我正在搜索可用属性的列表,但在此处和类参考的首页中没有找到它们。所以我决定在这里发布相关信息。
标准属性
属性字符串支持以下文本标准属性。如果该键不在字典中,则使用下面描述的默认值。
NSAttributedString 编程指南 完整的类参考位于此处。
I think, it is a very convenient way to use
regular expressions
to find a range for applying attributes. This is how I did it:I was searching for a list of available attributes and didn't find them here and in a class reference's first page. So I decided to post here information on that.
Standard Attributes
Attributed strings support the following standard attributes for text. If the key is not in the dictionary, then use the default values described below.
NSAttributedString programming guide
A full class reference is here.
该解决方案适用于任何长度
This solution will work for any length
我编写了帮助程序来轻松添加属性:
https://github.com/shmidt/MASAttributes
您也可以通过 CocoaPods 安装:
pod 'MASAttributes', '~>; 1.0.0'
I wrote helper to add attributes easily:
https://github.com/shmidt/MASAttributes
You can install through CocoaPods also :
pod 'MASAttributes', '~> 1.0.0'
从 iOS 7 开始,您可以将
NSAttributedString
与 HTML 语法一起使用:您必须将文件“string.html”添加到您的项目中,并且 html 的内容可以如下所示:
现在,您可以使用 < code>NSAttributedString 根据需要,即使没有 HTML 文件,例如:
来源
Since iOS 7 you can use
NSAttributedString
with HTML syntax:You have to add the file "string.html" to you project, and the content of the html can be like this:
Now, you can use
NSAttributedString
as you want, even without HTML file, like for example:Source
我总是发现使用属性字符串是一个极其冗长乏味的过程。
所以我制作了一个 Mac 应用程序来为您创建所有代码。
https://itunes.apple.com/us/app/attributed-string-创建者/id730928349?mt=12
I always found working with attributed strings to be an incredibly long winded and tedious process.
So I made a Mac App that creates all the code for you.
https://itunes.apple.com/us/app/attributed-string-creator/id730928349?mt=12
带有属性字符串扩展的更简单的解决方案。
尝试一下并查看(在 Swift 3 和 4 中测试)
以下是预期结果:
An easier solution with attributed string extension.
Try this and see (Tested in Swift 3 & 4)
Here is expected result:
在 Swift 4 中:
In Swift 4:
您可以在 Swift 中加载
HTML
属性字符串,如下所示从文件
http://sketchytech.blogspot.in/2013/11/creating-nsattributedstring-from-html.html
并根据您所需的属性设置字符串....遵循此..
http://makeapppie.com/2014/ 10/20/swift-swift-using-attributed-strings-in-swift/
You can load an
HTML
attributed string inSwift
as followTo load a
html
from filehttp://sketchytech.blogspot.in/2013/11/creating-nsattributedstring-from-html.html
And setup string as per your required attribute....follow this..
http://makeapppie.com/2014/10/20/swift-swift-using-attributed-strings-in-swift/
我创建了一个库,使这变得更加容易。查看 ZenCopy。
您可以创建 Style 对象,和/或将它们设置为键以供稍后引用。像这样:
然后,您可以轻松构建字符串并设置它们的样式并具有参数:)
您还可以使用正则表达式搜索轻松设置样式!
这会将前面带有“@”的所有单词设置为“token”样式。 (例如@jpmcglon)
我仍然需要让它与
NSAttributedString
提供的一切一起工作,但我认为fontName
、fontSize
和颜色封面其中大部分。预计很快会有大量更新:)如果您需要,我可以帮助您开始使用。也在寻找反馈,所以如果它让你的生活更轻松,我会说任务完成。
I made a library that makes this a lot easier. Check out ZenCopy.
You can create Style objects, and/or set them to keys to reference later. Like this:
Then, you can easily construct strings AND style them AND have params :)
You can also style things easily with regex searches!
This will style all words with '@' in front of it with the 'token' style. (e.g. @jpmcglone)
I need to still get it working w/ everything
NSAttributedString
has to offer, but I thinkfontName
,fontSize
and color cover the bulk of it. Expect lots of updates soon :)I can help you get started with this if you need. Also looking for feedback, so if it makes your life easier, I'd say mission accomplished.
为了解决此类问题,我在 swift 中创建了一个名为 Atributika 的库。
您可以在这里找到它 https://github.com/psharanda/Atributika
To solve such kind of problems I created library in swift which is called Atributika.
You can find it here https://github.com/psharanda/Atributika
Swift 4
属性列表请参见此处 NSAttributedStringKey在苹果文档上
Swift 4
Attributes list please see here NSAttributedStringKey on Apple Docs
超级简单的方法来做到这一点。
欲了解更多详情,请点击这里;
https://github.com/iOSTechHub/AttributedString
Super easy way to do this.
For more detail click here;
https://github.com/iOSTechHub/AttributedString