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?
谢谢
我在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)
我希望在显示之前和之后都一样.如何显示多行?
使用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)
这应该有用吗?
如果我有一个字符串:
这是<b>简单</ b>字符串.
我需要摆脱<b>,(抱歉b和尖括号之间没有空格,由于某些原因预览没有显示),也让"简单"这个词变为粗体,我的想法是:
问题是一旦标签被删除,我仍然需要知道单词的位置,我是否首先记住'简单'的位置,删除后,位置-4应该是'简单'的新位置?有没有更好的方法?甚至将html标签转换为属性?
谢谢
编辑:应该是b而不是br
我正在使用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)
但它仍然从左向右流动:

我错过了什么?
我有一个NSAttributedString,它由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)
几件事.
不知道为什么它不崩溃.但我怀疑它仍然在主线程上运行,因为UI仍然会锁定几秒钟.
html中有图像.想知道是否是造成大部分延误的原因.我仍然希望显示图像,但想知道我是否可以将它们拉出来,只是最初显示文本并在后台线程上加载图像.不确定NSAttributedString内置了什么内容来提取图像,或者我必须手动解析它们.
是否有任何方法可以在后台线程上加载此数据,因此在使用使用html数据初始化的NSAttributedString时,我无法锁定UI?
我正在进行扩展,将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相同的错误但我无法看到扩展需要什么?
谢谢
ios ×4
objective-c ×3
swift ×3
html ×2
swift3 ×2
uitextview ×2
cocoa ×1
cocoa-touch ×1
image ×1
ios6 ×1
multiline ×1
nsstring ×1
pasteboard ×1
uikit ×1
uilabel ×1