标签: nsattributedstring

如何复制 NSMutableAttributedString

我想知道如何才能复制一份NSMutableAttributedString. 我有一个名为的属性text,我想在某个时刻保存其内容并在发生某些情况时恢复到它。我尝试创建一个名为textCopywhere 我可以将其保存到的属性@property (nonatomic, copy),但是当我这样做时出现运行时错误:

-[NSConcreteAttributedString insertAttributedString:atIndex:]:发送到实例的无法识别的选择器。

我将如何实现这个目标?

每当我将 设为 时,我都会收到此NSMutableAttributedString消息@property (nonatomic, copy)。我不明白为什么这行不通。NSMutableAttributedString一般来说,无论我是否使用它的 setter 方法,复制参数似乎都不起作用。

objective-c nsattributedstring

1
推荐指数
1
解决办法
4568
查看次数

NSAttributedString drawInRect 当框架偏移时消失

我有一个非常奇怪的问题。当我偏移我用来绘制我的框架/矩形时NSAttributedString

我的文字消失了。有什么想法吗?这是简单偏移 40 像素的图像。

r.size = [self.text boundingRectWithSize:r.size
                                     options:NSStringDrawingUsesLineFragmentOrigin
                                     context:nil].size;
    r.origin.y +=40;
    [[UIColor orangeColor] setFill];
    CGContextFillRect(ctx, r);
    [self.text drawInRect:r];
Run Code Online (Sandbox Code Playgroud)

结果:

在此输入图像描述

这里是没有偏移的情况。 在此输入图像描述

如何在给定的偏移处绘制 AttributedText?

uikit nsattributedstring ios

1
推荐指数
1
解决办法
1608
查看次数

在iOS上将NSData转换为NSAttributedString

在我的应用程序中,我试图存档一个NSAttributedString对象,所以我NSData使用以下代码成功将其转换为对象:

NSData *attrStrData = [attrStr dataFromRange:NSMakeRange(0, attrStr.length) documentAttributes:NULL error:nil];
Run Code Online (Sandbox Code Playgroud)

现在我想将此NSData对象转换回来NSAttributedString.我相信OSX你有一些方法,但有没有iOS?我找到了NSAttributedString+Encoding,但似乎对它的支持较少,而且没有CocoaPods.欢迎任何建议.

objective-c nsattributedstring nsdata ios ios7

1
推荐指数
1
解决办法
2016
查看次数

改变UILabel的第一线颜色

我正在尝试改变UILabels第一行的颜色.由于某种原因它不起作用.这是我的代码:

NSMutableAttributedString *text = 
 [[NSMutableAttributedString alloc] 
   initWithAttributedString: label.attributedText];

[text addAttribute:NSForegroundColorAttributeName 
             value:[UIColor redColor] 
             range:[label.text rangeOfString:@"\n"];];
[label setAttributedText: text];
Run Code Online (Sandbox Code Playgroud)

我没有看到第一行有任何变化.

objective-c nsattributedstring uilabel ios

1
推荐指数
1
解决办法
397
查看次数

下划线覆盖 NSAttributedString 中的文本

我正在尝试创建一个属性字符串,但下划线覆盖了我的文本而不是出现在它后面:

在此处输入图片说明

有没有办法来解决这个问题?我正在使用以下代码:

let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineSpacing = 10.0

let attributes = [NSForegroundColorAttributeName: UIColor.white,
                  NSUnderlineStyleAttributeName: NSUnderlineStyle.styleThick.rawValue,
                  NSUnderlineColorAttributeName: UIColor.red,
                  NSParagraphStyleAttributeName: paragraphStyle]

let attributedString = NSAttributedString(string: "Story", attributes: attributes)
Run Code Online (Sandbox Code Playgroud)

谢谢!

编辑:

提供更多上下文:

我在放置在 .xib 文件中的 UILabel 上显示属性字符串:

view.textLabel.attributedText = attributedString
Run Code Online (Sandbox Code Playgroud)

标签的字体如下: System Bold 32.0

我在 iPhone 6 - iOS 10.3 模拟器上运行代码。

编辑2:

我应该提到标签在某些时候可能包含不止一行文本。这就是 numberOfLines 设置为 0 的原因。

编辑 3:如果有人遇到这个问题——似乎在 iOS 9 和 10 以及 UILabel 和 UITextView 上绘制下划线的方式有很大不同。我最终不得不通过继承 NSLayoutManager 自己绘制下划线。

nsattributedstring ios swift

1
推荐指数
1
解决办法
3479
查看次数

具有删除线的多行属性字符串

我有一个标签:

label.numberOfLines = 0
Run Code Online (Sandbox Code Playgroud)

而我正在努力使这个标签的文字删除:

let index: NSMutableAttributedString = NSMutableAttributedString(string: label.text!)
index.addAttributes([NSStrikethroughStyleAttributeName: NSUnderlineStyle.styleSingle.rawValue, NSStrikethroughColorAttributeName: UIColor.red], range: NSMakeRange(0, index.length))
label.textColor = UIColor.red
label.attributedText = index
Run Code Online (Sandbox Code Playgroud)

属性字符串是否适用于多行或使用numberOfLines设置为0的标签?如果是这样,如何使多行文字删除?

nsattributedstring uilabel ios swift swift3

1
推荐指数
1
解决办法
1911
查看次数

自定义 UITableViewCell 中带有属性文本的 UILabel:颜色在重用之前不会呈现(iOS10)

我有一个这样的方便扩展:

extension NSMutableAttributedString {

    func append(string: String, attributes: [String: Any]) {
        let attributed = NSMutableAttributedString(string: string)
        let range = NSRange(location: 0, length: string.utf16.count)
        for (attribute, value) in attributes {
            attributed.addAttribute(attribute, value: value, range: range)
        }
        append(attributed)
    }
}
Run Code Online (Sandbox Code Playgroud)

我这样在 UILabel 中设置文本样式:

let normalAttributes = [NSForegroundColorAttributeName: darkGray]
let lightAttributes = [NSForegroundColorAttributeName: lightGray]
let text = NSMutableAttributableString()
text.append("Part 1", attributes: normalAttributes)
text.append("Part 2", attributes: lightAttributes)
Run Code Online (Sandbox Code Playgroud)

所有这些都在自定义 UITableViewCell 类中。发生的事情是文本使用 NIB 文件中的基色渲染,而不是根据我的属性字符串使用自定义前景色 - 直到我执行某些导致单元格重用的操作(例如滚动),之后颜色突然正确渲染.

我似乎找不到对象的生命周期有什么问题,也没有其他地方会弄乱这个标签。标签 fwiw 是一个 IBOutlet,所以我不会每次都创建一个新的或任何奇怪的东西。

nsattributedstring ios swift

1
推荐指数
1
解决办法
1670
查看次数

比较Swift String和NSAttributedString

如何比较NSAttributedString和Swift字符串,例如:

let string = "test string"
let attributedString = NSAttributedString(string: string)
let areStringsEqual = attributedString == string
Run Code Online (Sandbox Code Playgroud)

将编译。

equality nsattributedstring swift

1
推荐指数
1
解决办法
548
查看次数

How to get NSAttributedString's underline style attribute to work?

Using NSAttributedString, I would like to underline a portion of a string. The following code does not work as expected:

let string = NSMutableAttributedString(string: "This is my string.")

string.addAttributes([.underlineStyle : NSUnderlineStyle.single], 
                           range: NSRange(location: 5, length: 2))
Run Code Online (Sandbox Code Playgroud)

One expects "is" to be underlined. Instead, the UITextView layout engine ignores the attribute, and generates a warning in the console. Unhelpfully, the warning makes no mention of underlineStyle or NSUnderlineStyle.

这个问题很常见。StackOverflow上有很多示例显示了正确的用法。但是,虽然我确定必须有一些答案可以解释该问题,但我已阅读的答案仅显示了用法的不透明示例。

提出这个问题(并在下面给出答案)是为了纪念对该问题的解释,以便将来我和其他人可以从中学习。

nsattributedstring ios swift

1
推荐指数
1
解决办法
456
查看次数

如何使用 UIStackView 中的属性文本删除 UILabel 的底部填充

我想在 UIStackview 中删除带有属性文本的 UILabel 的底部填充。

在此处输入图片说明

我找到了这个解决方案How to remove the extra padding under a one line UILabel。这适用于普通文本,但不适用于属性文本。

   let textLabel = UILabel()
        textLabel.translatesAutoresizingMaskIntoConstraints = false
        textLabel.text = "What is a chemical property and how can you observe it?"
        textLabel.numberOfLines = 0
        textLabel.lineBreakMode = .byWordWrapping
        textLabel.backgroundColor = .lightGray
        mainStackView.addArrangedSubview(textLabel)

        let textLabel2 = UILabel()
        textLabel2.translatesAutoresizingMaskIntoConstraints = false

        let html = "<html lang=\"en\"><head><meta charset=\"UTF-8\"></head><body><div style=\"font-size:36;\"><p>What is a <em>chemical property</em> and how can you observe it?</p></div></body></html>"

        let data = Data(html.utf8)

        if let …
Run Code Online (Sandbox Code Playgroud)

nsattributedstring uilabel ios uistackview swift3

1
推荐指数
1
解决办法
457
查看次数