标签: nsattributedstring

NSTextField 与带有附件的 NSAttributedString 的奇怪行为

当我通过 NSAttributedString 将图像附件放置在 NSTextField 中时,当我单击它或更改它时,图像会消失。怎么了?

NSTextAttachment* attachment = [[NSTextAttachment alloc] init];
NSImage *symbolImage = [NSImage imageNamed:@"enabled.png"];
NSLog(@"%@", symbolImage);
NSTextAttachmentCell *anAttachmentCell = [[NSTextAttachmentCell
                                           alloc] initImageCell:symbolImage];
[attachment setAttachmentCell:anAttachmentCell];
return [NSAttributedString attributedStringWithAttachment:attachment];
Run Code Online (Sandbox Code Playgroud)

macos cocoa objective-c nsattributedstring

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

将 attributeText 分配给 UILabel 会导致应用程序崩溃,并显示“NSConcreteMutableAttributedString initWithString:: nil value”

这是我的 tableViewCell 代码的实现,我在这里所做的只是从 MainViewController 获取数据并将其分配给两个不同的 UILabel。

问题出在 UILabel *subTitle 中,当我使用 attributeText 分配它以传递所需的行距时,应用程序在滚动 tableView 后崩溃。但如果我简单地用 .text = 分配它,它就可以正常运行,不会崩溃。

@property (strong, nonatomic) IBOutlet UILabel *title;
@property (strong, nonatomic) IBOutlet UILabel *subTitle;

- (void)configureWithPosts:(Data *)data {

  self.title.text = data.title;

  NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:data.excerpt];
  NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
  [attributeString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [attributeString length])];
  [paragraphStyle setLineSpacing:3];

  self.subTitle.textColor = [UIColor colorWithRed:102.0f/255.0f green:102.0f/255.0f blue:102.0f/255.0f alpha:1.0];
  self.subTitle.attributedText = attributeString;

}
Run Code Online (Sandbox Code Playgroud)

谁能告诉我我在这里做错了什么?

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'NSConcreteMutableAttributedString initWithString:: nil …
Run Code Online (Sandbox Code Playgroud)

objective-c uitableview nsattributedstring ios

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

如何检测 NSAttributedString 是否包含 NSTextAttachment 并将其删除?

我收到作为输入的 a NSAttributedString,其中可能包含附加为 的图像NSTextAttachment。我需要检查是否真的附加了这样的图像,在这种情况下,将其删除。我一直在寻找相关的帖子但没有成功,我该怎么做?

编辑:我正在尝试这个:

let mutableAttrStr = NSMutableAttributedString(attributedString: textView.attributedText)
textView.attributedText.enumerateAttribute(NSAttachmentAttributeName, in: NSMakeRange(0, textView.attributedText.length), options: NSAttributedString.EnumerationOptions(rawValue: 0)) { (value, range, stop) in

            if (value as? NSTextAttachment) != nil {
                mutableAttrStr.replaceCharacters(in: range, with: NSAttributedString(string: ""))
            }
        }
Run Code Online (Sandbox Code Playgroud)

如果textView.attributedText包含多个附件(我\u{ef}在它的 中看到多个附件string),我希望枚举if (value as? NSTextAttachment) != nil多次匹配条件,但该代码块只执行一次。

如何删除所有附件?

nsattributedstring nstextattachment ios swift

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

UILabel.attributedText 设置属性一次,然后仅更改文本(字符串)

如何仅设置 UILabel 文本属性一次,然后仅更改文本(字符串)

mTextValue.attributedText = NSAttributedString(string: "STRING", 
       attributes: 
           [NSAttributedStringKey.strokeWidth: -3.0,      
            NSAttributedStringKey.strokeColor: UIColor.black])


mTextValue.text = "NEW STRING" // won't change anything
Run Code Online (Sandbox Code Playgroud)

或者要设置新字符串,我必须一次又一次地NSAttributedString设置吗?.attributedText

nsattributedstring uilabel ios swift

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

使用 attributeText 在 UITextView 中实现撤消和重做

我正在尝试将撤消和重做功能添加到我的UITextView. 我使用的attributedText不仅仅是text的属性UITextView。我已经尝试使用Apple 文档undoManager中引用的函数调用,但是似乎没有任何反应。我很惊讶我无法通过谷歌搜索找到任何有关该主题的信息。有没有人在 / 上实现撤消和重做之前遇到过这个问题/知道如何解决这个问题?UITextViewattributedText

示例代码

textView.attributedText = NSMutableAttributedString(string: "SOME TEXT")

@objc func undo(_ sender: UIButton) {
    textView.undoManager?.undo()
}

@objc func redo(_ sender: UIButton) {
    textView.undoManager?.redo()
}
Run Code Online (Sandbox Code Playgroud)

undo redo uitextview nsattributedstring swift

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

属性字符串仅更改字体大小,而不更改字体名称

我正在尝试将属性文本设置为标签。但是,我想更改大小但不想设置字体名称,该怎么办?

let myAttributedString = NSMutableAttributedString(string: "Some Text",
 attributes: [NSFontAttributeName:UIFont(name: "Georgia",size: 18.0)!])
Run Code Online (Sandbox Code Playgroud)

nsattributedstring ios swift

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

NSMutableAttributedString:如何检索一定范围内的属性

我建立了一个简单的游乐场来演示我在检索属性字符串的属性时遇到的问题。也许我不明白如何定义范围:也许我错过了其他东西。

我定义了一个 NSMutableAttributedString,它有两种颜色,并将字体更改为粗体(蓝色)和斜体(红色):

var someText =  NSMutableAttributedString()

let blueAttri : [NSAttributedString.Key: Any] = [NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 12), NSAttributedString.Key.foregroundColor : UIColor.blue]
let redAttri : [NSAttributedString.Key: Any] = [NSAttributedString.Key.font: UIFont.italicSystemFont(ofSize: 12), NSAttributedString.Key.foregroundColor : UIColor.red]

someText = NSMutableAttributedString(string: "Some blue string here; ", attributes: blueAttri)
someText.append(NSMutableAttributedString(string: "Some red string here; ", attributes: redAttri))
Run Code Online (Sandbox Code Playgroud)

此时,字符串看起来很好,显示两种颜色的文本。

然后我定义了 3 个范围(尝试不同的方法来定义范围。)

var range1 = NSRange()
var range2 = NSRange(location: 0, length: someText.length)
var range3 = NSRange(location: 40, length: 44)
Run Code Online (Sandbox Code Playgroud)

最后,我尝试检索文本的属性

// retrieve attributes
let attributes1 = someText.attributes(at: 0, …
Run Code Online (Sandbox Code Playgroud)

nsattributedstring swift

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

主线程上来自 HTML 的 NSAttributedString 表现得好像多线程

我正在将一些 HTML 转换NSAttributedString为主线程上的an (Apple 告诉您的方式)。这需要一些时间,然后它会继续执行块的其余部分。

现在,如果另一个块也排队在主线程中运行(例如在从 HTTP 请求获得响应之后),我希望它在其他所有事情都完成,但事实并非如此:它们并行运行,好像他们在不同的线程上。我确实在各处放置了断言,以确保它在主线程上。

我做了一个实验“单视图应用程序”项目来测试这个,文件包含一个很长的 html 字符串<p>lorem</p> ipsum <b>dolor</b> <i><u>sit</u> amet</i>和一个带有以下代码的视图控制器:

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        dispatchStuff()
        for _ in 0..<10 {
            // slowOperation()
            parseHTML()
        }
    }

    func dispatchStuff() {
        for i in 0..<10 {
            let wait = Double(i) * 0.2
            DispatchQueue.main.asyncAfter(deadline: .now() + wait) {
                assert(Thread.isMainThread, "not main thread!")
                print(" dispatched after \(wait) seconds")
            }
        }
    }

    // just loads a …
Run Code Online (Sandbox Code Playgroud)

html concurrency multithreading nsattributedstring swift

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

如何通过界面构建​​器在属性字符串中使用动态颜色?

根据在 iOS上实现暗模式,我们需要将 foregroundColor 属性设置为新标签颜色。这是如何使用界面构建器完成的?

我尝试使用“文本颜色”选项并将颜色设置为 Developer->labelColor,但这不起作用。

编辑:由于目前这是不可能的,我使用了这个解决方法:

override func viewDidLoad() {
    // Support Dark Mode
    if #available(iOS 13.0, *) {
        let attributes = NSMutableAttributedString(attributedString: textView.attributedText!)
        attributes.addAttribute(.foregroundColor, value: UIColor.label, range: NSRange(location: 0, length: attributes.mutableString.length))
        textView.attributedText = attributes
    }
}
Run Code Online (Sandbox Code Playgroud)

interface-builder nsattributedstring ios ios-darkmode

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

Swift 5.5 AttributedString 不显示在界面中

下面是 Swift 5.5 制作属性字符串的方法:

let s = "Hello"
var attrib = AttributedString(s)
if let range = attrib.range(of: "ell") {
    attrib[range].foregroundColor = .red
    self.label.attributedText = NSAttributedString(attrib)
}
Run Code Online (Sandbox Code Playgroud)

问题是标签没有显示任何红色字符。事实上,我可以打印self.label.attributedText,并且可以看到红色属性甚至没有进入 NSAttributedString。这是怎么回事?

nsattributedstring swift ios15

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