标签: 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多行

我在UILabel工作过.但是不推荐使用setLineBreakMode.我一直在使用NSAttributedString.但UILabel setLineBreakMode是之后UILabel setNumberOfLines else不起作用.

之前:

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(42.0f, 10.0f, 275.0f, 50.0f)];
label.text = @"XXXXXX";
memoLabel.textAlignment = UITextAlignmentLeft;
memoLabel.numberOfLines = 2;
memoLabel.lineBreakMode = UILineBreakModeTailTruncation;
memoLabel.font = [UIFont systemFontOfSize:11];
memoLabel.backgroundColor = [UIColor clearColor];
Run Code Online (Sandbox Code Playgroud)

IOS 6之后:

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
paragraphStyle.alignment = NSTextAlignmentLeft;
NSAttributedString *string
    = [[NSAttributedString alloc] initWithString:text
                                  attributes:[NSDictionary
                                              dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:11],
                                              NSFontAttributeName,
                                              paragraphStyle, NSParagraphStyleAttributeName,nil]];
[paragraphStyle release];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(42.0f, 10.0f, 275.0f, 50.0f)];
label.attributedText = string;
[string relase];
Run Code Online (Sandbox Code Playgroud)

我希望在显示之前和之后都一样.如何显示多行?

objective-c multiline nsattributedstring uilabel ios6

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

UITextView与NSAttributedString和自定义属性不起作用

使用UITextView时,我尝试将自定义属性添加到属性字符串.但是,在分配到UITextView的attributedText后,所有自定义键都将丢失.像这样:

NSMutableAttributedString *lString = [[ NSMutableAttributedString alloc ] initWithString: @"astring"
                                             attributes: @{ @"customkey": @"customvalue" }];
NSLog(@"string: %@", lString);  // shows customkey present
textView.attributedText = lString;
NSLog(@"result: %@", self.textView.attributedText);   // shows customkey missing
Run Code Online (Sandbox Code Playgroud)

这应该有用吗?

cocoa-touch uitextview nsattributedstring ios

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

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

如果我有一个字符串:

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

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

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

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

谢谢

编辑:应该是b而不是br

objective-c nsstring nsattributedstring ios

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

NSMutableParagraphStyle忽略了NSWritingDirectionNatural,默认为LTR为阿拉伯语文本

我正在使用NSAttributedString UIKit Additions在UIView中绘制属性字符串.我的问题是,尽管使用的价值NSWritingDirectionNatural为baseWritingDirection我的段落样式属性,文本始终默认为左到右.

这是我如何形成属性字符串(简化):

NSString *arabic = @"??????? ??????? ???????? ?????? ????? ??????? ?????? ?????. ???? ??? ????? ???? ?????? ?????? ???????? ????? ???? ??? ??? ?? ?????? ?????? ???? ?????? ??? ?????????? ???????? ???????";

NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
paragraph.baseWritingDirection = NSWritingDirectionNatural;
paragraph.lineBreakMode = NSLineBreakByWordWrapping;

NSMutableDictionary *attributes = [[NSMutableDictionary alloc] init];
attributes[NSParagraphStyleAttributeName] = paragraph;

NSAttributedString *string = [[NSAttributedString alloc] 
                             initWithString:arabic 
                             attributes:attributes];
Run Code Online (Sandbox Code Playgroud)

以下是我绘制文字的方式:

- (void)drawRect:(CGRect)rect {
    [self.attributedText drawWithRect:rect 
                              options:NSStringDrawingUsesLineFragmentOrigin 
                              context:nil];
}
Run Code Online (Sandbox Code Playgroud)

但它仍然从左向右流动:

截图

我错过了什么?

uikit nsattributedstring right-to-left

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

NSAttributedString:将图像适合容器

我有一个NSAttributedString,它由HTML构成,它显示一些图像.问题是图像比容器大,我想知道如何使它们适合它.

谢谢你的帮助

html image nsattributedstring ios swift

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

带有选项卡的NSAttributedString

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

在此输入图像描述

objective-c nsattributedstring ios

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

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
查看次数

在扩展文件中使用未解析的标识符错误?

我正在进行扩展,将html转换为属性字符串,代码为

extension String {
var htmlToAttributedString: NSAttributedString? {
    guard let data = data(using: .utf8) else { return nil }
    do {
        return try NSAttributedString(data: data, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: String.Encoding.utf8.rawValue], documentAttributes: nil)
    } catch let error as NSError {
        print(error.localizedDescription)
        return  nil
    }
}
var html2String: String {
    return htmlToAttributedString?.string ?? ""
}
Run Code Online (Sandbox Code Playgroud)

我收到以下3个相同的错误

使用未解析的标识符'NSDocumentTypeDocumentAttribute'

使用未解析的标识符'NSHTMLTextDocumentType'

使用未解析的标识符'NSCharacterEncodingDocumentAttribute'

我假设我犯了一个错误的语法导致3相同的错误但我无法看到扩展需要什么?

谢谢

nsattributedstring swift swift3

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

NSTextAttachment周围的间距

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

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

在此输入图像描述

uitextview nsattributedstring nstextattachment swift3

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