我看到有一个属性name(NSParagraphStyleAttributeName)用于将段落样式应用于Cocoa中的文本.文本存储在一个NSAttributedString,但是在该字符串中定义"段落"的内容 - 它是换行符\n吗?其他人物?如果它是a \n,那么如何在不启动新段落的情况下创建新行.最后,当您将ParagraphStyle附加到字符串时,是否必须使用整个段落的确切范围,或者可以将其放在段落中的任何子范围内.如果它可以是子范围,系统如何处理同一段落上的两个或多个ParagraphStyles?
谢谢,罗布
我一直在使用NSStrokeWidthAttributeName的NSAttributedString对象,因为它是画把大纲文本周围.问题是笔划位于文本的填充区域内.当文本很小(例如1像素厚)时,抚摸使文本难以阅读.我真正想要的是外面的中风.有没有办法做到这一点?
我试过NSShadow没有偏移和模糊,但它太模糊,很难看到.如果有办法增加阴影的大小而没有任何模糊,那也可以.
我正在尝试很好地显示NSTextView中突出显示的段落.现在,我是通过创建一个背景颜色的NSAttributedString来做到这一点的.这是一些简化的代码:
NSDictionary *attributes = @{NSBackgroundColorAttributeName:NSColor.greenColor};
NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:@"Here is a single line of text with single spacing" attributes:attributes];
[textView.textStorage setAttributedString:attrString];
Run Code Online (Sandbox Code Playgroud)
这种方法基本上有效,因为它产生突出显示的文本.

不幸的是,当存在多条线时,除了线本身之外,高光覆盖线之间的垂直空间,导致丑陋.

有谁知道在Cocoa中进行这种突出显示的方法?下面的图片基本上就是我要找的东西(忽略白框上的阴影):

我愿意使用CoreText,html或任何必要的东西来使事情看起来更好.
我已经使用这个答案来为特定范围的文本创建CGRect.
在这里UITextView我设置了它attributedText(所以我有一堆带有不同字形大小的样式文本).
这对于左对齐的第一行文本非常有用,但在使用NSTextAlignmentJustified或时使用时会产生一些非常奇怪的结果NSTextAlignmentCenter.
当线条缠绕时它也不能正确计算,或者(如果有\n线条断裂).
我得到这样的东西(这是中心对齐):

相反,我期待这个:

这个有一个\n换行符 - 前两个代码位被成功突出显示,但最后一个more code for you to see不是因为文本换行没有考虑到x,y计算中.
- (void)formatMarkdownCodeBlockWithAttributes:(NSDictionary *)attributesDict
withHighlightProperties:(NSDictionary *)highlightProperties
forFontSize:(CGFloat)pointSize
{
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"`.+?`" options:NO error:nil];
NSArray *matchesArray = [regex matchesInString:[self.attributedString string] options:NO range:NSMakeRange(0, self.attributedString.length)];
for (NSTextCheckingResult *match in matchesArray)
{
NSRange range = [match range];
if (range.location != NSNotFound) {
self.textView.attributedText = self.attributedString;
CGRect codeRect = [self frameOfTextRange:range forString:[[self.attributedString string] …Run Code Online (Sandbox Code Playgroud) 我在我的UITextView中为我的NSAttributedString使用了合理的文本:
var paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = NSTextAlignment.Justified;
normalAttributes.setObject(paragraphStyle, forKey: NSParagraphStyleAttributeName)
Run Code Online (Sandbox Code Playgroud)
问题是它会拉伸一些单词以实现对齐,而不是仅仅拉伸单词之间的空格.我发现这非常令人分心,并且知道有一种方法可以使单词不被拉伸,而只是间距.下面是我的应用程序的截图,后面是一个来自应用程序的屏幕截图,其中一些功能也使用了对齐的文本和完全相同的字体.


我如何达到同样的效果?
任何帮助将不胜感激.
我正在使用NSAttributedString为来自API的文本的某些部分(在Twitter上认为"@mention")着色.
API为我提供了文本和一组实体,这些实体表示应该着色的文本部分(或链接,标签等).
但有时,由于表情符号,颜色会被抵消.
例如,使用此文本:
"@ericd一些文字.@apero"
API给出:
[{"text":"ericd","len":6,"pos":0},{"text":"apero","len":6,"pos":18}]
我使用NSRange成功转换为NSAttributedString:
for m in entities.mentions {
let r = NSMakeRange(m.pos, m.len)
myAttributedString.addAttribute(NSForegroundColorAttributeName, value: someValue, range: r)
}
Run Code Online (Sandbox Code Playgroud)
我们认为这"pos": 18是正确的,这是"@apero"开始的地方.正如预期的那样,彩色部分是"@ericd"和"@apero".
但是当在文本中使用表情符号的某些特定组合时,API不能很好地转换为NSATtributedString,颜色是偏移的:
"@ericd一些文字.✌@apero"
得到:
[{"text":"ericd","len":6,"pos":0},{"text":"apero","len":6,"pos":22}]
"pos": 22:API作者声明这是正确的,我理解他们的观点.
不幸的是,NSAttributedString不同意,我的着色是关闭的:
第二次提到的最后一个字符没有着色(因为因为表情符号,"pos"太短了?).
正如您可能已经猜到的那样,我无论如何都无法改变API的行为方式,我必须在客户端进行调整.
除此之外......我不知道该怎么做.我是否应该尝试检测文本中的表情符号,并在有问题的表情符号时手动修改提及的位置?但是,检测哪个表情符号改变位置以及哪个表情符号没有改变的标准是什么?以及如何确定我需要多少偏移?也许问题是由NSAttributedString引起的?
我知道这与表情符号的长度相比,与他们作为离散字符的长度相比,但是......嗯......我迷失了(叹气).
请注意,我已经尝试实现类似于这个东西的解决方案,因为我的API与此兼容,但它只是部分工作,一些表情符号仍在破坏索引:
我正在从swift 3转到swift 4.我有UILabels,我给标签提供了非常具体的文本属性.初始化strokeTextAttributes时,我在获取"意外发现nil时解包可选值"错误.坦白说,我完全迷失了.
在swift 3中,strokeTextAttributes是[String:Any],但是swift 4引发了错误,直到我将其更改为下面的内容.
let strokeTextAttributes = [
NSAttributedStringKey.strokeColor.rawValue : UIColor.black,
NSAttributedStringKey.foregroundColor : UIColor.white,
NSAttributedStringKey.strokeWidth : -2.0,
NSAttributedStringKey.font : UIFont.boldSystemFont(ofSize: 18)
] as! [NSAttributedStringKey : Any]
chevronRightLabel.attributedText = NSMutableAttributedString(string: "0", attributes: strokeTextAttributes)
Run Code Online (Sandbox Code Playgroud) 我只是想知道如何更改NSAttributedString的背景颜色,我可以看到背景一直是白色,但我想让它变黑.
那可能吗??非常感谢!
我试图从UITextView(和/或UITextField)获取选定的文本范围,以便我可以编辑选定的文本,或修改属性字符串.我做出选择时会触发下面的方法,但方法中的代码会返回空值.
- (void)textViewDidChangeSelection:(UITextView *)textView {
UITextRange *selectedRange = [textField selectedTextRange];
NSLog(@"Start: %@ <> End: %@", selectedRange.start, selectedRange.end);
}
Run Code Online (Sandbox Code Playgroud) 编辑
请参阅我的答案以获得完整的解决方案:
我设法通过使用UITextView而不是a 来解决这个问题UILabel.我写了一个类,使UITextView行为像一个UILabel但具有完全准确的链接检测.
我已经成功设置了链接的样式而没有使用问题,NSMutableAttributedString但我无法准确地检测到哪个字符已被点击.我已经尝试了这个问题中的所有解决方案(我可以转换为Swift 4代码),但没有运气.
以下代码有效,但无法准确检测单击了哪个字符并获取了链接的错误位置:
func didTapAttributedTextInLabel(label: UILabel, inRange targetRange: NSRange) -> Bool {
// Create instances of NSLayoutManager, NSTextContainer and NSTextStorage
let layoutManager = NSLayoutManager()
let textContainer = NSTextContainer(size: CGSize.zero)
let textStorage = NSTextStorage(attributedString: label.attributedText!)
// Configure layoutManager and textStorage
layoutManager.addTextContainer(textContainer)
textStorage.addLayoutManager(layoutManager)
// Configure textContainer
textContainer.lineFragmentPadding = 0.0
textContainer.lineBreakMode = label.lineBreakMode
textContainer.maximumNumberOfLines = label.numberOfLines
let labelSize = label.bounds.size
textContainer.size = labelSize
// Find the tapped …Run Code Online (Sandbox Code Playgroud) ios ×6
cocoa ×3
uitextview ×3
macos ×2
nstextview ×2
objective-c ×2
swift ×2
swift4 ×2
uilabel ×2
core-text ×1
emoji ×1
iphone ×1
nsrange ×1
uitextfield ×1
uitextrange ×1
unicode ×1
xcode ×1