标签: nsattributedstring

将NSAttributedString复制到粘贴板

Cocoa的新手,我正在试图弄清楚如何将NSAttributedString复制到粘贴板.我查看了文档并且不确定我是否应该使用NSPasteboardItem.

这是我必须复制常规NSString:

NSPasteboard *pb = [NSPasteboard generalPasteboard];
NSArray *types = [NSArray arrayWithObjects:NSStringPboardType, nil];
[pb declareTypes:types owner:self];

[pb setString:@"asdfasdf" forType:NSStringPboardType];
Run Code Online (Sandbox Code Playgroud)

如何设置NSAttributedString?

谢谢

cocoa pasteboard nsattributedstring

7
推荐指数
2
解决办法
3010
查看次数

将NSAttributedString转换为字符串并返回

我有NSString,我需要制作NSAttributedString.

NSString是这样的:

bvcx b vcxbcvx bcxvbcxv bvx xbc bcvx bxcv bcxv bcxv bcxv bcvx bcvx bcxvbcvx bvc bcvx bxcv{
NSFont = "\"LucidaGrande 24.00 pt. P [] (0x108768a80) fobj=0x108788880, spc=7.59\"";
NSParagraphStyle = "Alignment 4, LineSpacing 0, ParagraphSpacing 0, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 0/0, LineHeightMultiple 0, LineBreakMode 0, Tabs (\n    28L,\n    56L,\n    84L,\n    112L,\n    140L,\n    168L,\n    196L,\n    224L,\n    252L,\n    280L,\n    308L,\n    336L\n), DefaultTabInterval 0, Blocks (null), Lists (null), BaseWritingDirection -1, HyphenationFactor 0, TighteningFactor 0.05, HeaderLevel 0";
} …
Run Code Online (Sandbox Code Playgroud)

cocoa objective-c nsstring nsattributedstring

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

NSString:去掉<b> </ b>并为该段制作一个带颜色的属性字符串?

如果我有一个字符串:

这是<b>简单</ b>字符串.

我需要摆脱<b>,(抱歉b和尖括号之间没有空格,由于某些原因预览没有显示),也让"简单"这个词变为粗体,我的想法是:

  1. 用空的空间替换尖括号和br
  2. 使'简单'段具有属性

问题是一旦标签被删除,我仍然需要知道单词的位置,我是否首先记住'简单'的位置,删除后,位置-4应该是'简单'的新位置?有没有更好的方法?甚至将html标签转换为属性?

谢谢

编辑:应该是b而不是br

objective-c nsstring nsattributedstring ios

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

NSAttributedString到NSData没有'NSAttributedString'的可见@interface声明选择器'RTFDFromRange:documentAttributes:

我正在尝试转换NSAttributedString为NSData使用RTFDFromRange方法.得到这个:

No visible @interface for 'NSAttributedString' declares the selector 'RTFDFromRange:documentAttributes:
Run Code Online (Sandbox Code Playgroud)

我的代码出了什么问题?

NSAttributedString *val=self.textview.attributedText;
    NSData *data = [val RTFDFromRange:NSMakeRange(0, self.textview.text.length) documentAttributes:nil];
Run Code Online (Sandbox Code Playgroud)

nsattributedstring nsdata ios

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

如何从Storyboard/Xib设置UILabel的字母间距?

我知道有一些方法可以调整字母间距,如果我参考了UILabel,但有没有办法从Storyboard中做到这一点?

我没有看到这里的选项:

在此输入图像描述

nsattributedstring uilabel ios

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

在iOS 8下,NSAttributedString性能更差

在iOS 8(和8.1 beta)下,创建NSAttributedString的性能远远低于7(2-3x).如果您在同一视图上使用多个实例,则这一点尤其明显,加载4个不同的标签将导致用户点击并显示新视图时延迟超过一秒.

不幸的是,你甚至无法把它扔进另一个线程,因为它在幕后使用WebKit.我向Apple提交了一个错误,但我需要有关变通方法或更好的实现方法的想法.

在viewDidLoad中:

self.labelOne.attributedText = [[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUTF8StringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType }
                                                     documentAttributes:nil
                                                                  error:&error];
Run Code Online (Sandbox Code Playgroud)

快速示例项目:https://github.com/BenSS/AttributedStringTest

更新:
iOS9再次改进了东西,因此速度并没有完全破坏UI交互.不幸的是,它还没有像iOS7那样快.(用演示自己测试!)

objective-c nsattributedstring ios ios8 ios9

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

带有选项卡的NSAttributedString

你如何UILabel使用这种文本格式创建?你会使用NSAttributedString吗?

在此输入图像描述

objective-c nsattributedstring ios

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

在UITextView上替换属性文本时保持自定义属性

我有一个可编辑的UITextView,它加载了一个属性String.此属性String加载了应用程序设置的自定义属性.出于这个原因,我总是将这些自定义属性添加到typingAttributestextView.

每当我打字它按预期工作文本,所加入的文字具有自定义属性集,但每当我使用的预测建议更换一个字,整个单词的设置没有自定义属性.

据我所知,每次以编程方式设置属性文本时,都会typingAttributes自动清除.因此,当选择预测建议时,文本会以编程方式设置,因此我丢失了所有自定义属性.

有没有办法解决?

uitextview nsattributedstring ios swift

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

NS在没有挂起UI线程的情况下从html派生字符串

我试图将一些HTML文本加载到NSAttributedString中,我看到UI挂起.

环顾四周我不确定这是否可能,因为它可能必须在主线程上运行:后台线程中来自HTML的NSAttributedString

在swift 3 iOS 10中,我能够毫无例外地运行它

let myString = // some html content
DispatchQueue.global(qos: .background).async { 
    let data = myString.data(using: .utf8)
    let options: [String: Any] = [
        NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType,
        NSCharacterEncodingDocumentAttribute: String.Encoding.utf8.rawValue
    ]

    do {
        let attributedString = try NSAttributedString(data: data!, options: options, documentAttributes: nil)

        DispatchQueue.main.async {
            () -> Void in
            self.label.attributedText = attributedString
        }
    } catch let error as NSError {
        print(error.localizedDescription)
    }
}
Run Code Online (Sandbox Code Playgroud)

几件事.

  1. 不知道为什么它不崩溃.但我怀疑它仍然在主线程上运行,因为UI仍然会锁定几秒钟.

  2. html中有图像.想知道是否是造成大部分延误的原因.我仍然希望显示图像,但想知道我是否可以将它们拉出来,只是最初显示文本并在后台线程上加载图像.不确定NSAttributedString内置了什么内容来提取图像,或者我必须手动解析它们.

是否有任何方法可以在后台线程上加载此数据,因此在使用使用html数据初始化的NSAttributedString时,我无法锁定UI?

html nsattributedstring grand-central-dispatch swift

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

NSTextAttachment周围的间距

如何在NSTextAttachments周围设置间距,如下例所示?

在示例中,无间距是我将NSTextAttachment附加到NSAttributedString时的默认行为.

在此输入图像描述

uitextview nsattributedstring nstextattachment swift3

7
推荐指数
3
解决办法
832
查看次数