标签: nsattributedstring

NSTextField 添加行距

我使用 NSTextField 而不是 NSTextView 来接收用户输入,但我需要自定义字体、文本颜色和行距。我使用下面的代码,字体和颜色都可以,但我不知道如何设置行距。

[self.titleField setTextColor:textColor];
[self.titleField setFont:bold14];
Run Code Online (Sandbox Code Playgroud)

我还使用 NSAttributedString 来解决问题:

NSFont *bold14 = [NSFont boldSystemFontOfSize:14.0];
NSColor *textColor = [NSColor redColor];
NSMutableParagraphStyle *textParagraph = [[NSMutableParagraphStyle alloc] init];
[textParagraph setLineSpacing:10.0];

NSDictionary *attrDic = [NSDictionary dictionaryWithObjectsAndKeys:bold14, NSFontAttributeName, textColor, NSForegroundColorAttributeName, textParagraph, NSParagraphStyleAttributeName, nil];
NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:title attributes:attrDic]; 
[self.titleField setAllowsEditingTextAttributes:YES];
[self.titleField setAttributedStringValue:attrString];
Run Code Online (Sandbox Code Playgroud)

上面的代码可以显示属性字符串,但是当我删除文本字段中的字符串并开始输入时,这些单词没有任何属性。

如何在 NSTextField 中输入具有自定义字体、颜色和行距的字符串?

cocoa objective-c nstextfield nsattributedstring

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

如果我同时设置 UILabel.text 和 UILabel.attributedText 会发生什么?

我有一个使用 HTML 制作的属性字符串DTCoreText,我想将其设置为 UILabel 的值。但是,我想稍后检查与 UILabel 相比,字符串的值是否已更改。是否可以同时设置 UILabeltext和attributedText属性?是否会attributedText简单地掩盖text属性,以便text属性可以保留为内部值?

nsstring nsattributedstring uilabel core-text ios

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

来自带有块引用的 html 的 NSAttributedString

我有一些简单的 HTML,带有一个 blockquote 标签,我想在 a 中显示UILabel(它位于 内部UITableViewCell,所以我不认为 usingUIWebView是解决我的问题的方法)。

我决定将其转换为NSAttributedString并且一切正常,但我收到的是引用的文本,看起来与未引用的文本完全相同(没有缩进,没有块标记)。

我想问是否有什么方法可以实现类似于这里提到的现代引用的东西?或者至少在每行段落之前>使用一些缩进和前缀(例如老式电子邮件引用)NSAttributedString?

html objective-c nsattributedstring ios

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

iOS 中的 NSTextList

iOS 上有 NSTextList 的等效项吗?

\n\n

在 OSX 上我可以做

\n\n
NSTextList *list = [[NSTextList alloc] initWithMarkerFormat:@"square" options:0];\nNSMutableParagraphStyle *paragraph = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];\n[paragraph setTextLists:[NSArray arrayWithObject:list]];\nNSTextTab *t1 = [[NSTextTab alloc] initWithType:0 location:11.0f];\nNSTextTab *t2 = [[NSTextTab alloc] initWithType:0 location:36.0f];\n[paragraph setTabStops:[NSArray arrayWithObjects:t1, t2, nil]];\nNSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:paragraph, NSParagraphStyleAttributeName, nil];\nNSString *string = @"\\t\xe2\x96\xaa\\tList item 1\\n\\t\xe2\x96\xaa\\tList item 2\\n\\t\xe2\x96\xaa\\tList item 3";\nNSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:string attributes:attributes];\n
Run Code Online (Sandbox Code Playgroud)\n\n

但 iOS 没有替代方案,因为所有解决方案都无法正常运行

\n

cocoa objective-c nsattributedstring ios

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

在 Swift 中在 2 个标签上设置 attributeText 时出现内存泄漏

当我在视图中的 2 个标签上设置 attributeText 参数时,我看到仪器报告了奇怪的内存泄漏。

如果我只在一个标签上设置属性文本,则不会出现内存泄漏。此外,只有当标签是多行标签时,这似乎才会泄漏。

为了复制这一点,我在故事板视图控制器上放置了 2 个标签,连接了 IBOutlet,并使用了以下代码:

class ViewController: UIViewController
{
    @IBOutlet weak var label: UILabel!
    @IBOutlet weak var label2: UILabel!

    override func viewDidLoad()
    {
        super.viewDidLoad()

        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.alignment = .Natural
        paragraphStyle.lineHeightMultiple = 1.0
        paragraphStyle.lineBreakMode = .ByWordWrapping

        let attributes = [
            NSFontAttributeName: UIFont(name: "HelveticaNeue-Light", size: 15.0)!,
            NSForegroundColorAttributeName: UIColor.blackColor(),
            NSKernAttributeName: 0.4,
            NSParagraphStyleAttributeName: paragraphStyle,
            NSUnderlineStyleAttributeName: NSUnderlineStyle.StyleNone.rawValue
        ]

        self.label.attributedText = NSAttributedString(string: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore …
Run Code Online (Sandbox Code Playgroud)

memory-leaks nsattributedstring uilabel ios swift

5
推荐指数
0
解决办法
802
查看次数

iOS 上的 NSAttributedString 阴影和描边?

当我使用描边和阴影时,我会得到某种双描。我怎样才能解决这个问题?

游乐场代码:

import UIKit

var shadow = NSShadow()
shadow.shadowColor = UIColor.black
shadow.shadowOffset = CGSize(width: 0, height: 3)

class CustomLabel: UILabel {
    override func drawText(in rect: CGRect) {
        let attributes: [String: Any] = [NSStrokeWidthAttributeName: -2.0,
                          NSStrokeColorAttributeName: UIColor.black,
                          NSForegroundColorAttributeName: UIColor.white,
                          NSShadowAttributeName: shadow,
                          NSFontAttributeName: UIFont(name: "AvenirNext-Bold", size: 50)]
        self.attributedText = NSAttributedString(string: self.text ?? "", attributes: attributes)
        super.drawText(in: rect)
    }
}

let label = CustomLabel(frame: CGRect(x: 0, y: 0, width: 200, height: 100))
label.backgroundColor = UIColor.orange
label.text = "Hello"
Run Code Online (Sandbox Code Playgroud)

结果:

在此处输入图片说明

nsattributedstring ios swift

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

NSAttributedString 在初始化时崩溃,但为什么呢?

有时这件事会崩溃,但我不知道为什么以及何时崩溃。有人有想法吗?

extension String {
var htmlDecoded: String? {

    if let encodedData = self.data(using: String.Encoding.utf8) as Data? {
        let attributedOptions = [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
                                  NSCharacterEncodingDocumentAttribute: String.Encoding.utf8.rawValue] as [String : Any]
        do {
            let attributedString = try NSAttributedString(data: encodedData,
                                                          options: attributedOptions,
                                                          documentAttributes: nil)
            return attributedString.string

        } catch let error as NSError {
            print("ERROR: ", error.localizedDescription)

            return self
        }
    }
    return self
}
}
Run Code Online (Sandbox Code Playgroud)

这是我从 HockeyApp 得到的错误

function signature specialization <Arg[0] = Owned To Guaranteed and Exploded> of @nonobjc (extension in UIKit):__C.NSAttributedString.init(data: Foundation.Data, …
Run Code Online (Sandbox Code Playgroud)

encoding nsattributedstring swift

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

iOS - 在 Localizable.strings 中用粗体字符串强调

有没有办法像这样在 Localizable 文件中加粗一些单词?

"Pending network connection" = "<b>Pending</b> network connection";
Run Code Online (Sandbox Code Playgroud)

我在里面有这个字符串,我只想强调某些单词:

 "camSave" = "To complete onboarding:\n1. Tap Save and disconnect to disconnect from the camera.\n2. Connect to the internet.\n3. Log in to Cloud.";
Run Code Online (Sandbox Code Playgroud)

顺便说一句,我想在警报中使用它

localization nsattributedstring localizable.strings ios swift

5
推荐指数
2
解决办法
1711
查看次数

如何使用 NSLayoutManager 用自定义绘图替换文本附件?

我的最终目标是让用户能够将文本拆分为一个NSTextView或UITextView多个部分,其中两个后续部分通过自定义视图分隔符在视觉上分开(可能,但不一定是水平规则 - 我希望能够调整分离器的视觉外观没有麻烦)。

分节符分隔符的示例屏幕截图

我需要一个适用于两个平台的解决方案:macOS 和 iOS。

(我的相关问题集中在 macOS 的解决方案上,并且有一个使用NSTextAttachmentCells 在 iOS 上不可用。)

我目前解决这个问题的方法如下:

  1. 我在窗口的工具栏中有一个按钮来插入分节符。
  2. 当用户点击该按钮时,我会创建一个没有文本但带有文本附件的新属性字符串:

    let attachment = SectionChangeTextAttachment()
    let attachmentString = NSAttributedString(attachment: attachment)
    
    Run Code Online (Sandbox Code Playgroud)

    (SectionChangeTextAttachment是NSTextAttachment我创建的自定义子类,以便能够将此分节符附件与其他可能的附件区分开来。)

  3. 我将其插入attachmentString到当前光标位置的文本存储中:

    textStorage.insert(attachmentString, at: textView.selectedRange().location)
    
    Run Code Online (Sandbox Code Playgroud)
  4. 我创建了一个自定义NSLayoutManager子类。它的任务是找到类的所有附件,SectionChangeTextAttachment并用分隔符图(或字形?)替换所有出现的地方。这就是问题所在:由于 Apple 有限且部分过时的文档,我无法弄清楚如何执行此操作。

所以我的问题是:

如何让我的布局管理器用比字符/字形占用更多空间的自定义图形(分隔符)替换附件字符(带有特定附件)?

(我猜附件字符没有字形,因此布局管理器没有为它提供任何空间。)

我需要覆盖哪些方法才能使其工作?

(这种方法从一开始就有意义吗?)

nslayoutmanager nsattributedstring nstextattachment ios nstextstorage

5
推荐指数
0
解决办法
399
查看次数

如何在 SwiftUI 中将 NSAttributedString 与 ScrollView 一起使用?

我已经能够使NSAttributedStrings可通过UIViewRepresentable它,直到我换一个角度的伟大工程ScrollView。

当放置在 中时ScrollView,NSAttributedString视图停止渲染。

我已经尝试了一些其他的方法是替换NSAttributedString与添加多个Text()视图合力得到格式化其内部运作ScrollView和支持斜体和monospace font。不幸的是,这不适用于文本块内的链接,这意味着我仍然需要一个NSAttributedString.

import SwiftUI

struct TextWithAttributedString: UIViewRepresentable {

    var attributedString: NSAttributedString

    init(_ attributedString: NSAttributedString) {
        self.attributedString = attributedString
    }

    func makeUIView(context: Context) -> UITextView {
        let textView = UITextView(frame: .zero)
        textView.attributedText = self.attributedString
        textView.isEditable = false
        return textView
    }

    func updateUIView(_ textView: UITextView, context: Context) {
        textView.attributedText = self.attributedString
    }
}


let exampleText …
Run Code Online (Sandbox Code Playgroud)

uikit nsattributedstring swift swiftui

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