如果我有一个很长的NSTextStorage并且我知道NSTextContainer的CGSize,我如何计算文本页数? 是否有针对此事的标准程序或某种动态计数的方法? 我找不到一个很好的教程,可以使用自定义字体从原始文本制作iBooks书(我需要绘制20-50页的文本)。
答案 0 :(得分:1)
我找到了一种使用TextKit的NSLayoutManager计算多个页面的好方法。我创建NSTextContainers时只计算页数
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Calculator extends JFrame {
private JTextField Number1TxtField;
private JTextField Number2TxtField;
private JTextField ResultTxtField;
private JButton add;
private JButton subtract;
private JButton multiply;
private JButton divide;
public Calculator() { // class for the calculator
JPanel topPanel = new JPanel();
topPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 8, 5));
topPanel.add(new JLabel("Number 1"));
Number1TxtField = new JTextField(5);
topPanel.add(Number1TxtField);
topPanel.add(new JLabel("Number 2"));
Number2TxtField = new JTextField(5);
topPanel.add(Number2TxtField);
topPanel.add(new JLabel("Result"));
ResultTxtField = new JTextField(8);
topPanel.add(ResultTxtField);
ResultTxtField.setEditable(false);
topPanel.add(ResultTxtField);
// new JPanel
JPanel buttonsPanel = new JPanel();
buttonsPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 8, 5));
add = new JButton("Add");
buttonsPanel.add(add);
subtract = new JButton("Subtract");
buttonsPanel.add(subtract);
multiply = new JButton("Multiply");
buttonsPanel.add(multiply);
divide = new JButton("Divide");
buttonsPanel.add(divide);
ButtonListener btnlistener = new ButtonListener();
add.addActionListener(btnlistener);
subtract.addActionListener(btnlistener);
multiply.addActionListener(btnlistener);
divide.addActionListener(btnlistener);
getContentPane().add(topPanel, BorderLayout.NORTH);
getContentPane().add(buttonsPanel);
}
class ButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String num1str = Number1TxtField.getText();
double num1 = Double.parseDouble(num1str);
String num2str = Number2TxtField.getText();
double num2 = Double.parseDouble(num2str);
double result;
if (e.getSource() == add)
result = num1 + num2;
else if (e.getSource() == subtract)
result = num1 - num2;
else if (e.getSource() == multiply)
result = num1 * num2;
else // DivideButton
result = num1 / num2;
ResultTxtField.setText(String.valueOf(result));
}
}
public static void main(String[] args) {
Calculator frame = new Calculator();
frame.setTitle("Calculator");
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
columnCount是您使用多列布局
时的列数frameForViewAtIndex为scrollView
中的当前textView返回CGRect