【问题标题】:How to get the number of characters of monospaced font in a line of wrapped text in NSTextView?如何获取NSTextView中一行换行文本中等宽字体的字符数?
【发布时间】:2017-01-09 18:40:42
【问题描述】:

我想知道在 NSTextView 中有多少个字母有一整行由没有空格的字母和换行符组成的包裹文本。文本的字体被定义为等宽字体。我找不到如何获取字体的宽度(我认为我可以从 NSTextView 的宽度和字体的宽度来计算它)。

【问题讨论】:

    标签: swift string font-size nstextview


    【解决方案1】:

    假设您的文本视图包含一整行或更多

    var range = NSMakeRange(NSNotFound, 0)
    textView.layoutManager?.lineFragmentRectForGlyphAtIndex(0, effectiveRange: &range)
    
    print(range.length) // number of characters that can be fitted on a single line
    

    【讨论】:

    • 感谢您提供非常有帮助的答案!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-22
    相关资源
    最近更新 更多