Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/102.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 Swift 4-PDFanRotation文本(或UITextField)为文本的大小_Ios_Swift - Fatal编程技术网

Ios Swift 4-PDFanRotation文本(或UITextField)为文本的大小

Ios Swift 4-PDFanRotation文本(或UITextField)为文本的大小,ios,swift,Ios,Swift,我已经创建了一个PDFAnnotation文本小部件,一切都很好,它是多行的,所以我要做的是将这个注释的大小设置为文本的大小…这有意义吗?我该怎么做 这是我的注解: let page = pdfView.currentPage textAnnotation = PDFAnnotation(bounds: CGRect(x: 300, y: 200, width: 600, height: 100), forType: PDFAnnotationSubtype(rawVa

我已经创建了一个PDFAnnotation文本小部件,一切都很好,它是多行的,所以我要做的是将这个注释的大小设置为文本的大小…这有意义吗?我该怎么做

这是我的注解:

let page = pdfView.currentPage

            textAnnotation = PDFAnnotation(bounds: CGRect(x: 300, y: 200, width: 600, height: 100), forType: PDFAnnotationSubtype(rawValue: PDFAnnotationSubtype.widget.rawValue), withProperties: nil)

            textAnnotation.widgetFieldType = PDFAnnotationWidgetSubtype(rawValue: PDFAnnotationWidgetSubtype.text.rawValue)

            textAnnotation.font = UIFont.systemFont(ofSize: 80)

            textAnnotation.fontColor = .red

            textAnnotation.isMultiline = true

            textAnnotation.widgetStringValue = "Text Here"

            page!.addAnnotation(textAnnotation)
如何使文本的大小与本机工具中的一样?或者我是如何对UITextField执行此操作的,因为我可能会执行或多或少相同的操作

还是什么都没有…太难了

更新

目前还没有对此进行任何处理。但在查看本机iOS PDF注释后,它似乎不会使文本字段的宽度变长,而是转到下一行,使文本区域的高度变大……我的PDFAnnotation设置为isMultiline,但是当我转到下一行时,我看不到它,因为我的高度只有100.0…那么,它如何使转到下一行并增加文本区域的高度呢

更新

我创建了自己的自定义PDF旋转

class TextAnnotation:  PDFAnnotation {

    var currentText: String?

    override init(bounds: CGRect, forType annotationType: PDFAnnotationSubtype, withProperties properties: [AnyHashable : Any]?) {
        super.init(bounds: bounds, forType: annotationType, withProperties: properties)

        self.widgetFieldType = PDFAnnotationWidgetSubtype(rawValue: PDFAnnotationWidgetSubtype.text.rawValue)

        self.font = UIFont.systemFont(ofSize: 80)

        self.isMultiline = true

        self.widgetStringValue = "Text Here"

        self.currentText = self.widgetStringValue!
    }

    override func draw(with box: PDFDisplayBox, in context: CGContext) {
        UIGraphicsPushContext(context)
        context.saveGState()

        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.alignment = .center

        let attributes: [NSAttributedString.Key: Any] = [
            .paragraphStyle: paragraphStyle,
            .font: UIFont.systemFont(ofSize: 80),
            .foregroundColor: UIColor.red
        ]

        widgetStringValue!.draw(in: pageBounds, withAttributes: attributes)

        context.restoreGState()
        UIGraphicsPopContext()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}
我注意到,每次键入某个内容时都会调用draw方法,因此我尝试将self.widgetStringValue与变量currentText进行比较,但当我尝试更改宽度时,如果它们不相等,则什么也做不了


也许我可以创建一个委托方法,并在我的ViewController中调用它以再次删除/添加注释…或者尝试从此处更改边界。

您可以使用简单的辅助函数来计算高度

/*
- Parameters:
   - constrainedWidth: The max width of the label
   - fontSize: Font size used for the UILabel
   - text: The String used in the calculations

- Returns: CGFLoat which is the height of the label
*/
func estimatedHeight(constrainedWidth width: CGFloat, fontSize: CGFloat, text:String) -> CGFloat {
    let tempLabel = UILabel(frame: CGRect(x: 0, y: 0, width: width, height: .greatestFiniteMagnitude))
    tempLabel.text = text
    tempLabel.lineBreakMode = .byWordWrapping
    tempLabel.numberOfLines = 0
    tempLabel.font.withSize(fontSize)
    tempLabel.sizeToFit()

    return tempLabel.frame.height
}

通过该函数,您将获得UILabel的高度,然后您可以简单地向注释高度添加所需的任何填充或边距。

我不擅长PDFKit,因此我将询问UITextField:在UITextField的情况下,您到底想要什么?您有文本消息,希望UITextField的大小与消息的大小完全一致。是否正确?是的,我希望文本的PDFAnnotation小部件在我尝试或前往新行时适合消息。该小部件与您的应用程序相关时的用途是什么?有一些你想要/期望的行为与你不想要的行为的截图/视频会非常有用。文本小部件的目的是向PDF中添加文本并让用户编辑文本。我认为这对PDFanMotion小部件文本不起作用。就像我现在开始想的那样,我无法访问注释中的UITextField为什么不能访问它?我没有改变你的代码,相反,我给了你一种方法,让你只得到文本的估计高度。不要使用UILabel,我只是根据文本的宽度来估计容器的高度