我有一个基本的NSTextView,启用了丰富的文本和图形(在IB中).我想得到的是拖入的任何图像的路径和文件名,所以我可以将它们传递给另一个类.
我是NSAttributedString的新手,但我有一个循环使用enumerateAttributesInRange:options:usingBlock:寻找NSAttachmentAttributeName,这一切都正常.但是深入一点,我进入了fileWrapper类,显然无法给出项目的路径.
我如何获得NSTextAttachment的名称和路径?
相关:是否有更简单的方法来获取它们然后单步执行属性?
非常感谢!
cocoa nstextview nsattributedstring nstextattachment nsfilewrapper
如何在iPhone上设置NSAtributedString的字体.我在网上找到了如何为mac做这件事,但它不一样.当我试图将它转换到iOS平台时,它无法正常工作.我需要设置字体名称和字体大小.
NSDictionary *attributes = [[NSDictionary alloc] initWithObjectsAndKeys:
[UIFont fontWithName:@"Helvetica" size:26], kCTFontAttributeName,
[UIColor blackColor], kCTForegroundColorAttributeName, nil];
Run Code Online (Sandbox Code Playgroud) 好吧,我曾在几个地方搜索过,虽然有些人据称找到了修复,但它似乎并不适用于我的情况.
我正在尝试在程序上设置几个UItextviews的行高,如下所示:
UITextView *lab = [LocalTexts objectAtIndex:j];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineHeightMultiple = 50.0f;
paragraphStyle.maximumLineHeight = 50.0f;
paragraphStyle.minimumLineHeight = 50.0f;
NSString *string = lab.text;
NSDictionary *ats = @{
NSFontAttributeName : [UIFont fontWithName:@"DIN Medium" size:16.0f],
NSParagraphStyleAttributeName : paragraphStyle,
};
lab.attributedText = [[NSAttributedString alloc] initWithString:string attributes:ats];
Run Code Online (Sandbox Code Playgroud)
奇怪的是,如果我禁用NSFontAttributeName赋值,行高也会起作用,如果我将Paragraph样式设置为具有某个段落高度,它总是有效,所以NSParagraphStyleAttribute不会被完全忽略.我不知道这是一个错误还是我实际上做错了什么.
我尝试将其实现为纯CORE TEXT,但它对于当前的项目范围来说有点过于复杂.
希望有人能指出我正确的方向.谢谢.
我很确定这实际上是一个UIKit错误,但想得到一些输入,看看我是否在这里遗漏了一些愚蠢的东西.
这是我的代码:
// single line with modified line spacing and 2 colors - broken, line spacing is added to the bottom!
UILabel *brokenLabel = [[UILabel alloc] init];
brokenLabel.backgroundColor = [UIColor greenColor];
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"Just some text"];
[attributedString addAttributes:@{NSForegroundColorAttributeName : [UIColor redColor]} range:[attributedString.string rangeOfString:@"text"]];
attributedString = attributedStringFromAttributedStringWithLineSpacing(attributedString, 20, NSTextAlignmentCenter);
brokenLabel.attributedText = attributedString;
[brokenLabel sizeToFit];
brokenLabel.frame = CGRectOffset(brokenLabel.frame, 50, 100);
[self.view addSubview:brokenLabel];
// end
// single line with modified line spacing and 1 color - correct
UILabel *workingLabel = …Run Code Online (Sandbox Code Playgroud) 我想在我的UINavigationBar中设置一个两行标签.当我执行以下操作时,它只显示一行(换行符之前的部分).
NSString *title = @"First line of title is bigger\nSecond line of title is smaller";
NSDictionary *attribute1 = @{NSForegroundColorAttributeName: [UIColor colorWithRed:148/255.0f green:147/255.0f blue:147/255.0f alpha:1],
NSFontAttributeName: [UIFont boldSystemFontOfSize: 14.0f]};
NSDictionary *attribute2 = @{NSForegroundColorAttributeName: [UIColor colorWithRed:148/255.0f green:147/255.0f blue:147/255.0f alpha:1],
NSFontAttributeName: [UIFont boldSystemFontOfSize: 10.0f]};
const NSRange line1Range = {0, 29};
const NSRange line2Range = {31, 30};
NSMutableAttributedString *attributedText =[[NSMutableAttributedString alloc] initWithString:title];
[attributedText setAttributes: attribute1 range:line1Range];
[attributedText setAttributes: attribute2 range:line2Range];
UILabel *label = [[UILabel alloc] init];
label.attributedText=attributedText;
self.navigationItem.titleView = label;
[self.navigationItem.titleView sizeToFit];
Run Code Online (Sandbox Code Playgroud)
为了比较,以下显示两行.以下仅供比较.顶级版本是我需要的.
UILabel …Run Code Online (Sandbox Code Playgroud) 我的报告中出现了各种崩溃,报告了以下原因:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'failed to generate newParagraphStyle'
Run Code Online (Sandbox Code Playgroud)
发生这种情况的应用确实以编程方式设置了很多属性文本(字体,颜色,有时是段落样式),但我找不到与此直接相关的任何模式.我所看到的几乎所有的报告都包含了堆栈跟踪中某种形式的setLineBreakMode的引用:
-[_UICascadingTextStorage(Accessors) setLineBreakMode:]
Run Code Online (Sandbox Code Playgroud)
我自己从来没有能够重新创建它,但它确实在我的崩溃报告中定期出现.我没有专门在这些地方设置换行模式.有任何想法吗?
编辑:我花了更多的时间研究这个,并找到了一个模式来重现.所以我有一个UITextField,当你点击键盘上的返回时,检测到"/ n"字符,文本字段将重新启动其第一个响应者状态.如果在此之后的任何时候,您尝试修改该字段的文本(或属性文本),它会崩溃.有一个关键元素,只有在设置了typingAttributes时才会发生.他们的目标并不重要.
我尝试了一切,在设置文本之前将typingAttributes设置为nil,设置一个固定的字符串以确保它不是我们设置的 - 没有运气.总是崩溃.一旦你进入这种状态,文本字段就会被打破.避免它的黑客就是吹走文本域并创建一个新文本 - 但就是这样.一个黑客.
一些更有趣的信息,代码检测返回键的方式写得不好 - 它检测到"/ n"并返回NO:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
Run Code Online (Sandbox Code Playgroud)
它没有使用适当的方法:
- (BOOL)textFieldShouldReturn:(UITextField *)textField
Run Code Online (Sandbox Code Playgroud)
在应用程序的一个地方,这实际上修复了崩溃,在其他地方,它总是使用正确的方法,崩溃仍在发生.唯一的解决方法是吹走文本域并创建一个新文本域.对我来说,这似乎是iOS本身的一个错误.
我使用下面的代码来创建警报样式的规则列表 UIAlertController
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = NSTextAlignment.Left
let messageText = NSMutableAttributedString(
string: "1. Do not bully or target other users.\n2. Do not post others' private information.\n3. Do not post useless or offensive content.\n4. Do not post others' copyrighted content.\n5. Downvote and Flag posts which violate these rules.\n6. If you violate these rules, your account may be suspended and/or your content removed.",
attributes: [
NSParagraphStyleAttributeName: paragraphStyle,
NSFontAttributeName : UIFont(name: "Lato-Regular", size: 13.0)!,
NSForegroundColorAttributeName : colorAccentWords
]
)
Run Code Online (Sandbox Code Playgroud)
它给了我警报视图,如下所示:

我想要的是使编号列表保持预期,以便每行的溢出文本将保持对齐.我怎样才能做到这一点?
我有一个方法将NSAttributedString设置为粗体:
func setBold(text: String) -> NSMutableAttributedString {
guard let font = UIFont.CustomNormalBoldItalic() else {
fatalError("font not found")
}
let string = NSMutableAttributedString(string:"\(text)", attributes: [NSFontAttributeName : font])
self.setAttributedString(string)
return self
}
Run Code Online (Sandbox Code Playgroud)
这就是它的调用方式,它正常工作:
let formattedString = NSMutableAttributedString()
formattedString.setBold("Your text here")
Run Code Online (Sandbox Code Playgroud)
但是,我试图将NSLocalizedString的子字符串的文本设置为粗体.所以我会这样尝试:
let formattedString = NSMutableAttributedString()
return NSAttributedString(string: String.localizedStringWithFormat(
NSLocalizedString("message", comment: ""),
formattedString.setBold(NSLocalizedString("message.day", comment: "")),
NSLocalizedString("message.time", comment: "")
))
Run Code Online (Sandbox Code Playgroud)
它不是" 今天晚上10点开始",而是提供以下输出:
Today{
NSFont = "<UICTFont: 0x7fb75d4f1330> font-family: \"CustomText-MediumItalic\"; font-weight: normal; font-style: italic; font-size: 14.00pt";
} starting at 10pm {
} …Run Code Online (Sandbox Code Playgroud) 我有一个UITextView带有属性的文本并allowsEditingTextAttributes设置为true.
我正在尝试使用以下代码将属性字符串转换为Data对象:
let text = self.textView.attributedText
let data = try text.data(from: NSMakeRange(0, text.length), documentAttributes: [:])
Run Code Online (Sandbox Code Playgroud)
但是,这会引发以下错误:
Error Domain=NSCocoaErrorDomain Code=66062 "(null)"
Run Code Online (Sandbox Code Playgroud)
任何想法这个错误意味着什么或可能导致这个?我正在使用最新的Xcode和iOS.谢谢.
我在上使用字距调整属性,UILabel以使用一些自定义字母间距显示其文本。不幸的是,当我显示用户生成的字符串时,有时会看到以下内容:
即有时某些表情符号字符未显示。
如果我注释掉字距调整,但改用某些段落样式,则会得到相同类型的错误渲染。
我在文档中找不到任何内容,明确拒绝了对特殊unicode字符的支持。我是在做错什么还是iOS的错误?
重现此错误的代码可在以下游乐场中获得:https://github.com/Bootstragram/Playgrounds/tree/master/LabelWithEmoji.playground
并复制到这里:
//: A UIKit based Playground for presenting user interface
import UIKit
import PlaygroundSupport
extension NSAttributedString {
static func kernedSpacedText(_ text: String,
letterSpacing: CGFloat = 0.0,
lineHeight: CGFloat? = nil) -> NSAttributedString {
// TODO add the font attribute
let attributedString = NSMutableAttributedString(string: text)
attributedString.addAttribute(NSAttributedStringKey.kern,
value: letterSpacing,
range: NSRange(location: 0, length: text.count))
if let lineHeight = lineHeight {
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineSpacing = lineHeight
attributedString.addAttribute(NSAttributedStringKey.paragraphStyle,
value: paragraphStyle,
range: NSRange(location: …Run Code Online (Sandbox Code Playgroud) ios ×6
swift ×4
uilabel ×2
cocoa ×1
cocoa-touch ×1
iphone ×1
nsdata ×1
nstextview ×1
objective-c ×1
uikit ×1
uitextview ×1
unicode ×1