Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/115.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在给定任意点大小的UIFont时,如何计算适当的笔划宽度?_Ios_Nsattributedstring_Uifont_Stroke - Fatal编程技术网

Ios 在给定任意点大小的UIFont时,如何计算适当的笔划宽度?

Ios 在给定任意点大小的UIFont时,如何计算适当的笔划宽度?,ios,nsattributedstring,uifont,stroke,Ios,Nsattributedstring,Uifont,Stroke,我想对文本应用一个实线笔划。使用NSAttributedString指定.strokeWidth可以很容易地获得该属性。但是,我发现很难确定在任何给定的点大小下,应该为笔划宽度指定一个UIFont。我可以很容易地说,在点大小为50的情况下,笔划宽度为1看起来很棒。我直觉地认为,如果字体大小加倍,则笔划宽度应加倍,因此,随着字体缩放,笔划将按比例缩放,笔划厚度将与原始“基本”字体大小一致。然而,事实并非如此。随着字体大小和笔划宽度成比例增加,笔划宽度变得太粗 这里的屏幕截图显示第一行的字体大小为

我想对文本应用一个实线笔划。使用
NSAttributedString
指定
.strokeWidth
可以很容易地获得该属性。但是,我发现很难确定在任何给定的
点大小
下,应该为
笔划宽度
指定一个
UIFont
。我可以很容易地说,在点大小为50的情况下,笔划宽度为1看起来很棒。我直觉地认为,如果字体大小加倍,则笔划宽度应加倍,因此,随着字体缩放,笔划将按比例缩放,笔划厚度将与原始“基本”字体大小一致。然而,事实并非如此。随着字体大小和笔划宽度成比例增加,笔划宽度变得太粗

这里的屏幕截图显示第一行的字体大小为50,笔划宽度为1。下一行加倍,字体大小为100,笔划宽度为2,重复此操作直到最后一行,即350对7

我相信这是因为笔划是向内和向外呈现的。其中心位于角色的边缘,然后向两个方向扩展。如果您比较这两幅图像,您可以看到这一点,这一幅没有应用笔划

因此,随着字体大小的增加,笔划宽度不应成比例增加,它需要以较慢的速度增加,以确保所有大小的厚度一致。我试图确定计算该值的正确方法

因此,给定一个看起来令人满意的基本配置(比如50pt字体大小和1pt笔划宽度)和一个新的
点大小(例如350pt),如何计算正确的
笔划宽度?或者我应该使用不同的值,而不是
pointSize

我当前按比例缩放的算法是:
让strokeWidth=font.pointSize/50
(只需求解
1/50=x/pointSize

以下是我用来绘制此文本的代码:

    let text = "hello"
    let imageRect = CGRect(x: 0, y: 0, width: 343 * 3, height: 500 * 3)

    let colorSpace = CGColorSpaceCreateDeviceRGB()
    let alphaInfo = CGImageAlphaInfo.premultipliedLast.rawValue

    let bitmapContext = CGContext(data: nil, width: Int(imageRect.width), height: Int(imageRect.height), bitsPerComponent: 8, bytesPerRow: 0, space: colorSpace, bitmapInfo: alphaInfo)!
    bitmapContext.setAlpha(1)
    bitmapContext.setTextDrawingMode(CGTextDrawingMode.fill)

    //1
    bitmapContext.textPosition = CGPoint(x: 40, y: 1080)
    let displayLineText1 = CTLineCreateWithAttributedString(NSAttributedString(string: text, attributes: [.foregroundColor: UIColor.black, .font: UIFont.systemFont(ofSize: 50), .strokeColor: UIColor.red, .strokeWidth: 1]))
    CTLineDraw(displayLineText1, bitmapContext)

    //2
    bitmapContext.textPosition = CGPoint(x: 40, y: 1000)
    let displayLineText2 = CTLineCreateWithAttributedString(NSAttributedString(string: text, attributes: [.foregroundColor: UIColor.black, .font: UIFont.systemFont(ofSize: 100), .strokeColor: UIColor.red, .strokeWidth: 2]))
    CTLineDraw(displayLineText2, bitmapContext)

    //3
    bitmapContext.textPosition = CGPoint(x: 40, y: 875)
    let displayLineText3 = CTLineCreateWithAttributedString(NSAttributedString(string: text, attributes: [.foregroundColor: UIColor.black, .font: UIFont.systemFont(ofSize: 150), .strokeColor: UIColor.red, .strokeWidth: 3]))
    CTLineDraw(displayLineText3, bitmapContext)

    //4
    bitmapContext.textPosition = CGPoint(x: 40, y: 725)
    let displayLineText4 = CTLineCreateWithAttributedString(NSAttributedString(string: text, attributes: [.foregroundColor: UIColor.black, .font: UIFont.systemFont(ofSize: 200), .strokeColor: UIColor.red, .strokeWidth: 4]))
    CTLineDraw(displayLineText4, bitmapContext)

    //5
    bitmapContext.textPosition = CGPoint(x: 40, y: 540)
    let displayLineText5 = CTLineCreateWithAttributedString(NSAttributedString(string: text, attributes: [.foregroundColor: UIColor.black, .font: UIFont.systemFont(ofSize: 250), .strokeColor: UIColor.red, .strokeWidth: 5]))
    CTLineDraw(displayLineText5, bitmapContext)

    //6
    bitmapContext.textPosition = CGPoint(x: 40, y: 310)
    let displayLineText6 = CTLineCreateWithAttributedString(NSAttributedString(string: text, attributes: [.foregroundColor: UIColor.black, .font: UIFont.systemFont(ofSize: 300), .strokeColor: UIColor.red, .strokeWidth: 6]))
    CTLineDraw(displayLineText6, bitmapContext)

    //7
    bitmapContext.textPosition = CGPoint(x: 40, y: 40)
    let displayLineText7 = CTLineCreateWithAttributedString(NSAttributedString(string: text, attributes: [.foregroundColor: UIColor.black, .font: UIFont.systemFont(ofSize: 350), .strokeColor: UIColor.red, .strokeWidth: 7]))
    CTLineDraw(displayLineText7, bitmapContext)

    let textCGImage = bitmapContext.makeImage()!
    let textImage = CIImage(cgImage: textCGImage)
起自(加强调):

此属性的值是包含 浮点值。此值表示要更改的金额 笔划宽度和指定为字体点大小的百分比。 指定0(默认值)表示没有其他更改。指定正数 值单独更改笔划宽度。将负值指定为 笔划并填充文本。例如,列出了 文本将是3.0


因此,该值已缩放到字体大小。您不应该自己缩放它。选择一个值,该值给出在任何给定字体大小下您喜欢的相对笔划宽度,并将其用于所有字体大小。

可能不会回答,因为它不依赖于字体大小。您可能需要某种类型的符号厚度。虽然你可以从核心文本中得到一些,但是你可能无法得到笔划的厚度,仅仅是框架和位置。