jjzjj

ios - NSAttributedString 行尾的 NSKernAttributeName 空格

coder 2023-07-16 原文

当使用 NSKernAttributeName 时,它会在每一行的末尾放置一个空格,有什么办法可以解决这个问题吗?我可以将属性设置在以下范围内:

NSRange(location: 0, length: self.text!.characters.count-1)

但我不想为每一行都设置这个。

这是我正在使用的 Playground 中的测试代码

//: Playground - noun: a place where people can play

import UIKit
import XCPlayground

var text = "Hello, playground\nhow are you?"

let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.paragraphSpacing = 50
paragraphStyle.alignment = NSTextAlignment.Left
paragraphStyle.lineBreakMode = NSLineBreakMode.ByTruncatingTail

let attributes = [
    NSParagraphStyleAttributeName: paragraphStyle
    , NSKernAttributeName: 20
]


let attributedString = NSAttributedString(string: text, attributes: attributes)

let label = UILabel()
label.attributedText = attributedString
label.numberOfLines = 0
label.textColor = UIColor.greenColor()
label.backgroundColor = UIColor.orangeColor()
label.sizeToFit()
label.center = CGPoint(x: 500, y: 100)


var text2 = "What's up\nWhere are you?"
let attributedString2 = NSAttributedString(string: text2, attributes: attributes)

let label2 = UILabel()
label2.attributedText = attributedString2
label2.numberOfLines = 0
label2.textColor = UIColor.greenColor()
label2.backgroundColor = UIColor.orangeColor()
label2.sizeToFit()
label2.center = CGPoint(x: 500, y: 250)

var text3 = "Hello"
let attributedString3 = NSAttributedString(string: text3, attributes: attributes)

let label3 = UILabel()
label3.attributedText = attributedString3
label3.numberOfLines = 0
label3.textColor = UIColor.greenColor()
label3.backgroundColor = UIColor.orangeColor()
label3.sizeToFit()
label3.center = CGPoint(x: 500, y: 400)

let holderView = UIView(frame: CGRect(x: 0, y: 0, width: 1000, height: 500))
holderView.backgroundColor = UIColor.lightGrayColor()
holderView.addSubview(label)
holderView.addSubview(label2)
holderView.addSubview(label3)

XCPlaygroundPage.currentPage.liveView = holderView

结果是这样的:

您可以看到每行末尾的空格。

最佳答案

这实际上是字距调整工作原理的定义;它调整紧缩字符和下一个字符所在位置之间的空间。是否继续绘制下一个字符无关紧要。

Standard Attributes

The kerning attribute indicates how much the following character should be shifted from its default offset as defined by the current character’s font; a positive kern indicates a shift farther along and a negative kern indicates a shift closer to the current character.

如果有帮助,请考虑在文字处理器中执行此操作。如果打开了字距调整,并且您键入了一个字符,那么您希望插入点在哪里?预期的答案将是“从刚输入的字符偏移 kern 的数量”,因为这就是在 kern 为 0 的默认情况下发生的情况,对吗?好吧,这正是这里发生的事情:如果您对字符串的最后一个字符进行紧排,那么该字符串将包括最后一个紧排。

所以这里正确的做法是将您的 dropLast() 逻辑包装在一个扩展中,然后收工。

关于ios - NSAttributedString 行尾的 NSKernAttributeName 空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37594704/

有关ios - NSAttributedString 行尾的 NSKernAttributeName 空格的更多相关文章

  1. ruby - 如何验证 IO.copy_stream 是否成功 - 2

    这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下

  2. Ruby 文件 IO 定界符? - 2

    我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的

  3. Get https://registry-1.docker.io/v2/: net/http: request canceled while waiting - 2

    1.错误信息:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:requestcanceledwhilewaitingforconnection(Client.Timeoutexceededwhileawaitingheaders)或者:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:TLShandshaketimeout2.报错原因:docker使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里

  4. ruby - ruby 乘法语句中星号中断语法前的空格 - 2

    在添加一些空格以使代码更具可读性时(与上面的代码对齐),我遇到了这个:classCdefx42endendm=C.new现在这将给出“错误数量的参数”:m.x*m.x这将给出“语法错误,意外的tSTAR,期待$end”:2/m.x*m.x这里的解析器到底发生了什么?我使用Ruby1.9.2和2.1.5进行了测试。 最佳答案 *用于运算符(42*42)和参数解包(myfun*[42,42])。当你这样做时:m.x*m.x2/m.x*m.xRuby将此解释为参数解包,而不是*运算符(即乘法)。如果您不熟悉它,参数解包(有时也称为“spl

  5. ruby - 为什么不能使用类IO的实例方法noecho? - 2

    print"Enteryourpassword:"pass=STDIN.noecho(&:gets)puts"Yourpasswordis#{pass}!"输出:Enteryourpassword:input.rb:2:in`':undefinedmethod`noecho'for#>(NoMethodError) 最佳答案 一开始require'io/console'后来的Ruby1.9.3 关于ruby-为什么不能使用类IO的实例方法noecho?,我们在StackOverflow上

  6. ruby - 将 Ruby 中的字符串切成固定长度的字符串,忽略(不考虑/不管)新行或空格字符 - 2

    我有一个包含许多换行符和空格的字符串。我需要将它拆分成固定长度的子字符串。例如a="Thisissome\nText\nThisissometext"现在我想把它分成长度为17的字符串。所以现在它应该导致["Thisissome\nText","\nThisissometex","t"]评论:我的字符串可能包含任何字符(空格/单词等) 最佳答案 "Thisissome\nText\nThisissometext".scan(/.{1,17}/m)#=>["Thisissome\nText","\nThisissometex","t"

  7. ruby - 至少 2 个空格的正则表达式? - 2

    我需要忽略任何一个空格,并且应该匹配至少大于一个空格的空格..."MARYHADALITTLELAMB"我希望"MARY","HADALITTLE","LAMB" 最佳答案 空格匹配是\s,您可以在花括号中提供最小值和最大值。您也可以省略其中任何一个,如下所示:\s{2,}所以你的代码应该是这样的:"MARYHADALITTLELAMB".split(/\s{2,}/)Youcantestitonlinehere! 关于ruby-至少2个空格的正则表达式?,我们在StackOverflo

  8. ruby - 在 ruby​​ 中的字符串前后添加空格? - 2

    我想在随机字符串前后添加一个空格。我试过使用"Random_string".center(1,"")但它不起作用。谢谢 最佳答案 我发现这是最优雅的解决方案:padded_string="#{random_string}"走简单的路没有错。 关于ruby-在ruby​​中的字符串前后添加空格?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/3357897/

  9. ruby-on-rails - 在 Rails 中格式化没有空格的日期 - 2

    我使用的API返回日期为"20090320",即Y、m和d。我如何在rails中将其格式化为20-03-2009?提前致谢! 最佳答案 Date.strptime('20090320','%Y%m%d').strftime('%d-%m-%Y') 关于ruby-on-rails-在Rails中格式化没有空格的日期,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/21364460/

  10. ruby - 什么是 Ruby 正则表达式来匹配至少有一个句点且没有空格的字符串? - 2

    匹配至少有一个句点且没有空格的字符串的正则表达式是什么? 最佳答案 你可以使用这个:/^\S*\.\S*$/它是这样工作的:^您可以将\S替换为[^]以严格使用空格(而不是制表符等) 关于ruby-什么是Ruby正则表达式来匹配至少有一个句点且没有空格的字符串?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/3913436/

随机推荐