我有一个ivar,它在一个对象的init中被分配:
attString = [[NSMutableAttributedString alloc] init];
Run Code Online (Sandbox Code Playgroud)
在循环中,我想清除attString的内容并重新使用它.我该怎么做呢?
谢谢!
iphone objective-c mutable core-foundation nsattributedstring
[attributedString enumerateAttributesInRange:range options:NSAttributedStringEnumerationReverse usingBlock:
^(NSDictionary *attributes, NSRange range, BOOL *stop) {
NSMutableDictionary *mutableAttributes = [NSMutableDictionary dictionaryWithDictionary:attributes];
[mutableAttributes setObject:[NSNumber numberWithInt:1] forKey:@"NSUnderline"];
attributes = mutableAttributes;
}];
Run Code Online (Sandbox Code Playgroud)
我试图循环所有属性并添加NSUnderline给他们.在调试时,似乎NSUnderline被添加到字典中,但是当我第二次循环时它们被删除.我在更新NSDictionaries时做错了什么?
在玩NSAttributedString时,我遇到了一些来自UITextView的奇怪行为.说我有两个属性:
@property (weak, nonatomic) IBOutlet UILabel *label;
@property (weak, nonatomic) IBOutlet UITextView *textView;
Run Code Online (Sandbox Code Playgroud)
在这些属性的拥有控制器中,我有以下代码:
NSDictionary *attributes = @{NSFontAttributeName : [UIFont systemFontOfSize:20.],
NSForegroundColorAttributeName: [UIColor redColor]};
NSAttributedString *as = [[NSAttributedString alloc] initWithString:@"Hello there!" attributes:attributes];
NSMutableAttributedString *mas = [[NSMutableAttributedString alloc] initWithString:@"Hello where?" attributes:nil];
[mas addAttribute:NSForegroundColorAttributeName value:[UIColor yellowColor] range:NSMakeRange(3, 5)];
self.label.attributedText = as;
self.label.attributedText = mas;
self.textView.attributedText = as;
self.textView.attributedText = mas;
Run Code Online (Sandbox Code Playgroud)
在模拟器中运行时,标签使用系统默认字体查看(呃,使用您的想象力),如下所示:
<black>Hel</black><yellow>lo wh</yellow><black>ere?</black>
Run Code Online (Sandbox Code Playgroud)
使用大小为20.0的系统字体,文本视图如下所示:
<red>Hel</red><yellow>lo wh</yellow><red>ere?</red>
Run Code Online (Sandbox Code Playgroud)
看起来文本视图组合了两个属性字符串的属性.我发现这是一个令人惊讶的结果,并期望它表现得像标签.
我怀疑这是一个错误.如果不是,UITextView如何以及为什么以不同于UILabel的方式处理attributionText?
(XCode版本4.5.1)
我的旧代码使用NSLocalizedString来显示以下内容,其中outputText是一个NSMutableString,在单个输出会话中包含许多这样的行:
[outputText appendFormat: NSLocalizedString(@"\n\n%@ and %@ are identical. No comparison required.", @"\n\n%@ and %@ are identical. No comparison required."), self.ipAddress, secAddress.ipAddress];
Run Code Online (Sandbox Code Playgroud)
我正在尝试更改各种ipAddress字符串的颜色,但在使用NSMutableAttributedString时找不到类似的方法.
我面临的最大问题是,由于整个字符串将被本地化,我无法在不分解格式化输出的每个部分的情况下可靠地设置NSRange.
我是否需要剖析此字符串的每个部分,将其转换为NSAttributedString并将每个部分附加到outputText?
这一定是件容易的事,但我无法理解.
我有一个NSMutableAttributedString,例如,"这是一个测试".我想给"测试"蓝色这个词加上颜色,我这样做:
NSMutableAttributedString *coloredText = [[NSMutableAttributedString alloc] initWithString:@"This is a test"];
[coloredText addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(10,4)];
Run Code Online (Sandbox Code Playgroud)
这很好用.但是现在我想将"测试"后输入的任何内容的文本颜色设置为黑色.
如果我做:
[coloredText addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(coloredText.string.length, 1)];
Run Code Online (Sandbox Code Playgroud)
我得到一个objectAtIndex:effectiveRange:越界错误.假设因为范围超出了字符串的长度.
如果我做:
[coloredText addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(coloredText.string.length, 0)];
Run Code Online (Sandbox Code Playgroud)
错误消失但在"test"之后输入仍然是蓝色.
当它在字符串末尾时,如何在插入点设置当前颜色?
欢呼任何输入.
我有UITableView和动态大小调整单元格,以HTML格式显示评论列表,我遇到的问题是NSAttributedString渲染HTML内容非常慢!
这是分析器的快照.
我试图将NSAttributedString初始化放到单独的线程中,但仍然很慢并且用户在呈现HTML时看到空单元格,最后在完成呈现单元格时,布局不正确.
dispatch_async(GlobalQueue, {
let html = NSAttributedString(
data: self.comment.htmlBody.dataUsingEncoding(NSUnicodeStringEncoding, allowLossyConversion: false)!,
options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
documentAttributes: nil,
error: nil)
dispatch_async(MainQueue, {
self.commentLabel.attributedText = html
self.commentLabel.font = UIFont(name: "HelveticaNeue-Light", size: 14.0)!
if let writer = self.comment.author {
self.authorLabel.text = writer.name
}
self.layoutIfNeeded()
})
})
Run Code Online (Sandbox Code Playgroud)
请建议如何加快渲染和修复单元格布局.
谢谢!
更新:
使用单元格委托和标志解决,表明属性字符串已初始化.也许会帮助某人:
// TicketCell
private var isContentInitialized = false
private var commentAttributedString:NSAttributedString?
var delegate: TicketCommentCellDelegate?
var indexPath: NSIndexPath!
var comment: TicketComment! {
willSet {
if newValue != self.comment {
self.isContentInitialized = false
} …Run Code Online (Sandbox Code Playgroud) uitableview nsattributedstring grand-central-dispatch ios swift
我正在尝试确定a中给定字符串出现的索引String,然后NSRange使用这些索引生成一个以便为a添加属性NSMutableAttributedString.问题是rangeOfString退货,Range<Index>但addAttributes:range:预计会有NSRange.我尝试NSRange从开始和结束索引创建Range失败,因为String.CharacterView.Index不是Int,因此它不会编译.
如何使用Range<Index>值来创建NSRange?
var originalString = "Hello {world} and those who inhabit it."
let firstBraceIndex = originalString.rangeOfString("{") //Range<Index>
let firstClosingBraceIndex = originalString.rangeOfString("}")
let range = NSMakeRange(firstBraceIndex.startIndex, firstClosingBraceIndex.endIndex)
//compile time error: cannot convert value of type Index to expected argument type Int
let attributedString = NSMutableAttributedString(string: originalString)
attributedString.addAttributes([NSFontAttributeName: boldFont], range: range)
Run Code Online (Sandbox Code Playgroud) 在我的iOS应用程序中,我使用NSAttributedString生成项目符号列表.不幸的是,我正努力让子弹看起来很漂亮.我的第一次尝试是使用常规文本和子弹的unicode字符,基本上使用这样的字符串:
var attributedString = NSMutableAttributedString(
string: "Here is a list of bullets and a paragraph introducing them, note that this paragraph spans multiple lines\n" +
"• This is the first bullet\n" +
"• Here is a second bullet\n" +
"• And here is a third bullet with a lot of text such that it overflows to the next line"
)
Run Code Online (Sandbox Code Playgroud)
结果如下:
我喜欢子弹看起来如何,但是最后一个子弹中的溢出文本应该与之前的行对齐,我无法弄清楚如何用纯文本实现(没有对上面的段落应用相同的对齐).
我的第二次尝试是通过NSHTMLTextDocumentType在NSAttributedString中使用html,并使用<ul>和<li>元素生成项目符号.
let content = "Here is a list of bullets and a paragraph introducing …Run Code Online (Sandbox Code Playgroud) 我正在从Wordpress API获取HTML字符串并将其解析为Attributed String以在我的应用程序中显示它.由于字符串有自己的样式,它会显示不同的字体和大小,这会影响我们的设计选择.
我想要做的是在整个属性字符串上更改字体及其大小.
我尝试在属性字符串的选项中这样做,但它什么也没做:
let attributedT = try! NSAttributedString(
data: nContent!.decodeHTML().data(using: String.Encoding.unicode, allowLossyConversion: true)!,
options: [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSFontAttributeName: UIFont(name: "Helvetica", size: 16.0)!],
documentAttributes: nil)
contentLbl.attributedText = attributedT
Run Code Online (Sandbox Code Playgroud)
有没有人对如何实现这一点有任何想法?
PS我知道我可以在字符串的开头或结尾添加一个CSS标签,但这会覆盖其中的其他样式吗?此外,如果这是一个有效的解决方案,您能否提供一个如何做的样本?
我有像这样的"@Mervin测试员"的表情符号的AttributedString
现在我需要在这个属性String中找到一系列Mervin.
let attributedString = NSMutableAttributedString(string: " @Mervin tester ")
let range = // range for "Mervin" in above String.
Run Code Online (Sandbox Code Playgroud)
谢谢.
ios ×7
swift ×5
objective-c ×3
iphone ×2
nsrange ×2
cocoa-touch ×1
colors ×1
mutable ×1
nsdictionary ×1
range ×1
uilabel ×1
uitableview ×1
uitextview ×1