当我通过 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) 这是我的 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) 我收到作为输入的 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多次匹配条件,但该代码块只执行一次。
如何删除所有附件?
如何仅设置 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
我正在尝试将撤消和重做功能添加到我的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) 我正在尝试将属性文本设置为标签。但是,我想更改大小但不想设置字体名称,该怎么办?
let myAttributedString = NSMutableAttributedString(string: "Some Text",
attributes: [NSFontAttributeName:UIFont(name: "Georgia",size: 18.0)!])
Run Code Online (Sandbox Code Playgroud) 我建立了一个简单的游乐场来演示我在检索属性字符串的属性时遇到的问题。也许我不明白如何定义范围:也许我错过了其他东西。
我定义了一个 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) 我正在将一些 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) 根据在 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) 下面是 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。这是怎么回事?
swift ×7
ios ×5
objective-c ×2
cocoa ×1
concurrency ×1
html ×1
ios-darkmode ×1
ios15 ×1
macos ×1
redo ×1
uilabel ×1
uitableview ×1
uitextview ×1
undo ×1