Metabolite identification from 1D 1H NMR spectra is a major challenge in NMR-based metabolomics. This study introduces NMRformer, a Transformer-based deep learning framework for accurate peak assignment and metabolite identification in 1D 1H NMR spectroscopy. Unlike traditional approaches, NMRformer interprets spectra as sequences of spectral peaks and integrates a self-attention mechanism and peak height ratios directly into the Transformer encoder layer. It has the capability to recognize and interpret long-range dependencies between peaks and to quickly identify peaks corresponding to identical metabolites. The effectiveness of NMRformer has been rigorously validated by analyzing real 1D 1H NMR spectra from a variety of cellular and biofluid samples. NMRformer achieved peak assignment accuracies above 88% and metabolite identification accuracies above 80% in four types of cellular samples. It also achieved peak assignment accuracies above 88% and metabolite identification accuracies above 80% in three types of biofluid samples. These results underscore the ability of NMRformer to significantly improve the accuracy and efficiency of peak assignment and metabolite identification in NMR-based metabolomics studies.